Skip to content

Commit

Permalink
2024 Day 01
Browse files Browse the repository at this point in the history
  • Loading branch information
fibsifan committed Dec 1, 2024
1 parent a15dbad commit c2aa229
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/de/jball/aoc2024/day01/Day01.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.jball.aoc2024.day01

import de.jball.AdventOfCodeDay
import kotlin.math.abs

class Day01(test: Boolean = false): AdventOfCodeDay<Long>(test, 11, 31) {
private val regex = Regex("(\\d+)\\s+(\\d+)")
private val lists = input.map { line ->
val groups = regex.matchEntire(line)!!.groups
Pair(groups[1]!!.value.toLong(), groups[2]!!.value.toLong())
}.unzip()

override fun part1(): Long {
return lists.first.sorted().zip(lists.second.sorted()) { a, b -> abs(a-b) }.sum()
}

override fun part2(): Long {
val lookup = lists.second.groupingBy { it }.eachCount()
return lists.first.sumOf {
it * (lookup[it] ?: 0)
}
}
}

fun main() {
Day01().run()
}
11 changes: 11 additions & 0 deletions src/test/kotlin/de/jball/aoc2024/Tests2024.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.jball.aoc2024

import de.jball.aoc2024.day01.Day01
import kotlin.test.Test

class Tests2024 {
@Test
fun day01() {
Day01(true).run()
}
}
6 changes: 6 additions & 0 deletions src/test/resources/2024/Day01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

0 comments on commit c2aa229

Please sign in to comment.