Skip to content

Commit 8ca296b

Browse files
committed
day13
1 parent d0c2394 commit 8ca296b

File tree

4 files changed

+803
-0
lines changed

4 files changed

+803
-0
lines changed

doc/v2022/day13.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Day 13: Distress Signal
2+
3+
## Part 1
4+
5+
You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a distress signal.
6+
7+
Your handheld device must still not be working properly; the packets from the distress signal got decoded out of order. You'll need to re-order the list of received packets (your puzzle input) to decode the message.
8+
9+
Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify how many pairs of packets are in the right order.
10+
11+
For example:
12+
13+
```
14+
[1,1,3,1,1]
15+
[1,1,5,1,1]
16+
17+
[[1],[2,3,4]]
18+
[[1],4]
19+
20+
[9]
21+
[[8,7,6]]
22+
23+
[[4,4],4,4]
24+
[[4,4],4,4,4]
25+
26+
[7,7,7,7]
27+
[7,7,7]
28+
29+
[]
30+
[3]
31+
32+
[[[]]]
33+
[[]]
34+
35+
[1,[2,[3,[4,[5,6,7]]]],8,9]
36+
[1,[2,[3,[4,[5,6,0]]]],8,9]
37+
```
38+
39+
Packet data consists of lists and integers. Each list starts with [, ends with ], and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line.
40+
41+
When comparing two values, the first value is called left and the second value is called right. Then:
42+
43+
* If both values are integers, the lower integer should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input.
44+
* If both values are lists, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input.
45+
* If exactly one value is an integer, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing [0,0,0] and 2, convert the right value to [2] (a list containing 2); the result is then found by instead comparing [0,0,0] and [2].
46+
47+
Using these rules, you can determine which of the pairs in the example are in the right order:
48+
49+
```
50+
== Pair 1 ==
51+
- Compare [1,1,3,1,1] vs [1,1,5,1,1]
52+
- Compare 1 vs 1
53+
- Compare 1 vs 1
54+
- Compare 3 vs 5
55+
- Left side is smaller, so inputs are in the right order
56+
57+
== Pair 2 ==
58+
- Compare [[1],[2,3,4]] vs [[1],4]
59+
- Compare [1] vs [1]
60+
- Compare 1 vs 1
61+
- Compare [2,3,4] vs 4
62+
- Mixed types; convert right to [4] and retry comparison
63+
- Compare [2,3,4] vs [4]
64+
- Compare 2 vs 4
65+
- Left side is smaller, so inputs are in the right order
66+
67+
== Pair 3 ==
68+
- Compare [9] vs [[8,7,6]]
69+
- Compare 9 vs [8,7,6]
70+
- Mixed types; convert left to [9] and retry comparison
71+
- Compare [9] vs [8,7,6]
72+
- Compare 9 vs 8
73+
- Right side is smaller, so inputs are not in the right order
74+
75+
== Pair 4 ==
76+
- Compare [[4,4],4,4] vs [[4,4],4,4,4]
77+
- Compare [4,4] vs [4,4]
78+
- Compare 4 vs 4
79+
- Compare 4 vs 4
80+
- Compare 4 vs 4
81+
- Compare 4 vs 4
82+
- Left side ran out of items, so inputs are in the right order
83+
84+
== Pair 5 ==
85+
- Compare [7,7,7,7] vs [7,7,7]
86+
- Compare 7 vs 7
87+
- Compare 7 vs 7
88+
- Compare 7 vs 7
89+
- Right side ran out of items, so inputs are not in the right order
90+
91+
== Pair 6 ==
92+
- Compare [] vs [3]
93+
- Left side ran out of items, so inputs are in the right order
94+
95+
== Pair 7 ==
96+
- Compare [[[]]] vs [[]]
97+
- Compare [[]] vs []
98+
- Right side ran out of items, so inputs are not in the right order
99+
100+
== Pair 8 ==
101+
- Compare [1,[2,[3,[4,[5,6,7]]]],8,9] vs [1,[2,[3,[4,[5,6,0]]]],8,9]
102+
- Compare 1 vs 1
103+
- Compare [2,[3,[4,[5,6,7]]]] vs [2,[3,[4,[5,6,0]]]]
104+
- Compare 2 vs 2
105+
- Compare [3,[4,[5,6,7]]] vs [3,[4,[5,6,0]]]
106+
- Compare 3 vs 3
107+
- Compare [4,[5,6,7]] vs [4,[5,6,0]]
108+
- Compare 4 vs 4
109+
- Compare [5,6,7] vs [5,6,0]
110+
- Compare 5 vs 5
111+
- Compare 6 vs 6
112+
- Compare 7 vs 0
113+
- Right side is smaller, so inputs are not in the right order
114+
```
115+
116+
What are the indices of the pairs that are already in the right order? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is 13.
117+
118+
Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs?
119+
120+
## Part 2
121+
122+
Now, you just need to put all of the packets in the right order. Disregard the blank lines in your list of received packets.
123+
124+
The distress signal protocol also requires that you include two additional divider packets:
125+
126+
```
127+
[[2]]
128+
[[6]]
129+
```
130+
131+
Using the same rules as before, organize all packets - the ones in your list of received packets as well as the two divider packets - into the correct order.
132+
133+
For the example above, the result of putting the packets in the correct order is:
134+
135+
```
136+
[]
137+
[[]]
138+
[[[]]]
139+
[1,1,3,1,1]
140+
[1,1,5,1,1]
141+
[[1],[2,3,4]]
142+
[1,[2,[3,[4,[5,6,0]]]],8,9]
143+
[1,[2,[3,[4,[5,6,7]]]],8,9]
144+
[[1],4]
145+
[[2]]
146+
[3]
147+
[[4,4],4,4]
148+
[[4,4],4,4,4]
149+
[[6]]
150+
[7,7,7]
151+
[7,7,7,7]
152+
[[8,7,6]]
153+
[9]
154+
```
155+
156+
Afterward, locate the divider packets. To find the decoder key for this distress signal, you need to determine the indices of the two divider packets and multiply them together. (The first packet is at index 1, the second packet is at index 2, and so on.) In this example, the divider packets are 10th and 14th, and so the decoder key is 140.
157+
158+
Organize all of the packets into the correct order. What is the decoder key for the distress signal?
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package adventofcode.v2022;
2+
3+
import adventofcode.Utils;
4+
import org.apache.commons.collections4.ListUtils;
5+
import org.apache.commons.lang3.StringUtils;
6+
import org.apache.commons.lang3.tuple.Pair;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
import java.util.stream.Collectors;
14+
import java.util.stream.IntStream;
15+
import java.util.stream.Stream;
16+
17+
public class Day13 {
18+
19+
private static final Pattern PACKET_CONTENT = Pattern.compile("^\\[(.*)]$");
20+
21+
public static void main(String[] args) {
22+
Utils.exec(Day13::part1, Day13::part2);
23+
}
24+
25+
public static void part1() {
26+
List<Pair<Element, Element>> packets = readInput();
27+
Map<Integer, Boolean> compareResult = compare(packets);
28+
int result = compareResult.entrySet().stream()
29+
.filter(Map.Entry::getValue)
30+
.mapToInt(entry -> entry.getKey() + 1)
31+
.sum();
32+
System.out.println("Result : " + result);
33+
}
34+
35+
36+
public static void part2() {
37+
List<Element> packets = readInput().stream()
38+
.flatMap(pair -> Stream.of(pair.getLeft(), pair.getRight()))
39+
.collect(Collectors.toList());
40+
Complex add1 = new Complex(List.of(new Complex(List.of(new Value(2)))));
41+
packets.add(add1);
42+
Complex add2 = new Complex(List.of(new Complex(List.of(new Value(6)))));
43+
packets.add(add2);
44+
packets.sort((o1, o2) -> switch (o1.compareTo(o2)) {
45+
case EQUAL -> 0;
46+
case GREATER -> 1;
47+
case SMALLER -> -1;
48+
});
49+
int indexAdd1 = packets.indexOf(add1) + 1;
50+
int indexAdd2 = packets.indexOf(add2) + 1;
51+
System.out.println("Result : " + (indexAdd1 * indexAdd2));
52+
}
53+
54+
static Map<Integer, Boolean> compare(List<Pair<Element, Element>> packets) {
55+
return IntStream.range(0, packets.size())
56+
.mapToObj(i -> Pair.of(i, packets.get(i).getLeft().compareTo(packets.get(i).getRight())))
57+
.collect(Collectors.toMap(Pair::getLeft, pair -> pair.getRight() == Element.ComparisonState.SMALLER));
58+
}
59+
60+
static List<Pair<Element, Element>> readInput() {
61+
List<String> lines = Utils.readInput("/v2022/d13/input.txt");
62+
return ListUtils.partition(lines, 3).stream()
63+
.map(packets -> {
64+
String left = packets.get(0);
65+
String right = packets.get(1);
66+
return Pair.of(left, right);
67+
})
68+
.map(Day13::parse)
69+
.toList();
70+
}
71+
72+
static Pair<Element, Element> parse(Pair<String, String> strPair) {
73+
return Pair.of(parse(strPair.getLeft()), parse(strPair.getRight()));
74+
}
75+
76+
static Element parse(String element) {
77+
try {
78+
return new Value(Integer.parseInt(element));
79+
} catch (NumberFormatException e) {
80+
List<Element> elements = tokenize(element).stream()
81+
.map(Day13::parse)
82+
.toList();
83+
return new Complex(elements);
84+
}
85+
}
86+
87+
static List<String> tokenize(String element) {
88+
List<String> tokens = new ArrayList<>();
89+
StringBuilder currentToken = new StringBuilder();
90+
int bracketCount = 0;
91+
Matcher matcher = PACKET_CONTENT.matcher(element);
92+
if (matcher.matches()) {
93+
for (char c : matcher.group(1).toCharArray()) {
94+
if (c == ',' && bracketCount == 0) {
95+
tokens.add(currentToken.toString());
96+
currentToken = new StringBuilder();
97+
continue;
98+
}
99+
if (c == '[') {
100+
bracketCount++;
101+
}
102+
if (c == ']') {
103+
bracketCount--;
104+
}
105+
currentToken.append(c);
106+
}
107+
if (StringUtils.isNotBlank(currentToken)) {
108+
tokens.add(currentToken.toString());
109+
}
110+
}
111+
return tokens;
112+
}
113+
114+
interface Element {
115+
116+
ComparisonState compareTo(Element element);
117+
118+
enum ComparisonState {
119+
SMALLER, EQUAL, GREATER
120+
}
121+
122+
}
123+
124+
record Value(int value) implements Element {
125+
126+
@Override
127+
public ComparisonState compareTo(Element element) {
128+
if (element instanceof Value otherValue) {
129+
int left = value;
130+
int right = otherValue.value();
131+
if (left > right) {
132+
return ComparisonState.GREATER;
133+
} else if (left < right) {
134+
return ComparisonState.SMALLER;
135+
} else {
136+
return ComparisonState.EQUAL;
137+
}
138+
}
139+
return new Complex(List.of(this)).compareTo(element);
140+
}
141+
}
142+
143+
record Complex(List<Element> elements) implements Element {
144+
145+
@Override
146+
public ComparisonState compareTo(Element element) {
147+
if (element instanceof Value otherValue) {
148+
return compareTo(new Complex(List.of(otherValue)));
149+
}
150+
if (element instanceof Complex otherComplex) {
151+
for (int i = 0; i < this.elements.size(); i++) {
152+
if (otherComplex.elements().size() == i) {
153+
return ComparisonState.GREATER;
154+
}
155+
Element left = this.elements.get(i);
156+
Element right = otherComplex.elements().get(i);
157+
ComparisonState comparisonState = left.compareTo(right);
158+
if (comparisonState == ComparisonState.EQUAL) {
159+
continue;
160+
}
161+
return comparisonState;
162+
}
163+
if (elements.size() < otherComplex.elements.size()) {
164+
return ComparisonState.SMALLER;
165+
} else if (elements.size() == otherComplex.elements.size()) {
166+
return ComparisonState.EQUAL;
167+
}
168+
}
169+
return ComparisonState.GREATER;
170+
}
171+
}
172+
173+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[1,1,3,1,1]
2+
[1,1,5,1,1]
3+
4+
[[1],[2,3,4]]
5+
[[1],4]
6+
7+
[9]
8+
[[8,7,6]]
9+
10+
[[4,4],4,4]
11+
[[4,4],4,4,4]
12+
13+
[7,7,7,7]
14+
[7,7,7]
15+
16+
[]
17+
[3]
18+
19+
[[[]]]
20+
[[]]
21+
22+
[1,[2,[3,[4,[5,6,7]]]],8,9]
23+
[1,[2,[3,[4,[5,6,0]]]],8,9]

0 commit comments

Comments
 (0)