Skip to content

Commit 0ea69e1

Browse files
committed
day 14, part 1 solution
1 parent a99c3c3 commit 0ea69e1

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day14/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("org.adventofcode.kotlin-application-conventions")
3+
}
4+
5+
dependencies {
6+
7+
}
8+
9+
application {
10+
mainClass.set("org.adventofcode.AppKt")
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.adventofcode
2+
3+
import java.io.File
4+
5+
val RULE_REGEX_PATTERN = """([\w]+) -> ([\w])""".toRegex()
6+
7+
fun runSolutionPart1(template: String, rules: Map<String, String>) {
8+
println("Day 14 Solution: Part 1 (Basic Implementation)")
9+
val steps = 10
10+
val polymer = (0 until steps).fold(template) { polymer, _ ->
11+
polymer
12+
.windowedSequence(2)
13+
.mapNotNull { rules[it]?.let { elem -> "${it[0]}$elem${it[1]}" } }
14+
.mapIndexed { idx, chunk -> if (idx == 0) chunk else chunk.substring(1) }
15+
.joinToString("")
16+
}
17+
val charCounts = polymer.asSequence().fold(mapOf<Char, Int>()) { map, char ->
18+
map + (char to map.getOrDefault(char, 0) + 1)
19+
}
20+
val mostCommonCharCount = charCounts.maxOf { (_, count) -> count }
21+
val leastCommonCharCount = charCounts.minOf { (_, count) -> count }
22+
val score = mostCommonCharCount - leastCommonCharCount
23+
24+
println("Most common char count: $mostCommonCharCount")
25+
println("Least common char count: $leastCommonCharCount")
26+
println("Score: $score")
27+
}
28+
29+
fun main() {
30+
val input = File("day14/src/main/resources/puzzleInput.txt").readLines()
31+
val template = input.first()
32+
val rules = input.drop(2).mapNotNull {
33+
val result = RULE_REGEX_PATTERN.matchEntire(it)
34+
result?.groupValues?.let { (_, fromPair, toChar) -> fromPair to toChar }
35+
}.toMap()
36+
37+
runSolutionPart1(template, rules)
38+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
PBVHVOCOCFFNBCNCCBHK
2+
3+
FV -> C
4+
SS -> B
5+
SC -> B
6+
BP -> K
7+
VP -> S
8+
HK -> K
9+
FS -> F
10+
CC -> V
11+
VB -> P
12+
OP -> B
13+
FO -> N
14+
FH -> O
15+
VK -> N
16+
PV -> S
17+
HV -> O
18+
PF -> F
19+
HH -> F
20+
NK -> S
21+
NC -> S
22+
FC -> H
23+
FK -> K
24+
OO -> N
25+
HP -> C
26+
NN -> H
27+
BB -> H
28+
CN -> P
29+
PS -> N
30+
VF -> S
31+
CB -> B
32+
OH -> S
33+
CF -> C
34+
OK -> P
35+
CV -> V
36+
CS -> H
37+
KN -> B
38+
OV -> S
39+
HB -> C
40+
OS -> V
41+
PC -> B
42+
CK -> S
43+
PP -> K
44+
SN -> O
45+
VV -> C
46+
NS -> F
47+
PN -> K
48+
HS -> P
49+
VO -> B
50+
VC -> B
51+
NV -> P
52+
VS -> N
53+
FP -> F
54+
HO -> S
55+
KS -> O
56+
BN -> F
57+
VN -> P
58+
OC -> K
59+
SF -> P
60+
PO -> P
61+
SB -> O
62+
FN -> F
63+
OF -> F
64+
CP -> C
65+
HC -> O
66+
PH -> O
67+
BC -> O
68+
NO -> C
69+
BH -> C
70+
VH -> S
71+
KK -> O
72+
SV -> K
73+
KB -> K
74+
BS -> S
75+
HF -> B
76+
NH -> S
77+
PB -> N
78+
HN -> K
79+
SK -> B
80+
FB -> F
81+
KV -> S
82+
BF -> S
83+
ON -> S
84+
BV -> P
85+
KC -> S
86+
NB -> S
87+
NP -> B
88+
BK -> K
89+
NF -> C
90+
BO -> K
91+
KF -> B
92+
KH -> N
93+
SP -> O
94+
CO -> S
95+
KO -> V
96+
SO -> B
97+
CH -> C
98+
KP -> C
99+
FF -> K
100+
PK -> F
101+
OB -> H
102+
SH -> C
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

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ include(
2222
"day11",
2323
"day12",
2424
"day13",
25+
"day14",
2526
)

0 commit comments

Comments
 (0)