Skip to content

Commit b6c03d1

Browse files
committed
2022/Day 10
1 parent dd35752 commit b6c03d1

File tree

7 files changed

+212
-8
lines changed

7 files changed

+212
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
🎄 ---- Day 9: Rope Bridge ---
4343
🌟 Part 1: 6026 (28.7ms)
4444
🌟 Part 2: 2273 (22.2ms)
45+
46+
🎄 ---- Day 10: Cathode-Ray Tube ---
47+
🌟 Part 1: 12980 (12.51ms)
48+
🌟 Part 2: BRJLFULP (8.1us)
4549
```
4650

4751
## Visualizations

src/main/kotlin/me/grison/aoc/Asciis.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ val LETTERS_TO_FORM = mapOf(
1717
"L" to "# \n# \n# \n# \n# \n####",
1818
"O" to " ## \n# #\n# #\n# #\n# #\n ## ",
1919
"P" to "### \n# #\n### \n# \n# \n# ",
20+
"P2" to "### \n# #\n# #\n### \n# \n# ",
2021
"R" to "### \n# #\n# #\n### \n# #\n# #",
22+
"R2" to "### \n# #\n# #\n### \n# # \n# #",
2123
"S" to " ###\n# \n ## \n #\n #\n### ",
2224
"U" to "# #\n# #\n# #\n# #\n# #\n ## ",
2325
"Z" to "####\n #\n # \n # \n# \n####",
@@ -26,12 +28,12 @@ val LETTERS_TO_FORM = mapOf(
2628
val FORMS_TO_LETTER = LETTERS_TO_FORM.entries.associate { (k, v) -> v to k }
2729

2830

29-
fun String.ocrExtract(): String {
31+
fun String.ocrExtract(height: Int = 5): String {
3032
val display = replace("$CYAN#$RESET", "#")
3133
val cols = display.indexOf('\n')
32-
return (0 .. (cols / 5)).fold("") { acc, col ->
33-
val (start, end) = p(col * 5, col * 5 + 4)
34+
return (0 .. (cols / height)).fold("") { acc, col ->
35+
val (start, end) = p(col * height, col * height + (height-1))
3436
val form = display.allLines().map { it.substring(start, end) }.join("\n")
35-
acc + (FORMS_TO_LETTER[form] ?: "")
37+
acc + (FORMS_TO_LETTER[form] ?: "").replace("\\d".regex(), "")
3638
}
3739
}

src/main/kotlin/me/grison/aoc/Extensions.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fun gridPositions(dimensions: Pair<Int, Int>) = gridPositions(dimensions.first,
347347
fun gridPositions(height: Int, width: Int) = (0.until(height)).flatMap { y -> (0.until(width)).map { x -> p(y, x) } }
348348

349349

350-
fun Iterable<Pair<Long, Long>>.pointsDisplay(empty: String = " "): String {
350+
fun Iterable<Position>.pointsDisplay(empty: String = " "): String {
351351
val (maxX, maxY) = p(maxOf { it.first }, maxOf { it.second })
352352
val display = arrayListOf<List<String>>()
353353
for (y in 0..maxY) {
@@ -356,6 +356,15 @@ fun Iterable<Pair<Long, Long>>.pointsDisplay(empty: String = " "): String {
356356
return display.joinToString("\n") { it.joinToString("") }
357357
}
358358

359+
//fun Iterable<Pair<Long, Long>>.pointsDisplay(empty: String = " "): String {
360+
// val (maxX, maxY) = p(maxOf { it.first }, maxOf { it.second })
361+
// val display = arrayListOf<List<String>>()
362+
// for (y in 0..maxY) {
363+
// display.add((0..maxX).map { x -> if (p(x, y) in this) "$CYAN#$RESET" else empty })
364+
// }
365+
// return display.joinToString("\n") { it.joinToString("") }
366+
//}
367+
359368
fun Collection<Int>.toRange() = IntRange(this.first(), this.last())
360369
fun Collection<Long>.toRange() = LongRange(this.first(), this.last())
361370
fun Pair<Int, Int>.toRange() = IntRange(this.first, this.second)
@@ -392,4 +401,6 @@ fun Int.padLeft(length: Int, value: Char): String {
392401
return this.toString().padStart(length, value)
393402
}
394403

395-
fun Char.int() = this.toString().toInt()
404+
fun Char.int() = this.toString().toInt()
405+
406+
fun <T,U,V> t(t: T, u: U, v: V) = Triple(t, u, v)

src/main/kotlin/me/grison/aoc/y2021/Day13.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Day13 : Day(13, 2021) {
77

88
override fun partOne() = fold().first().size
99

10-
override fun partTwo() = fold().last().pointsDisplay().ocrExtract()
10+
override fun partTwo() = fold().last().map { p(it.first.toInt(), it.second.toInt()) }.pointsDisplay().ocrExtract()
1111

1212
private fun fold(): List<Set<Pair<Long, Long>>> {
1313
var coordinates = inputGroups[0].lines().map { it.allLongs().pair() }.toSet()
@@ -16,7 +16,7 @@ class Day13 : Day(13, 2021) {
1616
val steps = mutableListOf<Set<Pair<Long, Long>>>()
1717
foldInstructions.forEach { (axis, num) ->
1818
coordinates = coordinates.map { if (axis == "y") it.pivotSecond(num) else it.pivotFirst(num) }.toSet()
19-
steps.add(coordinates)
19+
steps.add(coordinates)
2020
}
2121

2222
return steps
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.grison.aoc.y2022
2+
3+
import arrow.syntax.function.memoize
4+
import me.grison.aoc.*
5+
import kotlin.math.abs
6+
7+
class Day10 : Day(10, 2022) {
8+
override fun title() = "Cathode-Ray Tube"
9+
10+
override fun partOne() = solve().first
11+
12+
override fun partTwo() = solve().second
13+
14+
private val interesting = listOf(20, 60, 100, 140, 180, 220)
15+
16+
private val solve = {
17+
var (cycles, x, sumStrength) = t(0, 1, 0)
18+
val crt = mutableListOf<Position>()
19+
fun signalStrength() = x * cycles.inc()
20+
21+
fun cycle() {
22+
if (cycles.inc() in interesting)
23+
sumStrength += signalStrength()
24+
25+
if (abs(cycles % 40 - x) < 2)
26+
crt.add(p(cycles % 40, cycles / 40))
27+
28+
cycles += 1
29+
}
30+
31+
inputList.forEach {
32+
when (it) {
33+
"noop" -> cycle()
34+
else -> {
35+
repeat(2) { cycle() }
36+
x += it.firstInt()
37+
}
38+
}
39+
}
40+
41+
p(sumStrength, crt.pointsDisplay().ocrExtract())
42+
}.memoize()
43+
}

src/main/resources/2022/10.txt

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
noop
2+
noop
3+
addx 5
4+
noop
5+
noop
6+
addx 6
7+
addx 4
8+
addx -4
9+
addx 4
10+
addx -6
11+
addx 11
12+
addx -1
13+
addx 2
14+
addx 4
15+
addx 3
16+
noop
17+
addx 2
18+
addx -30
19+
addx 2
20+
addx 33
21+
noop
22+
addx -37
23+
noop
24+
noop
25+
noop
26+
addx 3
27+
addx 2
28+
addx 5
29+
addx 20
30+
addx 7
31+
addx -24
32+
addx 2
33+
noop
34+
addx 7
35+
addx -2
36+
addx -6
37+
addx 13
38+
addx 3
39+
addx -2
40+
addx 2
41+
noop
42+
addx -5
43+
addx 10
44+
addx 5
45+
addx -39
46+
addx 1
47+
addx 5
48+
noop
49+
addx 3
50+
noop
51+
addx -5
52+
addx 10
53+
addx -2
54+
addx 2
55+
noop
56+
noop
57+
addx 7
58+
noop
59+
noop
60+
noop
61+
noop
62+
addx 3
63+
noop
64+
addx 3
65+
addx 2
66+
addx 8
67+
addx -1
68+
addx -20
69+
addx 21
70+
addx -38
71+
addx 5
72+
addx 2
73+
noop
74+
noop
75+
noop
76+
addx 8
77+
noop
78+
noop
79+
addx -2
80+
addx 2
81+
addx -7
82+
addx 14
83+
addx 5
84+
noop
85+
noop
86+
noop
87+
addx -16
88+
addx 17
89+
addx 2
90+
addx -12
91+
addx 19
92+
noop
93+
noop
94+
addx -37
95+
noop
96+
noop
97+
noop
98+
addx 3
99+
addx 2
100+
addx 2
101+
addx 5
102+
addx 20
103+
addx -19
104+
addx 2
105+
noop
106+
noop
107+
noop
108+
addx 5
109+
addx 19
110+
addx -12
111+
addx 3
112+
addx -2
113+
addx 2
114+
addx -18
115+
addx 25
116+
addx -14
117+
addx -22
118+
addx 1
119+
noop
120+
noop
121+
noop
122+
addx 3
123+
addx 5
124+
addx -4
125+
addx 7
126+
addx 4
127+
noop
128+
addx 1
129+
noop
130+
noop
131+
addx 2
132+
addx -6
133+
addx 15
134+
addx -1
135+
addx 4
136+
noop
137+
noop
138+
addx 1
139+
addx 4
140+
addx -33
141+
noop
142+
addx 21
143+
noop

src/test/kotlin/me/grison/aoc/y2022/AllDaysTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class AllDaysTest {
2020
Answer({ Day07() }, 1582412, 3696336),
2121
Answer({ Day08() }, 1763, 671160),
2222
Answer({ Day09() }, 6026, 2273),
23+
Answer({ Day10() }, 12980, "BRJLFULP"),
2324
).map {
2425
val day = it.inst.invoke()
2526
DynamicTest.dynamicTest("Day ${day.year}/${day.dayNumber} - Part 1 - expecting ${it.part1}") {

0 commit comments

Comments
 (0)