From 57eba89652e9b380e02c9b06b725b0d5a3fb2bf3 Mon Sep 17 00:00:00 2001 From: Michael Captain Date: Fri, 2 Dec 2022 21:50:28 +0200 Subject: [PATCH] Refactor input across years; set up Advent of Code 2022 --- .idea/kotlinc.xml | 3 + pom.xml | 8 +- src/main/advent2017/Day01.kt | 6 +- src/main/advent2017/Day02.kt | 9 +- src/main/advent2017/Day03.kt | 8 +- src/main/advent2017/Day04.kt | 8 +- src/main/advent2017/Day05.kt | 12 +- src/main/advent2017/Day06.kt | 8 +- src/main/advent2017/Day07.kt | 8 +- src/main/advent2017/Day08.kt | 6 +- src/main/advent2017/Day09.kt | 8 +- src/main/advent2017/Day10.kt | 6 +- src/main/advent2017/Day11.kt | 8 +- src/main/advent2017/Day12.kt | 14 +-- src/main/advent2017/Day13.kt | 8 +- src/main/advent2017/Day14.kt | 7 +- src/main/advent2017/Day15.kt | 6 +- src/main/advent2017/Day16.kt | 8 +- src/main/advent2017/Day17.kt | 8 +- src/main/advent2017/Day18.kt | 32 ++++-- src/main/advent2017/Day19.kt | 8 +- src/main/advent2017/Day20.kt | 8 +- src/main/advent2017/Day21.kt | 17 ++- src/main/advent2017/Day22.kt | 8 +- src/main/advent2017/Day23.kt | 12 +- src/main/advent2017/Day24.kt | 8 +- src/main/advent2017/Day25.kt | 2 +- src/main/advent2017/Input.kt | 5 + src/main/advent2018/Input.kt | 7 +- src/main/advent2019/Input.kt | 7 +- src/main/advent2019/Intcode.kt | 1 - src/main/advent2020/Day04.kt | 6 +- src/main/advent2020/Day16.kt | 6 +- src/main/advent2020/Day19.kt | 2 +- src/main/advent2020/Input.kt | 13 +-- src/main/advent2021/Input.kt | 12 +- src/main/advent2022/Input.kt | 6 + src/main/common/Input.kt | 13 +++ src/main/resources/advent2017/day01_input.txt | 1 + src/main/resources/advent2017/day02_input.txt | 16 +++ src/main/resources/advent2017/day03_input.txt | 1 + .../{day4_input => day04_input.txt} | 0 .../{day5_input => day05_input.txt} | 0 src/main/resources/advent2017/day06_input.txt | 1 + .../{day7_input => day07_input.txt} | 0 .../{day8_input => day08_input.txt} | 0 .../{day9_input => day09_input.txt} | 0 src/main/resources/advent2017/day10_input.txt | 1 + .../{day11_input => day11_input.txt} | 0 .../{day12_input => day12_input.txt} | 0 .../{day13_input => day13_input.txt} | 0 src/main/resources/advent2017/day14_input.txt | 1 + .../{day15_input => day15_input.txt} | 0 .../{day16_input => day16_input.txt} | 0 src/main/resources/advent2017/day17_input.txt | 1 + .../{day18_input => day18_input.txt} | 0 .../{day19_input => day19_input.txt} | 0 .../{day20_input => day20_input.txt} | 0 src/main/resources/advent2017/day21_input.txt | 108 ++++++++++++++++++ .../{day22_input => day22_input.txt} | 0 .../{day23_input => day23_input.txt} | 0 .../{day24_input => day24_input.txt} | 0 src/test/advent2017/Day12KtTest.kt | 4 +- src/test/advent2017/Day21KtTest.kt | 4 +- 64 files changed, 310 insertions(+), 150 deletions(-) create mode 100644 src/main/advent2017/Input.kt create mode 100644 src/main/advent2022/Input.kt create mode 100644 src/main/common/Input.kt create mode 100644 src/main/resources/advent2017/day01_input.txt create mode 100644 src/main/resources/advent2017/day02_input.txt create mode 100644 src/main/resources/advent2017/day03_input.txt rename src/main/resources/advent2017/{day4_input => day04_input.txt} (100%) rename src/main/resources/advent2017/{day5_input => day05_input.txt} (100%) create mode 100644 src/main/resources/advent2017/day06_input.txt rename src/main/resources/advent2017/{day7_input => day07_input.txt} (100%) rename src/main/resources/advent2017/{day8_input => day08_input.txt} (100%) rename src/main/resources/advent2017/{day9_input => day09_input.txt} (100%) create mode 100644 src/main/resources/advent2017/day10_input.txt rename src/main/resources/advent2017/{day11_input => day11_input.txt} (100%) rename src/main/resources/advent2017/{day12_input => day12_input.txt} (100%) rename src/main/resources/advent2017/{day13_input => day13_input.txt} (100%) create mode 100644 src/main/resources/advent2017/day14_input.txt rename src/main/resources/advent2017/{day15_input => day15_input.txt} (100%) rename src/main/resources/advent2017/{day16_input => day16_input.txt} (100%) create mode 100644 src/main/resources/advent2017/day17_input.txt rename src/main/resources/advent2017/{day18_input => day18_input.txt} (100%) rename src/main/resources/advent2017/{day19_input => day19_input.txt} (100%) rename src/main/resources/advent2017/{day20_input => day20_input.txt} (100%) create mode 100644 src/main/resources/advent2017/day21_input.txt rename src/main/resources/advent2017/{day22_input => day22_input.txt} (100%) rename src/main/resources/advent2017/{day23_input => day23_input.txt} (100%) rename src/main/resources/advent2017/{day24_input => day24_input.txt} (100%) diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 2d95299..cbad6c7 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -3,4 +3,7 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 60aec5c..6ec9f23 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,8 @@ UTF-8 - 1.7.10 + 1.7.22 + 1.6.4 5.8.2 true 17 @@ -26,6 +27,11 @@ kotlin-stdlib ${kotlin.version} + + org.jetbrains.kotlinx + kotlinx-coroutines-core + ${kotlinx.version} + org.jetbrains.kotlin kotlin-test-junit diff --git a/src/main/advent2017/Day01.kt b/src/main/advent2017/Day01.kt index a83fa88..e88d042 100644 --- a/src/main/advent2017/Day01.kt +++ b/src/main/advent2017/Day01.kt @@ -21,7 +21,7 @@ fun solve1b(s: String): Int { } fun main() { - val input = readLine()!! - println(solve1a(input)) - println(solve1b(input)) + val input = input("day01").first() + println(solve1a(input)) // 1203 + println(solve1b(input)) // 1146 } diff --git a/src/main/advent2017/Day02.kt b/src/main/advent2017/Day02.kt index 9701da3..c340817 100644 --- a/src/main/advent2017/Day02.kt +++ b/src/main/advent2017/Day02.kt @@ -30,8 +30,7 @@ fun solve2b(xs: List): Int { } fun main() { - val xs = mutableListOf() - for (i in 0..15) xs.add(readLine()!!) - println(solve2a(xs)) - println(solve2b(xs)) -} \ No newline at end of file + val xs = input("day02") + println(solve2a(xs)) // 51833 + println(solve2b(xs)) // 288 +} diff --git a/src/main/advent2017/Day03.kt b/src/main/advent2017/Day03.kt index 4846a47..dba8299 100644 --- a/src/main/advent2017/Day03.kt +++ b/src/main/advent2017/Day03.kt @@ -78,7 +78,7 @@ fun solve3b(n: Int): Int { } fun main() { - val input = readLine()!!.toInt() - println(solve3a(input)) - println(solve3b(input)) -} \ No newline at end of file + val input = input("day03").first().toInt() + println(solve3a(input)) // 475 + println(solve3b(input)) // 279138 +} diff --git a/src/main/advent2017/Day04.kt b/src/main/advent2017/Day04.kt index 2fc1e5b..4e8c61b 100644 --- a/src/main/advent2017/Day04.kt +++ b/src/main/advent2017/Day04.kt @@ -25,7 +25,7 @@ fun solve4b(xs: List): Int { } fun main() { - val input = File("src/main/resources/advent2017/day4_input").readLines() - println(solve4a(input)) - println(solve4b(input)) -} \ No newline at end of file + val input = input("day04") + println(solve4a(input)) // 386 + println(solve4b(input)) // 208 +} diff --git a/src/main/advent2017/Day05.kt b/src/main/advent2017/Day05.kt index a921619..05a5fcf 100644 --- a/src/main/advent2017/Day05.kt +++ b/src/main/advent2017/Day05.kt @@ -2,7 +2,8 @@ package advent2017 import java.io.File -fun solve5a(xs: MutableList): Int { +fun solve5a(input: List): Int { + val xs = input.toMutableList() var count = 0 var index = 0 while (true) { @@ -17,7 +18,8 @@ fun solve5a(xs: MutableList): Int { } } -fun solve5b(xs: MutableList): Int { +fun solve5b(input: List): Int { + val xs = input.toMutableList() var count = 0 var index = 0 while (true) { @@ -34,7 +36,7 @@ fun solve5b(xs: MutableList): Int { fun main() { - val input = File("src/main/resources/advent2017/day5_input").readLines().map { it.toInt() } - println(solve5a(input.toMutableList())) - println(solve5b(input.toMutableList())) + val input = input("day05").map { it.toInt() } + println(solve5a(input)) // 339351 + println(solve5b(input)) // 24315397 } diff --git a/src/main/advent2017/Day06.kt b/src/main/advent2017/Day06.kt index 7b547c3..15d9104 100644 --- a/src/main/advent2017/Day06.kt +++ b/src/main/advent2017/Day06.kt @@ -29,7 +29,7 @@ fun solve6b(xs: List): Int { } fun main() { - val input = readLine()!!.split("\t").map { it.toInt() } - println(solve6a(input)) - println(solve6b(input)) -} \ No newline at end of file + val input = input("day06").first().split("\t").map { it.toInt() } + println(solve6a(input)) // 6681 + println(solve6b(input)) // 2392 +} diff --git a/src/main/advent2017/Day07.kt b/src/main/advent2017/Day07.kt index edbb606..e1857bc 100644 --- a/src/main/advent2017/Day07.kt +++ b/src/main/advent2017/Day07.kt @@ -72,7 +72,7 @@ fun solve7b(lines: List): Int { } fun main() { - val input = File("src/main/resources/advent2017/day7_input").readLines() - println(solve7a(input)) - println(solve7b(input)) -} \ No newline at end of file + val input = input("day07") + println(solve7a(input)) // uownj + println(solve7b(input)) // 596 +} diff --git a/src/main/advent2017/Day08.kt b/src/main/advent2017/Day08.kt index 53b05e4..a1c973b 100644 --- a/src/main/advent2017/Day08.kt +++ b/src/main/advent2017/Day08.kt @@ -49,8 +49,8 @@ fun solve8b(lines: List): Int { } fun main() { - val input = File("src/main/resources/advent2017/day8_input").readLines() - println(solve8a(input)) - println(solve8b(input)) + val input = input("day08") + println(solve8a(input)) // 2971 + println(solve8b(input)) // 4254 } diff --git a/src/main/advent2017/Day09.kt b/src/main/advent2017/Day09.kt index 051410e..494cfa3 100644 --- a/src/main/advent2017/Day09.kt +++ b/src/main/advent2017/Day09.kt @@ -31,7 +31,7 @@ fun solve9b(input: String): Int { } fun main() { - val input = File("src/main/resources/advent2017/day9_input").readText() - println(solve9a(input)) - println(solve9b(input)) -} \ No newline at end of file + val input = input("day09").first() + println(solve9a(input)) // 17390 + println(solve9b(input)) // 7825 +} diff --git a/src/main/advent2017/Day10.kt b/src/main/advent2017/Day10.kt index 35ab36c..f21e2e7 100644 --- a/src/main/advent2017/Day10.kt +++ b/src/main/advent2017/Day10.kt @@ -59,8 +59,8 @@ fun solve10b(input: String): String { } fun main() { - val input = readLine()!! - println(solve10a(input, 256)) - println(solve10b(input)) + val input = input("day10").first() + println(solve10a(input, 256)) // 48705 + println(solve10b(input)) // 1c46642b6f2bc21db2a2149d0aeeae5d } diff --git a/src/main/advent2017/Day11.kt b/src/main/advent2017/Day11.kt index 17b3fb7..18d88a8 100644 --- a/src/main/advent2017/Day11.kt +++ b/src/main/advent2017/Day11.kt @@ -42,7 +42,7 @@ fun solve11b(s: String): Int { } fun main() { - val input = File("src/main/resources/advent2017/day11_input").readText() - println(solve11a(input)) - println(solve11b(input)) -} \ No newline at end of file + val input = input("day11").first() + println(solve11a(input)) // 784 + println(solve11b(input)) // 1558 +} diff --git a/src/main/advent2017/Day12.kt b/src/main/advent2017/Day12.kt index 85d5b7f..76c9b72 100644 --- a/src/main/advent2017/Day12.kt +++ b/src/main/advent2017/Day12.kt @@ -22,8 +22,7 @@ fun getGroup(startingVillage: Village): Set { return visited } -fun solve12a(s: String): Int { - val lines = s.split("\n") +fun solve12a(lines: List): Int { val villages = (0 until lines.count()).map { Village(it) } lines.forEach { val (id, pipes) = it.split(" <-> ") @@ -34,8 +33,7 @@ fun solve12a(s: String): Int { return getGroup(villages[0]).count() } -fun solve12b(s: String): Int { - val lines = s.split("\n") +fun solve12b(lines: List): Int { val villages = (0 until lines.count()).map { Village(it) } lines.forEach { val (id, pipes) = it.split(" <-> ") @@ -55,7 +53,7 @@ fun solve12b(s: String): Int { } fun main() { - val input = File("src/main/resources/advent2017/day12_input").readText() - println(solve12a(input)) - println(solve12b(input)) -} \ No newline at end of file + val input = input("day12") + println(solve12a(input)) // 130 + println(solve12b(input)) // 189 +} diff --git a/src/main/advent2017/Day13.kt b/src/main/advent2017/Day13.kt index c0f1631..9f52645 100644 --- a/src/main/advent2017/Day13.kt +++ b/src/main/advent2017/Day13.kt @@ -63,7 +63,7 @@ fun solve13b(lines: List): Int { } fun main() { - val input = File("src/main/resources/advent2017/day13_input").readText() - println(solve13a(input.split("\n").filter { it.isNotEmpty() })) - println(solve13b(input.split("\n").filter { it.isNotEmpty() })) -} \ No newline at end of file + val input = input("day13") + println(solve13a(input)) // 3184 + println(solve13b(input)) // 3878062 +} diff --git a/src/main/advent2017/Day14.kt b/src/main/advent2017/Day14.kt index e21197c..e57c67d 100644 --- a/src/main/advent2017/Day14.kt +++ b/src/main/advent2017/Day14.kt @@ -40,6 +40,7 @@ fun solve14b(input: String): Int { } fun main() { - val input = readLine()!! - println(solve14b(input)) -} \ No newline at end of file + val input = input("day14").first() + println(solve14a(input)) // 8106 + println(solve14b(input)) // 1164 +} diff --git a/src/main/advent2017/Day15.kt b/src/main/advent2017/Day15.kt index aad532f..83cecf0 100644 --- a/src/main/advent2017/Day15.kt +++ b/src/main/advent2017/Day15.kt @@ -50,6 +50,6 @@ fun solve15b(initialA: Long, initialB: Long, repeats: Int): Int { fun main() { - println(solve15a(703, 516, 40_000_000)) - println(solve15b(703, 516, 5_000_000)) -} \ No newline at end of file + println(solve15a(703, 516, 40_000_000)) // 594 + println(solve15b(703, 516, 5_000_000)) // 328 +} diff --git a/src/main/advent2017/Day16.kt b/src/main/advent2017/Day16.kt index d4edd76..d662136 100644 --- a/src/main/advent2017/Day16.kt +++ b/src/main/advent2017/Day16.kt @@ -63,7 +63,7 @@ fun solve16b(s: String): String { } fun main() { - val input = File("src/main/resources/advent2017/day16_input").readText() - println(solve16a(input)) - println(solve16b(input)) -} \ No newline at end of file + val input = input("day16").first() + println(solve16a(input)) // kbednhopmfcjilag + println(solve16b(input)) // fbmcgdnjakpioelh +} diff --git a/src/main/advent2017/Day17.kt b/src/main/advent2017/Day17.kt index 7c9f0d1..5ac9f0f 100644 --- a/src/main/advent2017/Day17.kt +++ b/src/main/advent2017/Day17.kt @@ -33,7 +33,7 @@ fun solve17b(step: Int): Int { } fun main() { - val input = readLine()!! - println(solve17a(input.toInt())) - println(solve17b(input.toInt())) -} \ No newline at end of file + val input = input("day17").first().toInt() + println(solve17a(input)) // 1670 + println(solve17b(input)) // 2316253 +} diff --git a/src/main/advent2017/Day18.kt b/src/main/advent2017/Day18.kt index 6205bea..79da75e 100644 --- a/src/main/advent2017/Day18.kt +++ b/src/main/advent2017/Day18.kt @@ -1,6 +1,5 @@ package advent2017 -import java.io.File import java.math.BigInteger import java.util.concurrent.ConcurrentLinkedQueue import kotlin.concurrent.thread @@ -13,6 +12,8 @@ class Duet(private val instructions: List, val p: BigInteger = BigIntege var partnerQueue = ConcurrentLinkedQueue() private var sendCount = 0 + fun sendCount() = sendCount + private fun lookupRegister(x: String): BigInteger { return registers[x] ?: x.toBigIntegerOrNull() ?: BigInteger.ZERO } @@ -64,7 +65,9 @@ class Duet(private val instructions: List, val p: BigInteger = BigIntege return } } - val jumpDistance = if (i[0] == "jgz") { jgz(i[1], i[2]) } else 1 + val jumpDistance = if (i[0] == "jgz") { + jgz(i[1], i[2]) + } else 1 currentInstruction += jumpDistance } } @@ -75,7 +78,6 @@ class Duet(private val instructions: List, val p: BigInteger = BigIntege private fun send(x: String) { sendCount += 1 - println("p: $p, send count: $sendCount") partnerQueue.add(lookupRegister(x)) } @@ -99,7 +101,9 @@ class Duet(private val instructions: List, val p: BigInteger = BigIntege "mod" -> mod(i[1], i[2]) "rcv" -> receive(i[1]) } - val jumpDistance = if (i[0] == "jgz") { jgz(i[1], i[2]) } else 1 + val jumpDistance = if (i[0] == "jgz") { + jgz(i[1], i[2]) + } else 1 currentInstruction += jumpDistance } } @@ -112,22 +116,28 @@ fun solve18a(input: List): BigInteger { return duet.firstReceived() } -fun solve18b(input: List) { +fun solve18b(input: List): Int { val duet1 = Duet(input, BigInteger.ZERO) val duet2 = Duet(input, BigInteger.ONE) duet1.partnerQueue = duet2.receivedSounds duet2.partnerQueue = duet1.receivedSounds - thread { + // TODO: replace with coroutines + val t1 = thread { duet1.duet() } - thread { + val t2 = thread { duet2.duet() } + // wait for deadlock + Thread.sleep(1000) + t1.stop() + t2.stop() + return duet2.sendCount() } fun main() { - val input = File("src/main/resources/advent2017/day18_input").readText() -// println(solve18a(input.split("\n"))) - println(solve18b(input.split("\n"))) -} \ No newline at end of file + val input = input("day18") + println(solve18a(input)) // 3423 + println(solve18b(input)) // 7493 +} diff --git a/src/main/advent2017/Day19.kt b/src/main/advent2017/Day19.kt index a5b44e2..9bb4a43 100644 --- a/src/main/advent2017/Day19.kt +++ b/src/main/advent2017/Day19.kt @@ -52,6 +52,8 @@ fun solve19(input: List): Pair { } fun main() { - val input = File("src/main/resources/advent2017/day19_input").readText() - println(solve19(input.split("\n"))) -} \ No newline at end of file + val input = input("day19") + val (partA, partB) = solve19(input) + println(partA) // VEBTPXCHLI + println(partB) // 18702 +} diff --git a/src/main/advent2017/Day20.kt b/src/main/advent2017/Day20.kt index f6d3fc3..6ee1c4e 100644 --- a/src/main/advent2017/Day20.kt +++ b/src/main/advent2017/Day20.kt @@ -68,7 +68,7 @@ fun solve20b(input: List): Int { fun main() { - val input = File("src/main/resources/advent2017/day20_input").readText() -// println(solve20a(input.split("\n"))) - println(solve20b(input.split("\n").filterNot { it == "" })) -} \ No newline at end of file + val input = input("day20") + println(solve20a(input)) // 170 + println(solve20b(input)) // 571 +} diff --git a/src/main/advent2017/Day21.kt b/src/main/advent2017/Day21.kt index 13c10f2..1f0dd1c 100644 --- a/src/main/advent2017/Day21.kt +++ b/src/main/advent2017/Day21.kt @@ -89,7 +89,7 @@ fun iterateImage(image: List, patterns: Map, List>) return joinSquaresToImage(splitImageToSquares(image).map { matchPattern(patterns, it) }) } -fun solve21a(input: List, iterations: Int): Int { +fun solve21(input: List, iterations: Int): Int { val patterns = mutableMapOf, List>() input.forEach { val (patternIn, patternOut) = it.split(" => ") @@ -101,7 +101,16 @@ fun solve21a(input: List, iterations: Int): Int { return finalImage.sumOf { it.count { c -> c == '#' } } } +fun solve21a(input: List): Int { + return solve21(input, 5) +} + +fun solve21b(input: List): Int { + return solve21(input, 18) +} + fun main() { - val input = File("src/main/resources/advent2017/day21_input").readText() - println(solve21a(input.split("\n").filterNot { it == "" }, 18)) -} \ No newline at end of file + val input = input("day21") + println(solve21a(input)) // 205 + println(solve21b(input)) // 3389823 +} diff --git a/src/main/advent2017/Day22.kt b/src/main/advent2017/Day22.kt index cb76322..6fa8757 100644 --- a/src/main/advent2017/Day22.kt +++ b/src/main/advent2017/Day22.kt @@ -114,7 +114,7 @@ fun solve22b(input: List, iterations: Int): Int { } fun main() { - val input = File("src/main/resources/advent2017/day22_input").readText() -// println(solve22a(input.split("\n").filterNot { it == "" }, 10000)) - println(solve22b(input.split("\n").filterNot { it == "" }, 10_000_000)) -} \ No newline at end of file + val input = input("day22") + println(solve22a(input, 10_000)) // 5259 + println(solve22b(input, 10_000_000)) // 2511722 +} diff --git a/src/main/advent2017/Day23.kt b/src/main/advent2017/Day23.kt index 6aaea7e..42b3d64 100644 --- a/src/main/advent2017/Day23.kt +++ b/src/main/advent2017/Day23.kt @@ -1,6 +1,5 @@ package advent2017 -import java.io.File import kotlin.math.roundToInt import kotlin.math.sqrt @@ -52,7 +51,6 @@ class Coprocessor(private val instructions: List, a: Long) { jnz(i[1], i[2]) } else 1 currentInstruction += jumpDistance - println(registers) } return this } @@ -77,8 +75,8 @@ fun isPrime(n: Int): Boolean { } fun solve23b(input: List): Long { - Coprocessor(input, 1L).startAt(0).run().getH() /* + Coprocessor(input, 1L).startAt(0).run().getH() val hackedRegisters1 = mutableMapOf( "a" to 1L, "b" to 109300L, @@ -113,7 +111,7 @@ fun solve23b(input: List): Long { } fun main() { - val input = File("src/main/resources/advent2017/day23_input").readText() - println(solve23a(input.split("\n").filterNot { it == "" })) - println(solve23b(input.split("\n").filterNot { it == "" })) -} \ No newline at end of file + val input = input("day23") + println(solve23a(input)) // 8281 + println(solve23b(input)) // 911 +} diff --git a/src/main/advent2017/Day24.kt b/src/main/advent2017/Day24.kt index 2c22698..413be7b 100644 --- a/src/main/advent2017/Day24.kt +++ b/src/main/advent2017/Day24.kt @@ -63,7 +63,7 @@ fun solve24b(input: List): Int { } fun main() { - val input = File("src/main/resources/advent2017/day24_input").readText() - println(solve24a(input.split("\n").filterNot { it == "" })) - println(solve24b(input.split("\n").filterNot { it == "" })) -} \ No newline at end of file + val input = input("day24") + println(solve24a(input)) // 1859 + println(solve24b(input)) // 1799 +} diff --git a/src/main/advent2017/Day25.kt b/src/main/advent2017/Day25.kt index d2ca661..1d56a4a 100644 --- a/src/main/advent2017/Day25.kt +++ b/src/main/advent2017/Day25.kt @@ -85,5 +85,5 @@ fun solve25a(): Int { } fun main() { - println(solve25a()) + println(solve25a()) // 4287 } diff --git a/src/main/advent2017/Input.kt b/src/main/advent2017/Input.kt new file mode 100644 index 0000000..6d01bc4 --- /dev/null +++ b/src/main/advent2017/Input.kt @@ -0,0 +1,5 @@ +package advent2017 + +const val adventYear = "advent2017" + +fun input(day: String) = common.input(adventYear, day) diff --git a/src/main/advent2018/Input.kt b/src/main/advent2018/Input.kt index b39fbc0..6779f55 100644 --- a/src/main/advent2018/Input.kt +++ b/src/main/advent2018/Input.kt @@ -1,8 +1,5 @@ package advent2018 -import java.io.File +const val adventYear = "advent2018" -fun input(dayNumber: String): List { - val filename = dayNumber + "_input.txt" - return File("src/main/resources/advent2018/${filename}").readLines() -} \ No newline at end of file +fun input(day: String) = common.input(adventYear, day) diff --git a/src/main/advent2019/Input.kt b/src/main/advent2019/Input.kt index d3264eb..04b5f33 100644 --- a/src/main/advent2019/Input.kt +++ b/src/main/advent2019/Input.kt @@ -1,8 +1,5 @@ package advent2019 -import java.io.File +const val adventYear = "advent2019" -fun input(dayNumber: String): List { - val filename = dayNumber + "_input.txt" - return File("src/main/resources/advent2019/${filename}").readLines() -} \ No newline at end of file +fun input(day: String) = common.input(adventYear, day) diff --git a/src/main/advent2019/Intcode.kt b/src/main/advent2019/Intcode.kt index a88d702..e42bda0 100644 --- a/src/main/advent2019/Intcode.kt +++ b/src/main/advent2019/Intcode.kt @@ -1,6 +1,5 @@ package advent2019 -import common.chunkWhen import java.util.* typealias Intcode = List diff --git a/src/main/advent2020/Day04.kt b/src/main/advent2020/Day04.kt index 2298fa1..39932ac 100644 --- a/src/main/advent2020/Day04.kt +++ b/src/main/advent2020/Day04.kt @@ -68,7 +68,7 @@ fun solve4b(inputText: String): Int { } fun main() { - val input = inputText("day04") - println(solve4a(input)) // 233 - println(solve4b(input)) // 111 + val inputText = inputText("day04") + println(solve4a(inputText)) // 233 + println(solve4b(inputText)) // 111 } diff --git a/src/main/advent2020/Day16.kt b/src/main/advent2020/Day16.kt index 54fccb2..a967264 100644 --- a/src/main/advent2020/Day16.kt +++ b/src/main/advent2020/Day16.kt @@ -59,7 +59,7 @@ fun solve16b(input: String): Long { } fun main() { - val input = inputText("day16").trim() - println(solve16a(input)) // 28873 - println(solve16b(input)) // 2587271823407 + val inputText = inputText("day16") + println(solve16a(inputText)) // 28873 + println(solve16b(inputText)) // 2587271823407 } diff --git a/src/main/advent2020/Day19.kt b/src/main/advent2020/Day19.kt index 91c9ecc..0b4337a 100644 --- a/src/main/advent2020/Day19.kt +++ b/src/main/advent2020/Day19.kt @@ -63,6 +63,6 @@ fun solve19b(input: String): Int { fun main() { val inputText = inputText("day19") println(solve19a(inputText)) // 139 - println(solve19b(inputText)) + println(solve19b(inputText)) // 289 } diff --git a/src/main/advent2020/Input.kt b/src/main/advent2020/Input.kt index 4bb7e68..43b2c54 100644 --- a/src/main/advent2020/Input.kt +++ b/src/main/advent2020/Input.kt @@ -1,13 +1,6 @@ package advent2020 -import java.io.File +const val adventYear = "advent2020" -fun input(prefix: String): List { - val filename = prefix + "_input.txt" - return File("src/main/resources/advent2020/${filename}").readLines() -} - -fun inputText(prefix: String): String { - val filename = prefix + "_input.txt" - return File("src/main/resources/advent2020/${filename}").readText() -} +fun input(day: String) = common.input(adventYear, day) +fun inputText(day: String) = common.inputText(adventYear, day) diff --git a/src/main/advent2021/Input.kt b/src/main/advent2021/Input.kt index 8a5d7d4..ab3b4b8 100644 --- a/src/main/advent2021/Input.kt +++ b/src/main/advent2021/Input.kt @@ -1,13 +1,5 @@ package advent2021 -import java.io.File +const val adventYear = "advent2021" -fun input(prefix: String): List { - val filename = prefix + "_input.txt" - return File("src/main/resources/advent2021/${filename}").readLines() -} - -fun inputText(prefix: String): String { - val filename = prefix + "_input.txt" - return File("src/main/resources/advent2021/${filename}").readText() -} \ No newline at end of file +fun input(day: String) = common.input(adventYear, day) diff --git a/src/main/advent2022/Input.kt b/src/main/advent2022/Input.kt new file mode 100644 index 0000000..cc704c1 --- /dev/null +++ b/src/main/advent2022/Input.kt @@ -0,0 +1,6 @@ +package advent2022 + +const val adventYear = "advent2022" + +fun input(day: String) = common.input(adventYear, day) +fun inputText(day: String) = common.input(adventYear, day) diff --git a/src/main/common/Input.kt b/src/main/common/Input.kt new file mode 100644 index 0000000..80c70bb --- /dev/null +++ b/src/main/common/Input.kt @@ -0,0 +1,13 @@ +package common + +import java.io.File + +fun input(adventYear: String, prefix: String): List { + val filename = prefix + "_input.txt" + return File("src/main/resources/${adventYear}/${filename}").readLines() +} + +fun inputText(adventYear: String, prefix: String): String { + val filename = prefix + "_input.txt" + return File("src/main/resources/${adventYear}/${filename}").readText() +} diff --git a/src/main/resources/advent2017/day01_input.txt b/src/main/resources/advent2017/day01_input.txt new file mode 100644 index 0000000..2cb3c24 --- /dev/null +++ b/src/main/resources/advent2017/day01_input.txt @@ -0,0 +1 @@ +31813174349235972159811869755166343882958376474278437681632495222499211488649543755655138842553867246131245462881756862736922925752647341673342756514856663979496747158241792857625471323535183222497949751644488277317173496124473893452425118133645984488759128897146498831373795721661696492622276282881218371273973538163779782435211491196616375135472517935481964439956844536136823757764494967297251545389464472794474447941564778733926532741752757865243946976266426548341889873514383464142659425122786667399143335772174973128383869893325977319651839516694295534146668728822393452626321892357192574444856264721585365164945647254645264693957898373214897848424966266582991272496771159583715456714645585576641458358326521858518319315233857473695712238323787254556597566461188452279853766184333696344395818615215846348586541164194624371353556812548945447432787795489443312941687221314432694115847863129826532628228386894683392352799514942665396273726821936346663485499159141368443782475714679953213388375939519711591262489869326145476958378464652451441434846382474578535468433514121336844727988128998543975147649823215332929623574231738442281161294838499441799996857746549441142859199799125595761724782225452394593514388571187279266291364278184761833324476838939898258225748562345853633364314923186685534864178665214135631494876474186833392929124337161222959459117554238429216916532175247326391321525832362274683763488347654497889261543959591212539851835354335598844669618391876623638137926893582131945361264841733341247646125278489995838369127582438419889922365596554237153412394494932582424222479798382932335239274297663365164912953364777876187522324991837775492621675953397843833247525599771974555545348388871578347332456586949283657613841414576976542343934911424716613479249893113961925713317644349946444271959375981158445151659431844142242547191181944395897963146947935463718145169266129118413523541222444997678726644615185324461293228124456118853885552279849917342474792984425629248492847827653133583215539325866881662159421987315186914769478947389188382383546881622246793781846254253759714573354544997853153798862436887889318646643359555663135476261863 diff --git a/src/main/resources/advent2017/day02_input.txt b/src/main/resources/advent2017/day02_input.txt new file mode 100644 index 0000000..5feeb6a --- /dev/null +++ b/src/main/resources/advent2017/day02_input.txt @@ -0,0 +1,16 @@ +4168 3925 858 2203 440 185 2886 160 1811 4272 4333 2180 174 157 361 1555 +150 111 188 130 98 673 408 632 771 585 191 92 622 158 537 142 +5785 5174 1304 3369 3891 131 141 5781 5543 4919 478 6585 116 520 673 112 +5900 173 5711 236 2920 177 3585 4735 2135 2122 5209 265 5889 233 4639 5572 +861 511 907 138 981 168 889 986 980 471 107 130 596 744 251 123 +2196 188 1245 145 1669 2444 656 234 1852 610 503 2180 551 2241 643 175 +2051 1518 1744 233 2155 139 658 159 1178 821 167 546 126 974 136 1946 +161 1438 3317 4996 4336 2170 130 4987 3323 178 174 4830 3737 4611 2655 2743 +3990 190 192 1630 1623 203 1139 2207 3994 1693 1468 1829 164 4391 3867 3036 +116 1668 1778 69 99 761 201 2013 837 1225 419 120 1920 1950 121 1831 +107 1006 92 807 1880 1420 36 1819 1039 1987 114 2028 1771 25 85 430 +5295 1204 242 479 273 2868 3453 6095 5324 6047 5143 293 3288 3037 184 987 +295 1988 197 2120 199 1856 181 232 564 1914 1691 210 1527 1731 1575 31 +191 53 714 745 89 899 854 679 45 81 726 801 72 338 95 417 +219 3933 6626 2137 3222 1637 5312 238 5895 222 154 6649 169 6438 3435 4183 +37 1069 166 1037 172 258 1071 90 497 1219 145 1206 143 153 1067 510 diff --git a/src/main/resources/advent2017/day03_input.txt b/src/main/resources/advent2017/day03_input.txt new file mode 100644 index 0000000..148fd75 --- /dev/null +++ b/src/main/resources/advent2017/day03_input.txt @@ -0,0 +1 @@ +277678 diff --git a/src/main/resources/advent2017/day4_input b/src/main/resources/advent2017/day04_input.txt similarity index 100% rename from src/main/resources/advent2017/day4_input rename to src/main/resources/advent2017/day04_input.txt diff --git a/src/main/resources/advent2017/day5_input b/src/main/resources/advent2017/day05_input.txt similarity index 100% rename from src/main/resources/advent2017/day5_input rename to src/main/resources/advent2017/day05_input.txt diff --git a/src/main/resources/advent2017/day06_input.txt b/src/main/resources/advent2017/day06_input.txt new file mode 100644 index 0000000..04f1425 --- /dev/null +++ b/src/main/resources/advent2017/day06_input.txt @@ -0,0 +1 @@ +4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3 diff --git a/src/main/resources/advent2017/day7_input b/src/main/resources/advent2017/day07_input.txt similarity index 100% rename from src/main/resources/advent2017/day7_input rename to src/main/resources/advent2017/day07_input.txt diff --git a/src/main/resources/advent2017/day8_input b/src/main/resources/advent2017/day08_input.txt similarity index 100% rename from src/main/resources/advent2017/day8_input rename to src/main/resources/advent2017/day08_input.txt diff --git a/src/main/resources/advent2017/day9_input b/src/main/resources/advent2017/day09_input.txt similarity index 100% rename from src/main/resources/advent2017/day9_input rename to src/main/resources/advent2017/day09_input.txt diff --git a/src/main/resources/advent2017/day10_input.txt b/src/main/resources/advent2017/day10_input.txt new file mode 100644 index 0000000..216967b --- /dev/null +++ b/src/main/resources/advent2017/day10_input.txt @@ -0,0 +1 @@ +192,69,168,160,78,1,166,28,0,83,198,2,254,255,41,12 diff --git a/src/main/resources/advent2017/day11_input b/src/main/resources/advent2017/day11_input.txt similarity index 100% rename from src/main/resources/advent2017/day11_input rename to src/main/resources/advent2017/day11_input.txt diff --git a/src/main/resources/advent2017/day12_input b/src/main/resources/advent2017/day12_input.txt similarity index 100% rename from src/main/resources/advent2017/day12_input rename to src/main/resources/advent2017/day12_input.txt diff --git a/src/main/resources/advent2017/day13_input b/src/main/resources/advent2017/day13_input.txt similarity index 100% rename from src/main/resources/advent2017/day13_input rename to src/main/resources/advent2017/day13_input.txt diff --git a/src/main/resources/advent2017/day14_input.txt b/src/main/resources/advent2017/day14_input.txt new file mode 100644 index 0000000..4344069 --- /dev/null +++ b/src/main/resources/advent2017/day14_input.txt @@ -0,0 +1 @@ +oundnydw diff --git a/src/main/resources/advent2017/day15_input b/src/main/resources/advent2017/day15_input.txt similarity index 100% rename from src/main/resources/advent2017/day15_input rename to src/main/resources/advent2017/day15_input.txt diff --git a/src/main/resources/advent2017/day16_input b/src/main/resources/advent2017/day16_input.txt similarity index 100% rename from src/main/resources/advent2017/day16_input rename to src/main/resources/advent2017/day16_input.txt diff --git a/src/main/resources/advent2017/day17_input.txt b/src/main/resources/advent2017/day17_input.txt new file mode 100644 index 0000000..8661997 --- /dev/null +++ b/src/main/resources/advent2017/day17_input.txt @@ -0,0 +1 @@ +328 diff --git a/src/main/resources/advent2017/day18_input b/src/main/resources/advent2017/day18_input.txt similarity index 100% rename from src/main/resources/advent2017/day18_input rename to src/main/resources/advent2017/day18_input.txt diff --git a/src/main/resources/advent2017/day19_input b/src/main/resources/advent2017/day19_input.txt similarity index 100% rename from src/main/resources/advent2017/day19_input rename to src/main/resources/advent2017/day19_input.txt diff --git a/src/main/resources/advent2017/day20_input b/src/main/resources/advent2017/day20_input.txt similarity index 100% rename from src/main/resources/advent2017/day20_input rename to src/main/resources/advent2017/day20_input.txt diff --git a/src/main/resources/advent2017/day21_input.txt b/src/main/resources/advent2017/day21_input.txt new file mode 100644 index 0000000..f338534 --- /dev/null +++ b/src/main/resources/advent2017/day21_input.txt @@ -0,0 +1,108 @@ +../.. => ###/.##/#.. +#./.. => #.#/..#/#.. +##/.. => ###/.#./### +.#/#. => ##./###/... +##/#. => ##./###/#.# +##/## => #.#/#.#/### +.../.../... => #.#./#..#/#.##/#.#. +#../.../... => #.##/.##./#..#/.### +.#./.../... => #..#/#.#./.#../#.## +##./.../... => .#../...#/####/...# +#.#/.../... => ##.#/..#./...#/###. +###/.../... => #.#./#..#/####/##.. +.#./#../... => ..##/..##/####/##.# +##./#../... => #.#./#.../..../##.. +..#/#../... => .#.#/##.#/...#/#### +#.#/#../... => .#../.###/.##./##.# +.##/#../... => ##../.#.#/#.../..## +###/#../... => ##.#/##.#/.###/##.. +.../.#./... => ..#./..../##.#/#.#. +#../.#./... => ..##/.#.#/..#./###. +.#./.#./... => ...#/.#../.#.#/##.. +##./.#./... => #..#/.###/##../#.#. +#.#/.#./... => ##.#/..#./.#../#..# +###/.#./... => #.#./####/#..#/#... +.#./##./... => ##../##.#/.###/##.. +##./##./... => .#../####/.##./.#.. +..#/##./... => ####/##.#/##.#/###. +#.#/##./... => .##./#.##/##.#/#... +.##/##./... => ..../#.##/##.#/##.. +###/##./... => #.../.##./#.#./#... +.../#.#/... => #..#/..##/#.../#.## +#../#.#/... => ..##/..#./..#./..## +.#./#.#/... => ..##/####/####/.... +##./#.#/... => ###./.#../##.#/#.#. +#.#/#.#/... => .##./#..#/..#./##.. +###/#.#/... => ##.#/..#./#..#/...# +.../###/... => ..##/.#.#/#.../...# +#../###/... => ..##/#.##/#.#./..#. +.#./###/... => ##../..##/.##./...# +##./###/... => #.#./#.../#.../.##. +#.#/###/... => ##.#/..##/..##/.### +###/###/... => ..#./#..#/.#../.##. +..#/.../#.. => ###./#.#./#.##/#.## +#.#/.../#.. => ####/.##./#..#/.### +.##/.../#.. => #.#./..../..../##.# +###/.../#.. => .#.#/..../.#.#/###. +.##/#../#.. => ####/#..#/.##./#### +###/#../#.. => ##../.#../..../###. +..#/.#./#.. => .###/##../#.##/...# +#.#/.#./#.. => ...#/####/#.../..#. +.##/.#./#.. => #.../####/.#.#/###. +###/.#./#.. => ####/..../.#../##.. +.##/##./#.. => ..../###./##../.### +###/##./#.. => #.../#..#/#..#/###. +#../..#/#.. => ..../.###/.#../.#.# +.#./..#/#.. => .#.#/...#/#.#./##.. +##./..#/#.. => .#../##.#/.#../.##. +#.#/..#/#.. => ##../#.##/.###/#.#. +.##/..#/#.. => #.#./..../.#.#/..## +###/..#/#.. => ...#/#.../...#/..#. +#../#.#/#.. => ##.#/..#./###./.### +.#./#.#/#.. => ..../##../.#.#/.### +##./#.#/#.. => ###./#.#./.#../.#.# +..#/#.#/#.. => ###./..../##.#/#..# +#.#/#.#/#.. => .#.#/#.##/#.../..#. +.##/#.#/#.. => .#.#/#.../##../#### +###/#.#/#.. => #.##/..#./..##/.... +#../.##/#.. => .#../.#../...#/#... +.#./.##/#.. => ##../#..#/###./##.# +##./.##/#.. => .#.#/#..#/..../#..# +#.#/.##/#.. => ##.#/..../##../##.. +.##/.##/#.. => #.#./..#./#.../.#.. +###/.##/#.. => #.#./##.#/####/.... +#../###/#.. => ##../#.##/.#../.### +.#./###/#.. => #.../#.##/..../.#.# +##./###/#.. => ###./##../.#../..## +..#/###/#.. => ..#./.#../####/#..# +#.#/###/#.. => #.##/..#./..#./#.## +.##/###/#.. => .#../#.../####/#... +###/###/#.. => #.../..#./..../.##. +.#./#.#/.#. => .#.#/####/###./.... +##./#.#/.#. => ##.#/###./#.##/#..# +#.#/#.#/.#. => ####/#.#./..../##.. +###/#.#/.#. => ##.#/##../.###/..## +.#./###/.#. => .##./...#/##.#/.### +##./###/.#. => ..##/.#../..#./#... +#.#/###/.#. => ####/#.../..#./#.#. +###/###/.#. => #.../.###/..##/.#.# +#.#/..#/##. => ...#/###./#.#./#.## +###/..#/##. => ##../..#./###./##.. +.##/#.#/##. => #.../#.#./#.#./#..# +###/#.#/##. => #.../##.#/#.#./.... +#.#/.##/##. => ...#/#.#./...#/#... +###/.##/##. => .###/...#/#..#/###. +.##/###/##. => ###./.##./##.#/#..# +###/###/##. => #.../##../.###/.#.. +#.#/.../#.# => #.../#..#/...#/#..# +###/.../#.# => .#../..##/.##./.#.# +###/#../#.# => ..../.#.#/###./#... +#.#/.#./#.# => ##.#/.#.#/#.##/...# +###/.#./#.# => ..../#.../#.../.### +###/##./#.# => ..##/.##./##.#/##.# +#.#/#.#/#.# => ..##/.#../..##/..#. +###/#.#/#.# => ..##/.###/...#/##.. +#.#/###/#.# => ..#./.#.#/.###/#### +###/###/#.# => #.#./##../#.#./##.# +###/#.#/### => #.#./..##/#.#./#... +###/###/### => ##../.###/###./#..# diff --git a/src/main/resources/advent2017/day22_input b/src/main/resources/advent2017/day22_input.txt similarity index 100% rename from src/main/resources/advent2017/day22_input rename to src/main/resources/advent2017/day22_input.txt diff --git a/src/main/resources/advent2017/day23_input b/src/main/resources/advent2017/day23_input.txt similarity index 100% rename from src/main/resources/advent2017/day23_input rename to src/main/resources/advent2017/day23_input.txt diff --git a/src/main/resources/advent2017/day24_input b/src/main/resources/advent2017/day24_input.txt similarity index 100% rename from src/main/resources/advent2017/day24_input rename to src/main/resources/advent2017/day24_input.txt diff --git a/src/test/advent2017/Day12KtTest.kt b/src/test/advent2017/Day12KtTest.kt index d52f031..58b64b0 100644 --- a/src/test/advent2017/Day12KtTest.kt +++ b/src/test/advent2017/Day12KtTest.kt @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test internal class Day12KtTest { - val s = "0 <-> 2\n1 <-> 1\n2 <-> 0, 3, 4\n3 <-> 2, 4\n4 <-> 2, 3, 6\n5 <-> 6\n6 <-> 4, 5" + val s = "0 <-> 2\n1 <-> 1\n2 <-> 0, 3, 4\n3 <-> 2, 4\n4 <-> 2, 3, 6\n5 <-> 6\n6 <-> 4, 5".split("\n") @Test fun solve12a() { @@ -15,4 +15,4 @@ internal class Day12KtTest { fun solve12b() { assertEquals(2, solve12b(s)) } -} \ No newline at end of file +} diff --git a/src/test/advent2017/Day21KtTest.kt b/src/test/advent2017/Day21KtTest.kt index 0b9aa23..d6d76c2 100644 --- a/src/test/advent2017/Day21KtTest.kt +++ b/src/test/advent2017/Day21KtTest.kt @@ -12,6 +12,6 @@ internal class Day21KtTest { @Test fun solve21a() { - assertEquals(12, solve21a(listOf("../.# => ##./#../...", ".#./..#/### => #..#/..../..../#..#"), 2)) + assertEquals(12, solve21(listOf("../.# => ##./#../...", ".#./..#/### => #..#/..../..../#..#"), 2)) } -} \ No newline at end of file +} -- 2.45.2