Skip to content

Commit 221e852

Browse files
committed
finish day 15 and prepare 16
1 parent 4748145 commit 221e852

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import tr.emreone.kotlin_utils.automation.Day
4+
5+
class Day15 : Day(15, 2023, "Lens Library") {
6+
7+
class Lens(val label: String, val focalLength: Int? = null) {
8+
9+
override fun hashCode(): Int {
10+
var hash = 0
11+
12+
this.label.forEach {
13+
hash += it.code
14+
hash *= 17
15+
hash = hash.mod(256)
16+
}
17+
18+
return hash
19+
}
20+
}
21+
22+
override fun part1(): Long {
23+
return inputAsString
24+
.replace("\\s+".toRegex(), "")
25+
.split(",")
26+
.sumOf {
27+
Lens(it, null).hashCode().toLong()
28+
}
29+
}
30+
31+
override fun part2(): Long {
32+
val boxes = mutableMapOf<Int, MutableList<Lens>>()
33+
34+
inputAsString
35+
.replace("\\s+".toRegex(), "")
36+
.split(",")
37+
.forEach {
38+
val index = it.indexOfAny(listOf("=", "-"), 0)
39+
val operation = it.get(index)
40+
val (label, focalLength) = it.split(operation)
41+
42+
val lens = Lens(label, focalLength.takeIf { it.isNotEmpty() }?.toInt())
43+
when (operation) {
44+
'-' -> {
45+
boxes.get(lens.hashCode())
46+
?.removeAll { l ->
47+
l.label == label
48+
}
49+
}
50+
51+
'=' -> {
52+
val box = boxes.getOrDefault(lens.hashCode(), mutableListOf())
53+
54+
val i = box.indexOfFirst { it.label == label }
55+
if (i >= 0) {
56+
box.removeAt(i)
57+
box.add(i, lens)
58+
}
59+
else {
60+
box.add(lens)
61+
}
62+
63+
boxes[lens.hashCode()] = box
64+
}
65+
}
66+
}
67+
68+
return boxes.filterValues { it.isNotEmpty() }
69+
.entries
70+
.sumOf { (indexBox, box) ->
71+
box.mapIndexed { indexLens, lens ->
72+
(indexBox + 1) * (indexLens + 1) * lens.focalLength!!.toLong()
73+
}.sum()
74+
}
75+
}
76+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import tr.emreone.kotlin_utils.automation.Day
4+
5+
class Day16: Day(16, 2023, "The Floor Will Be Lava") {
6+
7+
8+
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import org.junit.jupiter.api.Test
4+
import tr.emreone.kotlin_utils.Resources
5+
import tr.emreone.kotlin_utils.automation.solve
6+
7+
internal class Day15Test {
8+
9+
@Test
10+
fun `execute_tests`() {
11+
solve<Day15>(false) {
12+
Resources.resourceAsList("day15_example.txt")
13+
.joinToString("\n") part1 1320 part2 145
14+
}
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tr.emreone.adventofcode.days
2+
3+
import org.junit.jupiter.api.Test
4+
import tr.emreone.kotlin_utils.Resources
5+
import tr.emreone.kotlin_utils.automation.solve
6+
7+
internal class Day16Test {
8+
9+
@Test
10+
fun `execute_tests`() {
11+
solve<Day16>(false) {
12+
Resources.resourceAsList("day16_example.txt")
13+
.joinToString("\n") part1 46
14+
}
15+
}
16+
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.|...\....
2+
|.-.\.....
3+
.....|-...
4+
........|.
5+
..........
6+
.........\
7+
..../.\\..
8+
.-.-/..|..
9+
.|....-|.\
10+
..//.|....

0 commit comments

Comments
 (0)