Skip to content

Commit

Permalink
2024 Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 2, 2024
1 parent f768859 commit 66c054c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/main/kotlin/no/rodland/advent_2024/Day02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package no.rodland.advent_2024

import no.rodland.advent.Day

// template generated: 02/12/2024
// Fredrik Rødland 2024

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

private val parsed = input.parse()

override fun partOne(): Long {


return parsed.count { it.isSafe() }.toLong()
}

override fun partTwo(): Long {
val expanded = parsed.expand()
return expanded.count { candidates ->
candidates.any { list -> list.isSafe() }
}.toLong()
}

override fun List<String>.parse(): List<List<Int>> {
return map { line ->
line.split(" ").map { it.toInt() }
}
}

private fun List<Int>.isSafe(): Boolean {
val inc = this[1] > this[0]
val diff = windowed(2).map {
if (inc) {
it.last() - it.first()
} else {
it.first() - it.last()
}
}
val all = diff.all { it in (1..3) }
return all
}

private fun List<List<Int>>.expand(): List<List<List<Int>>> {
return map { org ->
List(org.size) { index -> org.withoutItemAt(index) }
}
}

private fun List<Int>.withoutItemAt(index: Int) = filterIndexed { i, _ -> i != index }

override val day = "02".toInt()
}



82 changes: 82 additions & 0 deletions src/test/kotlin/no/rodland/advent_2024/Day02Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package no.rodland.advent_2024

import no.rodland.advent.*
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import readFile

//
// run: download_aoc_input.sh to download input
//

@Suppress("ClassName")
@DisableSlow
internal class Day02Test {
private val data02 = "2024/input_02.txt".readFile()
private val test02 = "2024/input_02_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 4L
private val resultOne = 680L
private val resultTwo = 710L

val test = defaultTestSuiteParseOnInit(
Day02(data02),
Day02(test02),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day02(data02) },
{ Day02(test02) },
)

@Nested
inner class Init {
@Test
fun `02,-,example,1`() {
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "02".toInt(), Part.TWO, false, "example"))
}

@Test
fun `02,-,example,2`() {
report(test.initTest.copy())
}

@Test
fun `02,-,test,init`() {
report(test.initTest)
}

@Test
fun `02,-,live,init`() {
report(test.initLive)
}
}

@Nested
inner class `Part 1` {
@Test
fun `02,1,test`() {
report(test.testPart1)
}

@Test
fun `02,1,live,1`() {
report(test.livePart1)
}
}

@Nested
inner class `Part 2` {
@Test
fun `02,2,test`() {
report(test.testPart2)
}

@Test
fun `02,2,live,1`() {
report(test.livePart2)
}
}
}
6 changes: 6 additions & 0 deletions src/test/resources/2024/input_02_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

0 comments on commit 66c054c

Please sign in to comment.