generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |