From 2e18735d4492e00b9d4a37675dd4fc23c9e00f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Thu, 21 Dec 2023 08:56:53 +0100 Subject: [PATCH] 2023 - Day 21 - init --- src/main/kotlin/no/rodland/advent/Misc.kt | 18 +++ .../kotlin/no/rodland/advent_2023/Day21.kt | 27 ++++ .../no/rodland/advent_2023/Day21Test.kt | 82 +++++++++++ src/test/resources/2023/input_21.txt | 131 ++++++++++++++++++ src/test/resources/2023/input_21_test.txt | 11 ++ 5 files changed, 269 insertions(+) create mode 100644 src/main/kotlin/no/rodland/advent_2023/Day21.kt create mode 100644 src/test/kotlin/no/rodland/advent_2023/Day21Test.kt create mode 100644 src/test/resources/2023/input_21_test.txt diff --git a/src/main/kotlin/no/rodland/advent/Misc.kt b/src/main/kotlin/no/rodland/advent/Misc.kt index 4b9d6205..c2fb6d0e 100644 --- a/src/main/kotlin/no/rodland/advent/Misc.kt +++ b/src/main/kotlin/no/rodland/advent/Misc.kt @@ -21,6 +21,24 @@ fun String.readFileAsString(): String { } } +// https://kotlinlang.slack.com/archives/C87V9MQFK/p1703069274599999?thread_ts=1703048402.894019&cid=C87V9MQFK +//fun chineseRemainder(values: List>): Long { +// if (values.isEmpty()) { +// return 0L +// } +// var result = values[0].first +// var lcm = values[0].second +// for (i in 1 until values.size) { +// val (base, modulo) = values[i] +// val target = base % modulo +// while (result % modulo != target) { +// result += lcm +// } +// lcm = lcm(lcm, modulo) +// } +// return result +//} + fun lcm(n1: BigInteger, n2: BigInteger): BigInteger { // https://no.wikipedia.org/wiki/Minste_felles_multiplum // lcm = (n1 * n2) / gcd diff --git a/src/main/kotlin/no/rodland/advent_2023/Day21.kt b/src/main/kotlin/no/rodland/advent_2023/Day21.kt new file mode 100644 index 00000000..66ee0d97 --- /dev/null +++ b/src/main/kotlin/no/rodland/advent_2023/Day21.kt @@ -0,0 +1,27 @@ +package no.rodland.advent_2023 + +import no.rodland.advent.Day + +// template generated: 21/12/2023 +// Fredrik Rødland 2023 + +class Day21(val input: List) : Day> { + + private val parsed = input.parse() + + override fun partOne(): Long { + return 2 + } + + override fun partTwo(): Long { + return 2 + } + + override fun List.parse(): List { + return map { line -> + line + } + } + + override val day = "21".toInt() +} diff --git a/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt b/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt new file mode 100644 index 00000000..0726edde --- /dev/null +++ b/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt @@ -0,0 +1,82 @@ +package no.rodland.advent_2023 + +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 Day21Test { + private val data21 = "2023/input_21.txt".readFile() + private val test21 = "2023/input_21_test.txt".readFile() + + private val resultTestOne = 2L + private val resultTestTwo = 2L + private val resultOne = 2L + private val resultTwo = 2L + + val test = defaultTestSuiteParseOnInit( + Day21(data21), + Day21(test21), + resultTestOne, + resultOne, + resultTestTwo, + resultTwo, + { Day21(data21) }, + { Day21(test21) }, + ) + + @Nested + inner class Init { + @Test + fun `21,-,example,1`() { + report(AOCTest({ "123".toInt() }, Unit, 123, 5, "21".toInt(), Part.TWO, false, "example")) + } + + @Test + fun `21,-,example,2`() { + report(test.initTest.copy()) + } + + @Test + fun `21,-,test,init`() { + report(test.initTest) + } + + @Test + fun `21,-,live,init`() { + report(test.initLive) + } + } + + @Nested + inner class `Part 1` { + @Test + fun `21,1,test`() { + report(test.testPart1) + } + + @Test + fun `21,1,live,1`() { + report(test.livePart1) + } + } + + @Nested + inner class `Part 2` { + @Test + fun `21,2,test`() { + report(test.testPart2) + } + + @Test + fun `21,2,live,1`() { + report(test.livePart2) + } + } +} diff --git a/src/test/resources/2023/input_21.txt b/src/test/resources/2023/input_21.txt index e69de29b..479e5776 100644 --- a/src/test/resources/2023/input_21.txt +++ b/src/test/resources/2023/input_21.txt @@ -0,0 +1,131 @@ +................................................................................................................................... +.#.................#....#...........#..........#...#.#....#.........................#...........................#......#........... +....#..............##............#.................#...................#.#.....#..##......#...........#...#............#.....##.... +....##..##..........#...........#....#......#.......#.........................#.......#.................#.....................#..#. +.......##....###...#.#......###.#............#..#...............................#......#.........#..#.....#........................ +.......###......#..#........................##....#.##..........#................................#....##.........#..........#...... +......#.....#..#.............#.............#...#................#...............#.....#.#......#..#.#................#..#.#.#.#.... +.......#...#...........................#..#........................#..............#..#....#.#...#..#.............##.........#...... +....#.#....#........................#........#.......#.........#.........................................#....#.................... +..........................##..###.#....#...##..#...............#...............#...#...#.#.........#....................#.......#.. +.....#.......#.....##....#.....................................#..#...............##.....##.#..............#.........#..#.....#.... +...#..#......................#....#....#...#.#...............#......#.....................#.................#.##...#............... +.........#........#........#......................................................................................#.....#.......... +..............#.......#..#.#...##..........................#......#.#............................#.#.....#....................#.... +.....#.#......#.............##...#......#...........................................#...................#..........#.#..#........#. +...##...#...............................#....................#..#.....................##.......#....#.....#..#................#.... +.#..#..............#...#......#..#......................#....#......##..#...............#.....#......#......##...#......#.......... +.................#.................##..........................#...#....#.............#..........##............#.....#............. +...........#...#..........................#...........................#.................#..#..#...#.....#........#....#....#.#..... +.............#.......#................#............................#........................#........#............#..#........#.... +...#....................#.........##..........................#.............................#.....#..........#.#................... +.................#............#.......................##...............#.........#.................#......#........#...#........... +............#......#...#....#.......................#...#....#...............#..............#.....#.........###.....#..........#... +.......#.........#....#....##......#.#.....................#..........##......##...............#...............##......#........... +.............#........#........................#...#.....#.................#....#........................#......#.................. +..........#.......#......#...................##........#.....#.....#.....#....................#...#.............#........#......... +...................##....#....................#.......#..#........#.#....#..#.......#.#....................#.#.#.....#.........##.. +.............#......#..#.#...................#....................#..........#..#................#..........#...###................ +...#........#..#....#............................#.....##......................#.........#.........#........#...#.#................ +...#.#......#......#...#.................#......#.#....................#...............................##..............#........#.. +.............#......#....#..........................................##......##....#.....................#..............#.....#..... +.#...........................#............#....##..#..............#.#........#..#.........#........................#...#........... +.........#.......#.#..#...#.....................#..#.....##.......#.........#...#....#....................#.##....#.#........#..... +.......................................#.........#.....................#........#.....#........................#................##. +....###.....#...........................#.........#.#.........................#....#......#..................##........#..#........ +.........................................#.............#.................................#.#..#..............................#..... +.#..#........#.........................##...#...##.......#..#.....#.........#.......#.....#.....................#...#......#..##... +..........#......#...............#...#..................#......#.....#........#.#...........#....#.............................#... +............#....#...............#.................#............................#...........#...#.................................. +....#.##.........#..#.................#.....#.##..........#.#.........#.....#..#...#........#..#..#...........................#.... +......#..........................#.....#..#.................###..........#.......#.................#..............#.........##.#... +.........#.#.#...............#.#....#.#............#..........................#.....................#.............#..........#..#.. +...............#.........................#...............#........#..#.........#.......................#........................#.. +.....#...##...#...................#...#.........#...................#.........#....#..........#......................##.......#.... +...........................#.........................#...#.##............#..........#....##....#........#.................###...... +.........#..............#....#....#.....................#.......#...........#....#...#.#......#.....................#....#.....#... +....#.#....##..#.............#.....#.........#...............#....#...............................#.....#.............#..#......... +........#.#...............#.......#........................##..#.............#..................................................#.. +....#.......#................................##....#.#..#..#................#......................#..................#............ +..........##..........#...#...#............#............#..........#.#.........#...#..#.......#.............................#...... +...#................#....#..........#..##...............#.......#.#...#..##.....##..#..........#.........#......................##. +..#......##................................#...........#.....#....###......#.#.......#............#............#........#.......... +.##.#....#...........#...#.................#.....##...#........................#..#......#..#.........#......................#..... +.....................##.....................................#............#....#...........#.#...#...............#..........#....... +...#....................##.......##.........#............##.............#..................#...................................#... +....#...........................##.....#....#...................................#.......#..##.......#...........#...#.............. +..#............#.................#....#...#....#.....................#.......##...........#...#..............................#..... +.............................#....#....#.........#...........................#.............##.......................#.............. +...#..........#...............#..........#.........#.....#........#................##............#..........#....#....##........... +.#................#...#............#.#............#...#......#......#........#...................##.........#....................#. +.#.............#........#.....#...#...............#............#........#.......#..#.....................##......#......#........#. +.....................#..........#...#.........#.#.....#.#..........#...#..........#.#..........#.................##................ +...............#........................##...#.......#.......#..#....#.............#..#......#.....#........##..................... +..............#...........##............#.........#.............#........#...#....#...............##.............#................. +............#.................#........#..............#................................#.....##.#..........#..........#............ +.................................................................S................................................................. +........###.............#......#...#..#.#.........#......#......#........#..#...#....#.......#...#.#...#...#......#.........#...... +......................#.#.#.....#...#.#.#....#..#...............................##..#.......#.............#.....#.................. +..........#.#.........##..#.#.#.........##............#.#..#............#..##.....#......#..#..#.#.#.......#.....#................. +...........#.#...#......#..............................#......#.................................#.#...#.............##............. +..................................#................#..............#..........#...#........#........................................ +..........#.........#.#........#..........#..#............#.......#....#..##..........#..........................#...............#. +...........#..#.##..............##.#......#.....#.#......#.................#....#.....#........#.#.......#....#.................#.. +..............#..........#...#........#......#......#...#..........#.....#.........#.........#...........#........#..#............. +.....#..................#................#...#......#.........................#....#...#..........#....#..#..##.................... +.#.............................#.....#................#........#........#........#....#........................................#... +.........................#.............#.........#............#.#.......#......#.........#...#..............#...#.#........#....... +................#.#......#....##........................#....#....#...........#.......#...........#.........................#...... +....##...#........#......#......#...#....#....#....#....#....#.....#..........#.#.#.#......................#..##..............#.... +............................#........#......#...#.....#................##..#........#...........#...#..#.......#........#........#. +...........#.......#..##........#....#.##...............#......#.........#................#..#......#...................#.......... +......#.............#...##..............#...#.......................................#.......#...#..........#...................##.. +........#............##.#.......#..........#......#.....................................#.......#.................................. +.............................#...............#.................#.....#.......#.#...........#...........#.............#............. +...#....####...................#..##.........................#..#.........#................##....#.#.##............................ +..#.#..#.......#............................#.......................#.#...........#.........................................#..#.#. +......#......#..#............#......#....#.......#..#.......#................#.......#................#............................ +.....#.##..#..#.............#.........#......#.#..............................##..#..................#.............#.......#....... +......#..#.#......................#...........#.............#.........#....#.....#.#.............#..............#.................. +...........#.....................................#........##....#.............###.###...#...#.......#..........#........#......#... +...#...............#..................#....#..#....#.........##.........#....###..#...#..........#.......................#.#.#..... +....#.........#.................#.......#....##...#..#.#...#..#.........#.#...#......#..............#...................#.......#.. +................................................#...#.#....#.......#.................#......#..................##.#.....#.......#.. +........#...#.....................................#.........#.......#......#...................##...........................#...... +..................#................#...........##....#....##............................#...#.##...........##..........##..#....... +......#............#..............#..#.......#................#...##.....#.........##..........................#...#..........##... +.#....#.......#.#.........#.....................................#..#...##....#....#........#.............#...................#..... +.........#.......#.#................................#...#............#.................#...............#.........#........#....#... +...........#.................#..................##.....#..........#.....#.......#.......##.....................#.............#..... +...#......#.......#......................#.........#.#....#.........................................#.....#....#................... +............#.....#...........#..........#............................#...##........................#..#...#...#.#................. +.....#..............#.#........#.........#.......#......#...........................................#...#..#..........#...#........ +......#.#..................#.....#..............#..#....#.......#...........#....#.......#........#....................#...##...... +..#.............##................#..................##.#.....#.#..#..###...............#..........#..#....#..#.........#.#........ +..............#....#..............#.................#....#...............###.#..#...#..#.........#.#.#.................#.......#.#. +....#..#..................#.....................#.................#..#....#.#.....#..................#...#....#...#................ +.............###.................#.............##...................#...##.........##.........#........#.#..................#..#... +..........#........#......#.#.......#..........##......#....#.#.........#........................................#..#.............. +.....#..............#.#............#....................#...........#..##..........#...........#.#....#.................#.......... +......#...#.......#......#.....##..#....................#......#...#......#...............#....#.#................#................ +.....#..........##....#................###.........#..#.....#...............#......................#.........##.....#...........#.. +....#..##......#......#...............#.............#..#........#.......#.................#...........##...#.#.....#............... +.......#...........##..#.........#.....##.##.........#................#.....#................................#.#................... +.......#..#..........#.#.........#..#.....................###......#........................................#.#.................... +.........#.#...........#.......##..........##................#........#....................##...............#...#...#....#.......#. +...............................#..#.#........................................................#.....#..............#.........#...... +...........#........#.....##..#.....#.#......#....................................................#...........................#.##. +...............#..............#..#....#.#...............#.#..............................#.......#.................#............#.. +.#.....#.............#.....#......#........................###.....#................................................###...#........ +............................##...........#.#....................#.............................###.......#.#..#..................#.. +.#..#.....#.#......#.......................##...................#........................................#................##...#... +.....#..........##........##..........................................................#.............#.............................. +..............................#........#........#.............................#.#...#.............##...........#.............#..... +..#..#...........#.........#.....#....#.....#.................#..................#...#...................#...........#.#.....#..... +.......................#......#...#.........#.#................#............#...#..........#...#.....#....#......#...........#...#. +...#.............#...#........#....#.#.......##.#................................................#........#...........#.....#...... +.........#.##..............#.....................................................#.......##.#.............................#........ +.#........#.......#..#............#.#.#.....#.#.........#.........................................#........#........#.#.#.......... +........##..............................................#................##.#.....#...........##....##.........#........#.......... +...#...........#...#...........#..............#..........##............#.#.................##....#.#.........#......#.#............ +................................................................................................................................... diff --git a/src/test/resources/2023/input_21_test.txt b/src/test/resources/2023/input_21_test.txt new file mode 100644 index 00000000..c66328c4 --- /dev/null +++ b/src/test/resources/2023/input_21_test.txt @@ -0,0 +1,11 @@ +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... \ No newline at end of file