Skip to content

Commit e48b2e8

Browse files
committed
Advent of Code 2021 - Day 14
1 parent e4633e5 commit e48b2e8

File tree

6 files changed

+210
-6
lines changed

6 files changed

+210
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ Last year I ran a [cruise log for my 25 days competing in the Advent of Code](ht
3131
- [The Advent of Code 2021 Day 11, Octopussy light](https://www.codingnagger.com/2021/12/11/the-advent-of-code-2021-day-11-octopussy-light/)
3232
- [The Advent of Code 2021 Day 12, Going deep and hard](https://www.codingnagger.com/2021/12/12/the-advent-of-code-2021-day-12-going-deep-and-hard/)
3333
- [The Advent of Code 2021 Day 13, Nautical origami](https://www.codingnagger.com/2021/12/13/the-advent-of-code-2021-day-13-nautical-origami/)
34+
- [The Advent of Code 2021 Day 14, Buckets of love](https://www.codingnagger.com/2021/12/14/the-advent-of-code-2021-day-14-buckets-of-love/)

src/main/java/com/codingnagger/App.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.codingnagger.days.Day11;
66
import com.codingnagger.days.Day12;
77
import com.codingnagger.days.Day13;
8+
import com.codingnagger.days.Day14;
89
import com.codingnagger.days.Day9;
910
import com.codingnagger.utils.InputLoader;
1011

@@ -18,9 +19,9 @@ public class App {
1819
public static void main(String[] args) throws IOException {
1920
System.out.println("Advent of Code 2021");
2021

21-
List<String> input = InputLoader.Load("day13.txt");
22+
List<String> input = InputLoader.Load("day14.txt");
2223

23-
Day day = new Day13();
24+
Day day = new Day14();
2425

2526
System.out.println("Part 1:");
2627
System.out.println(day.partOne(input));
Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,97 @@
11
package com.codingnagger.days;
22

3+
import java.math.BigInteger;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.HashMap;
8+
import java.util.LinkedHashMap;
39
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
412

513
public class Day14 implements Day {
614
@Override
715
public String partOne(List<String> input) {
8-
return null;
16+
return polymerize(input, 10);
917
}
1018

1119
@Override
1220
public String partTwo(List<String> input) {
13-
return null;
21+
return polymerize(input, 40);
22+
}
23+
24+
private String polymerize(List<String> input, int rounds) {
25+
LinkedHashMap<String, BigInteger> bucket = createBucket(input.get(0));
26+
Map<String, Character> insertionPairs = input.stream().skip(2).map(i -> i.split(" -> ")).collect(Collectors.toUnmodifiableMap(i -> i[0], i -> i[1].charAt(0)));
27+
28+
for (int i = 0; i < rounds; i++) {
29+
bucket = polymerize(bucket, insertionPairs);
30+
}
31+
32+
Map<Character, BigInteger> countCharacters = countCharacters(bucket);
33+
34+
return String.valueOf(countCharacters.values().stream().max(Comparator.naturalOrder()).get().subtract(countCharacters.values().stream().min(Comparator.naturalOrder()).get()));
35+
}
36+
37+
private LinkedHashMap<String, BigInteger> createBucket(String polymerTemplate) {
38+
LinkedHashMap<String, BigInteger> bucket = new LinkedHashMap<>();
39+
40+
List<Character> template = Arrays.stream(polymerTemplate.split("")).map(s -> s.charAt(0)).collect(Collectors.toList());
41+
42+
for (int i = 0; i < template.size()-1; i++) {
43+
String pair = new String(new char[]{template.get(i), template.get(i+1)});
44+
if (bucket.containsKey(pair)) {
45+
bucket.put(pair, bucket.get(pair).add(BigInteger.ONE));
46+
} else {
47+
bucket.put(pair, BigInteger.ONE);
48+
}
49+
}
50+
51+
return bucket;
52+
}
53+
54+
private Map<Character, BigInteger> countCharacters(LinkedHashMap<String, BigInteger> polymerTemplate) {
55+
Map<Character, BigInteger> characterCounts = new HashMap<>();
56+
57+
for (Map.Entry<String, BigInteger> pair : polymerTemplate.entrySet()) {
58+
char[] chars = pair.getKey().toCharArray();
59+
60+
if (characterCounts.isEmpty()) {
61+
characterCounts.put(chars[0], polymerTemplate.get(pair.getKey()));
62+
}
63+
64+
if (!characterCounts.containsKey(chars[1])) {
65+
characterCounts.put(chars[1], polymerTemplate.get(pair.getKey()));
66+
} else {
67+
characterCounts.put(chars[1], characterCounts.get(chars[1]).add(polymerTemplate.get(pair.getKey())));
68+
}
69+
}
70+
71+
return characterCounts;
72+
}
73+
74+
private LinkedHashMap<String, BigInteger> polymerize(LinkedHashMap<String, BigInteger> bucket, Map<String, Character> insertionPairs) {
75+
LinkedHashMap<String, BigInteger> newBucket = new LinkedHashMap<>();
76+
77+
for (String key : bucket.keySet()) {
78+
char c = insertionPairs.get(key);
79+
String firstNewPair = new String(new char[]{key.charAt(0), c});
80+
String secondNewPair = new String(new char[]{c, key.charAt(1)});
81+
82+
if (newBucket.containsKey(firstNewPair)) {
83+
newBucket.put(firstNewPair, newBucket.get(firstNewPair).add(bucket.get(key)));
84+
} else {
85+
newBucket.put(firstNewPair, bucket.get(key));
86+
}
87+
88+
if (newBucket.containsKey(secondNewPair)) {
89+
newBucket.put(secondNewPair, newBucket.get(secondNewPair).add(bucket.get(key)));
90+
} else {
91+
newBucket.put(secondNewPair, bucket.get(key));
92+
}
93+
}
94+
95+
return newBucket;
1496
}
1597
}

src/main/resources/day14.txt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
SFBBNKKOHHHPFOFFSPFV
2+
3+
HB -> C
4+
KO -> S
5+
KK -> N
6+
PF -> F
7+
VB -> F
8+
KC -> S
9+
BP -> H
10+
SS -> H
11+
BS -> B
12+
PB -> O
13+
VH -> C
14+
BK -> S
15+
BO -> F
16+
HN -> V
17+
NN -> K
18+
PV -> C
19+
NH -> P
20+
KP -> N
21+
NB -> V
22+
NF -> V
23+
PP -> O
24+
PN -> B
25+
VN -> K
26+
SC -> O
27+
NS -> N
28+
SV -> B
29+
BV -> P
30+
FV -> F
31+
OK -> H
32+
HF -> F
33+
CV -> K
34+
KB -> C
35+
OB -> B
36+
NO -> V
37+
OF -> B
38+
HP -> C
39+
BB -> F
40+
FB -> H
41+
OC -> K
42+
NV -> H
43+
OV -> S
44+
OP -> N
45+
SP -> N
46+
FK -> F
47+
VV -> B
48+
VK -> H
49+
OS -> F
50+
CO -> F
51+
CH -> V
52+
HV -> V
53+
FN -> B
54+
CS -> F
55+
PS -> F
56+
HS -> F
57+
VO -> K
58+
NP -> F
59+
FP -> B
60+
KF -> P
61+
CC -> N
62+
BF -> S
63+
VP -> F
64+
HO -> H
65+
FC -> F
66+
BH -> K
67+
NK -> S
68+
BN -> V
69+
SH -> K
70+
CP -> B
71+
VS -> K
72+
ON -> S
73+
FS -> P
74+
HK -> F
75+
PC -> O
76+
KN -> H
77+
CK -> N
78+
HH -> N
79+
CN -> S
80+
BC -> K
81+
PH -> N
82+
OO -> B
83+
FO -> O
84+
SK -> B
85+
FF -> V
86+
VC -> N
87+
SF -> N
88+
KH -> V
89+
SO -> F
90+
KS -> H
91+
SB -> K
92+
VF -> V
93+
PK -> O
94+
OH -> N
95+
HC -> F
96+
PO -> O
97+
NC -> F
98+
FH -> V
99+
KV -> V
100+
CB -> C
101+
CF -> O
102+
SN -> H

src/test/java/com/codingnagger/days/Day14Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public class Day14Test {
1515
public void partOne_shoudlYieldCorrectResult() {
1616
String result = DAY.partOne(INPUT);
1717

18-
assertThat(result).isEqualTo(null);
18+
assertThat(result).isEqualTo("1588");
1919
}
2020

2121
@Test
2222
public void partTwo_shoudlYieldCorrectResult() {
2323
String result = DAY.partTwo(INPUT);
2424

25-
assertThat(result).isEqualTo(null);
25+
assertThat(result).isEqualTo("2188189693529");
2626
}
2727
}

src/test/resources/day14.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
NNCB
2+
3+
CH -> B
4+
HH -> N
5+
CB -> H
6+
NH -> C
7+
HB -> C
8+
HC -> B
9+
HN -> C
10+
NN -> C
11+
BH -> H
12+
NC -> B
13+
NB -> B
14+
BN -> B
15+
BB -> N
16+
BC -> B
17+
CC -> N
18+
CN -> C

0 commit comments

Comments
 (0)