Skip to content

Commit

Permalink
2024 - Day 22 - part 1&2 TODO: clearify part2 - ensure working on seq…
Browse files Browse the repository at this point in the history
…uences as long as possible.
  • Loading branch information
fmmr committed Dec 22, 2024
1 parent 9fca1e7 commit 7c6346f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
35 changes: 30 additions & 5 deletions src/main/kotlin/no/rodland/advent_2024/Day22.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,45 @@ import no.rodland.advent.Day
// Fredrik Rødland 2024





class Day22(val input: List<String>) : Day<Long, Long, List<Long>> {

override fun partOne(): Long {
return input.parse().map { input -> generateSequence(input) { it.calc() }.take(2001) }.sumOf(Sequence<Long>::last)
return orgSequence().sumOf(Sequence<Long>::last)

}

override fun partTwo(): Long {
return 2

// XXX F TODO re-write

val map = orgSequence()
.map { it.map { n -> n % 10 } }
.map { seq ->
seq
.zipWithNext { a, b -> b to b - a }
.windowed(4) { list -> list.map { it.second } to list.last().first }

}
val prices = map.map {
it.groupingBy { (key, value) -> key }
.fold({ _, value -> value.second }, { _, a, _ -> a })
}

return prices.flatMap { it.keys }.toSet().maxOf { key ->
prices.sumOf { it[key] ?: 0 }
}


}

// private fun part2Seq(seq: Sequence<Long>) = seq.map { it % 10 }
// .zipWithNext { a, b -> b to b - a }
// .windowed(4) { (a, b, c, d) -> listOf(a.second, b.second, c.second, d.second) to d.first }
// .groupingBy { (key, value) -> key }
// .fold({ _, value -> value.second }, { _, a, _ -> a })

private fun orgSequence() = input.parse().map { input -> generateSequence(input) { it.calc() }.take(2001) }

private fun Long.mix(secret: Long) = this xor secret
private fun Long.prune() = this % 16777216L
private fun Long.step(calc: (Long) -> Long) = calc(this).mix(this).prune()
Expand Down
30 changes: 17 additions & 13 deletions src/test/kotlin/no/rodland/advent_2024/Day22Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ import readFile
internal class Day22Test {
private val data22 = "2024/input_22.txt".readFile()
private val test22 = "2024/input_22_test.txt".readFile()
private val test22Part2 = "2024/input_22_test_part2.txt".readFile()

private val resultTestOne = 37327623L
private val resultTestTwo = 2L
private val resultTestTwo = 23L
private val resultOne = 14082561342L
private val resultTwo = 2L
private val resultTwo = 1568L

val test = defaultTestSuiteParseOnInit(
Day22(data22),
Day22(test22),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day22(data22) },
{ Day22(test22) },
numTestPart1 = 3
)
val test = run {
val liveDay = Day22(data22)
val testDay = Day22(test22)
val testDayPart2 = Day22(test22Part2)
AOCTestSuite<Any?, Unit, Unit>(
AOCTest({ liveDay.partOne() }, Unit, resultOne, numTests = 3, day = liveDay.day, part = Part.ONE, live = true),
AOCTest({ liveDay.partTwo() }, Unit, resultTwo, 1, liveDay.day, Part.TWO, true),
AOCTest({ testDay.partOne() }, Unit, resultTestOne, 1, liveDay.day, Part.ONE, false),
AOCTest({ testDayPart2.partTwo() }, Unit, resultTestTwo, 1, liveDay.day, Part.TWO, false),
AOCTest<Unit, Unit>({ Day22(data22) }, Unit, Unit, 100, liveDay.day, Part.INIT, live = true),
AOCTest<Unit, Unit>({ Day22(test22) }, Unit, Unit, 100, liveDay.day, Part.INIT, live = false),
)
}

@Nested
inner class Init {
Expand Down Expand Up @@ -76,6 +79,7 @@ internal class Day22Test {
}

@Test
@Slow(14000)
fun `22,2,live,1`() {
report(test.livePart2)
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/2024/input_22_test_part2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
2024

0 comments on commit 7c6346f

Please sign in to comment.