Skip to content

Commit 2375b16

Browse files
authored
Day 25 2024 (#289)
1 parent 431aa4f commit 2375b16

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

src/main/kotlin/me/peckb/aoc/_2024/calendar/day24/Day24.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,4 @@ class Day24 @Inject constructor(
280280

281281
data class Wiring(val a: String, val op: String, val b: String, val result: String) {
282282
fun input() = setOf(a, b)
283-
}
283+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.peckb.aoc._2024.calendar.day25
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+
class Day25 @Inject constructor(
7+
private val generatorFactory: InputGeneratorFactory,
8+
) {
9+
fun partOne(filename: String) = generatorFactory.forFile(filename).read { input ->
10+
val keys = mutableListOf<Key>()
11+
val locks = mutableListOf<Lock>()
12+
// The locks are schematics that have the top row filled (#)
13+
// the keys have the top row empty and the bottom row filled.
14+
input.chunked(8).forEach search@ { image ->
15+
var a = 0
16+
var b = 0
17+
var c = 0
18+
var d = 0
19+
var e = 0
20+
var lock = false
21+
22+
val searchRange = if (image[0] == "#####") { lock = true; 1 .. 5 } else { 5 downTo 1 }
23+
searchRange.forEach { i ->
24+
if (image[i][0] == '#') { a++ }
25+
if (image[i][1] == '#') { b++ }
26+
if (image[i][2] == '#') { c++ }
27+
if (image[i][3] == '#') { d++ }
28+
if (image[i][4] == '#') { e++ }
29+
}
30+
31+
if (lock) { locks.add(Lock(a, b, c, d, e)) } else { keys.add(Key(a, b, c, d, e)) }
32+
}
33+
34+
keys.sumOf { key ->
35+
val (ka, kb, kc, kd, ke) = key
36+
37+
locks.count { lock ->
38+
val (la, lb, lc, ld, le) = lock
39+
40+
listOf(ka + la, kb + lb, kc + lc, kd + ld, ke + le).all { it <= 5 }
41+
}
42+
}
43+
}
44+
}
45+
46+
data class Key(val a: Int, val b: Int, val c: Int, val d: Int,val e: Int)
47+
48+
data class Lock(val a: Int, val b: Int, val c: Int, val d: Int,val e: Int)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 25: Code Chronicle](https://adventofcode.com/2024/day/25)

src/test/kotlin/me/peckb/aoc/_2024/TestDayComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import me.peckb.aoc._2024.calendar.day21.Day21Test
2424
import me.peckb.aoc._2024.calendar.day22.Day22Test
2525
import me.peckb.aoc._2024.calendar.day23.Day23Test
2626
import me.peckb.aoc._2024.calendar.day24.Day24Test
27+
import me.peckb.aoc._2024.calendar.day25.Day25Test
2728
import javax.inject.Singleton
2829
import me.peckb.aoc.DayComponent
2930
import me.peckb.aoc.InputModule
@@ -56,4 +57,5 @@ internal interface TestDayComponent : DayComponent {
5657
fun inject(day22Test: Day22Test)
5758
fun inject(day23Test: Day23Test)
5859
fun inject(day24Test: Day24Test)
60+
fun inject(day25Test: Day25Test)
5961
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.peckb.aoc._2024.calendar.day25
2+
3+
import javax.inject.Inject
4+
5+
import me.peckb.aoc._2024.DaggerTestDayComponent
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.BeforeEach
8+
import org.junit.jupiter.api.Test
9+
10+
internal class Day25Test {
11+
@Inject
12+
lateinit var day25: Day25
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay25PartOne() {
21+
assertEquals(3619, day25.partOne(DAY_25))
22+
}
23+
24+
companion object {
25+
private const val DAY_25: String = "advent-of-code-input/2024/day25.input"
26+
}
27+
}

0 commit comments

Comments
 (0)