generated from andilau/advent-of-code-starter
-
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.
* Day 10: Hoof It (WIP) * Day 10: Hoof It
- Loading branch information
Showing
5 changed files
with
227 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
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,58 @@ | ||
package days | ||
|
||
import java.util.PriorityQueue | ||
|
||
@AdventOfCodePuzzle( | ||
name = "Hoof It", | ||
url = "https://adventofcode.com/2024/day/10", | ||
date = Date(day = 10, year = 2024) | ||
) | ||
class Day10(val input: List<String>) : Puzzle { | ||
|
||
val map: Map<Point, Int> = findAllIgnoring().toMap() | ||
val starts: Set<Point> = map.filter { (_, v) -> v == 0 }.keys | ||
val exits: Set<Point> = map.filter { (_, v) -> v == 9 }.keys | ||
|
||
override fun partOne() = starts.sumOf { start -> scoreTrail(start, exits) } | ||
|
||
override fun partTwo() = starts.sumOf { start -> scoreTrail2(start, exits) } | ||
|
||
private fun findAllIgnoring(): List<Pair<Point, Int>> = input | ||
.flatMapIndexed { y, line -> line.mapIndexedNotNull() { x, c -> if (c.isDigit()) Point(x, y) to c.digitToInt() else null } } | ||
|
||
private fun scoreTrail(start: Point, exits: Set<Point>): Int { | ||
var reached = mutableSetOf<Point>() | ||
val queue = PriorityQueue<Pair<Point, Int>> { a, b -> a.second.compareTo(b.second) } | ||
queue.add(start to 0) | ||
val seen = mutableSetOf<Point>() | ||
|
||
while (queue.isNotEmpty()) { | ||
val (next, cost) = queue.poll() | ||
|
||
if (next in seen) continue | ||
seen += next | ||
if (next in exits) reached += next | ||
next.neighbors() | ||
.filter { it in map } | ||
.filter { map[it]!! - map[next]!! == 1 } | ||
.forEach { queue.add(it to cost + 1) } | ||
} | ||
return reached.size | ||
} | ||
|
||
private fun scoreTrail2(start: Point, exits: Set<Point>): Int { | ||
var reached = mutableListOf<Point>() | ||
val queue = PriorityQueue<List<Point>> { a, b -> a.size.compareTo(b.size) } | ||
queue.add(listOf(start)) | ||
while (queue.isNotEmpty()) { | ||
val path = queue.poll() | ||
|
||
if (path.last() in exits) reached += path.last() | ||
path.last().neighbors() | ||
.filter { it in map } | ||
.filter { map[it]!! - map[path.last()]!! == 1 } | ||
.forEach { queue.add(path + it) } | ||
} | ||
return reached.size | ||
} | ||
} |
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,43 @@ | ||
1089890105678121432132101232127321012354321 | ||
0178763234589030543045000345018018985467610 | ||
3298954103490347656106215496789123876508998 | ||
4567832234321258985287306784100034565012567 | ||
2356541025652767014396454993211239654523498 | ||
1403456710787854320478567884589748703012367 | ||
0512109876598941231569545675679856412721456 | ||
7657238102345410542349830901298764567810565 | ||
8998341201986789621056721850347543201998776 | ||
7087650345679878700876545765456670102367985 | ||
6121065436901263210980238750105985434451234 | ||
5432376327874354890121129653234356321010105 | ||
2345981210965568765430098544234067865430096 | ||
1296590210567809012321107230165123956721187 | ||
0187645323456918456789236109877034847810256 | ||
0099236012567823565018045234578765432901340 | ||
1678107101998894654327102198679896001076541 | ||
2363218900806765785210231087988587123189832 | ||
1454300210712567892104345896107698894898321 | ||
0510321345643498763569856745234565765765410 | ||
9621410478761098954478765030109874327656731 | ||
8734589569454167410349034121918969018549843 | ||
9435678400123256301256121030876878129450652 | ||
4521017312321343214787036980125561034321781 | ||
5670901205430456345698347898234432211289690 | ||
4989874396012387210510256723478934300478541 | ||
3090765487801091234423105410560125410367630 | ||
2101051234945670542394576321021076523458921 | ||
1672340545234987691087689987688987984361010 | ||
0589655676101296789079012896590868970154301 | ||
3438764985054385674108543003481078561567210 | ||
0127123078765676543287632112872369432018923 | ||
6546042129654778904398910001965454342129854 | ||
7237653434569865215897601223450569243036765 | ||
8198548521010764346710510319321678154545678 | ||
9011239630781453454323423408765321069694789 | ||
0100348749692354320345014503455430678783210 | ||
3217654458543765211276769612786726789698701 | ||
8348983267012891200989838764691810652189610 | ||
9456678101101010341234542123500901643076521 | ||
8764569434514567650765430054411898701103430 | ||
7623430127603498769876121069322345632212341 | ||
6510121098012389858789012378443454543303454 |
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
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,124 @@ | ||
package days | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.DynamicTest | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestFactory | ||
|
||
@DisplayName("Day 10") | ||
class Day10Test { | ||
val trailheadScore1 = """ | ||
0123 | ||
1234 | ||
8765 | ||
9876""".trimIndent().lines() | ||
|
||
val trailheadScore2 = """ | ||
...0... | ||
...1... | ||
...2... | ||
6543456 | ||
7.....7 | ||
8.....8 | ||
9.....9""".trimIndent().lines() | ||
val trailheadScore3 = """ | ||
..90..9 | ||
...1.98 | ||
...2..7 | ||
6543456 | ||
765.987 | ||
876.... | ||
987....""".trimIndent().lines() | ||
val trailheadScore4 = """ | ||
10..9.. | ||
2...8.. | ||
3...7.. | ||
4567654 | ||
...8..3 | ||
...9..2 | ||
.....01""".trimIndent().lines() | ||
val trailheadScore5 = """ | ||
89010123 | ||
78121874 | ||
87430965 | ||
96549874 | ||
45678903 | ||
32019012 | ||
01329801 | ||
10456732""".trimIndent().lines() | ||
|
||
@Nested | ||
@DisplayName("Part 1") | ||
inner class Part1 { | ||
|
||
@TestFactory | ||
fun `What is the sum of the scores of all trailheads on your topographic map`() = | ||
listOf( | ||
trailheadScore1 to 1, | ||
trailheadScore2 to 2, | ||
trailheadScore3 to 4, | ||
trailheadScore4 to 3, | ||
trailheadScore5 to 36, | ||
).map { (map, score) -> | ||
DynamicTest.dynamicTest("The sum of the scores of all trailheads on the topographic map is $score") { | ||
assertThat(Day10(map).partOne()).isEqualTo(score) | ||
} | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@DisplayName("Part 2") | ||
inner class Part2 { | ||
|
||
val trailheadScore6 = """ | ||
.....0. | ||
..4321. | ||
..5..2. | ||
..6543. | ||
..7..4. | ||
..8765. | ||
..9....""".trimIndent().lines() | ||
|
||
val trailheadScore7 = """ | ||
..90..9 | ||
...1.98 | ||
...2..7 | ||
6543456 | ||
765.987 | ||
876.... | ||
987....""".trimIndent().lines() | ||
|
||
val trailheadScore8 = """ | ||
012345 | ||
123456 | ||
234567 | ||
345678 | ||
4.6789 | ||
56789.""".trimIndent().lines() | ||
val trailheadScore9 = """ | ||
89010123 | ||
78121874 | ||
87430965 | ||
96549874 | ||
45678903 | ||
32019012 | ||
01329801 | ||
10456732""".trimIndent().lines() | ||
|
||
@TestFactory | ||
fun `What is the sum of the scores of all trailheads on your topographic map`() = | ||
listOf( | ||
trailheadScore6 to 3, | ||
trailheadScore7 to 13, | ||
trailheadScore8 to 227, | ||
trailheadScore9 to 81, | ||
).map { (map, score) -> | ||
DynamicTest.dynamicTest("The sum of the scores of all trailheads on the topographic map is $score") { | ||
assertThat(Day10(map).partTwo()).isEqualTo(score) | ||
} | ||
} | ||
} | ||
} |