From 01361eabf6f2cd1b491f04781d267130d9a45616 Mon Sep 17 00:00:00 2001 From: Andreas Lau Date: Sat, 7 Dec 2024 20:05:13 +0100 Subject: [PATCH] Day 5: Print Queue (#5) --- README.md | 1 + settings.gradle | 2 +- src/main/kotlin/days/Day5.kt | 47 + src/main/resources/input_day_5.txt | 1377 ++++++++++++++++++++++++++++ src/test/kotlin/SolutionsTest.kt | 6 +- src/test/kotlin/days/Day5Test.kt | 59 ++ 6 files changed, 1487 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/days/Day5.kt create mode 100644 src/main/resources/input_day_5.txt create mode 100644 src/test/kotlin/days/Day5Test.kt diff --git a/README.md b/README.md index 704d831..cbfc0ba 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Advent of Code is an Advent calendar of small programming puzzles by [Eric Wastl - Day 2: [Red-Nosed Reports](https://adventofcode.com/2024/day/2) -- [Day2.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day2.kt) - Day 3: [Mull It Over](https://adventofcode.com/2024/day/3) -- [Day3.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day3.kt) - Day 4: [Ceres Search](https://adventofcode.com/2024/day/4) -- [Day4.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day4.kt) +- Day 5: [Print Queue](https://adventofcode.com/2024/day/5) -- [Day4.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day5.kt) ### Features diff --git a/settings.gradle b/settings.gradle index de6340e..e7917f1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -rootProject.name = 'advent-of-code-starter' \ No newline at end of file +rootProject.name = 'advent-of-code-2024' \ No newline at end of file diff --git a/src/main/kotlin/days/Day5.kt b/src/main/kotlin/days/Day5.kt new file mode 100644 index 0000000..1d98630 --- /dev/null +++ b/src/main/kotlin/days/Day5.kt @@ -0,0 +1,47 @@ +package days + +@AdventOfCodePuzzle( + name = "Print Queue", + url = "https://adventofcode.com/2024/day/5", + date = Date(day = 5, year = 2024) +) +class Day5(input: List) : Puzzle { + + private val pageOrderingRules = input + .takeWhile { it.isNotEmpty() } + .map { line -> line.split('|').let { it[0].toInt() to it[1].toInt() } } + private val pageToProduce = input + .dropWhile { it.isNotEmpty() } + .drop(1) + .map { it.split(',').map { it.toInt() } } + + override fun partOne(): Int = pageToProduce + .filter { pages -> validOrder(pages) } + .sumOf(::middlePage) + + override fun partTwo() = pageToProduce + .filterNot { pages -> validOrder(pages) } + .map { pages -> reorder(pages) } + .sumOf { middlePage(it) } + + private fun reorder(pages: List): List { + val selectedRules = pageOrderingRules.filter { (prev, next) -> prev in pages && next in pages } + val eachCount = selectedRules.groupingBy { it.first }.eachCount().toMutableMap() + selectedRules.map { it.second }.forEach { eachCount.putIfAbsent(it, 0) } + + return eachCount + .map { (page, degree) -> degree to page } + .toMap() + .toSortedMap(Comparator.naturalOrder().reversed()) + .values.toList() + } + + private fun validOrder(pages: List): Boolean { + return pageOrderingRules + .filter { (prev, next) -> prev in pages && next in pages } + .all { (prev, next) -> pages.indexOf(prev) < pages.indexOf(next) } + } + + private fun middlePage(it: List) = it[it.size / 2] + +} \ No newline at end of file diff --git a/src/main/resources/input_day_5.txt b/src/main/resources/input_day_5.txt new file mode 100644 index 0000000..0d27859 --- /dev/null +++ b/src/main/resources/input_day_5.txt @@ -0,0 +1,1377 @@ +83|94 +97|41 +97|59 +35|79 +35|41 +35|84 +92|57 +92|97 +92|22 +92|23 +89|17 +89|95 +89|14 +89|94 +89|74 +52|39 +52|37 +52|81 +52|97 +52|12 +52|78 +87|35 +87|21 +87|58 +87|13 +87|52 +87|14 +87|53 +93|71 +93|77 +93|12 +93|69 +93|16 +93|94 +93|74 +93|83 +69|47 +69|92 +69|77 +69|28 +69|83 +69|94 +69|42 +69|31 +69|27 +42|23 +42|41 +42|37 +42|86 +42|54 +42|79 +42|81 +42|21 +42|53 +42|75 +25|58 +25|92 +25|53 +25|94 +25|13 +25|12 +25|17 +25|31 +25|19 +25|71 +25|95 +94|87 +94|14 +94|92 +94|35 +94|99 +94|54 +94|27 +94|52 +94|22 +94|24 +94|95 +94|17 +14|86 +14|24 +14|84 +14|99 +14|21 +14|13 +14|39 +14|35 +14|79 +14|54 +14|23 +14|33 +14|53 +57|86 +57|19 +57|16 +57|97 +57|74 +57|81 +57|93 +57|77 +57|78 +57|37 +57|15 +57|67 +57|46 +57|83 +99|41 +99|67 +99|77 +99|89 +99|78 +99|15 +99|81 +99|46 +99|79 +99|97 +99|59 +99|84 +99|83 +99|86 +99|23 +58|75 +58|13 +58|21 +58|95 +58|39 +58|52 +58|42 +58|54 +58|86 +58|93 +58|37 +58|99 +58|53 +58|31 +58|97 +58|22 +31|53 +31|84 +31|95 +31|86 +31|23 +31|93 +31|99 +31|46 +31|21 +31|24 +31|81 +31|57 +31|79 +31|35 +31|97 +31|22 +31|67 +37|19 +37|94 +37|41 +37|59 +37|67 +37|16 +37|14 +37|47 +37|78 +37|12 +37|15 +37|46 +37|75 +37|81 +37|71 +37|89 +37|74 +37|87 +79|81 +79|37 +79|86 +79|94 +79|75 +79|59 +79|47 +79|77 +79|97 +79|16 +79|12 +79|69 +79|78 +79|89 +79|46 +79|57 +79|15 +79|67 +79|41 +74|87 +74|19 +74|42 +74|27 +74|21 +74|24 +74|35 +74|54 +74|22 +74|52 +74|17 +74|84 +74|33 +74|28 +74|58 +74|92 +74|31 +74|71 +74|13 +74|95 +22|86 +22|84 +22|35 +22|41 +22|57 +22|81 +22|69 +22|99 +22|79 +22|37 +22|46 +22|59 +22|67 +22|78 +22|25 +22|23 +22|75 +22|97 +22|77 +22|52 +22|47 +84|75 +84|94 +84|47 +84|25 +84|78 +84|86 +84|67 +84|41 +84|59 +84|69 +84|89 +84|97 +84|77 +84|15 +84|79 +84|81 +84|93 +84|37 +84|46 +84|39 +84|23 +84|12 +12|95 +12|19 +12|53 +12|71 +12|83 +12|31 +12|92 +12|14 +12|74 +12|33 +12|15 +12|24 +12|22 +12|28 +12|16 +12|87 +12|42 +12|58 +12|27 +12|94 +12|17 +12|54 +12|13 +33|99 +33|37 +33|17 +33|22 +33|39 +33|21 +33|42 +33|95 +33|54 +33|53 +33|79 +33|52 +33|93 +33|31 +33|35 +33|75 +33|24 +33|23 +33|84 +33|58 +33|86 +33|13 +33|97 +33|57 +81|94 +81|33 +81|74 +81|14 +81|47 +81|67 +81|19 +81|13 +81|15 +81|27 +81|69 +81|78 +81|16 +81|25 +81|58 +81|87 +81|71 +81|89 +81|92 +81|28 +81|12 +81|83 +81|59 +81|77 +39|57 +39|74 +39|77 +39|46 +39|37 +39|78 +39|41 +39|94 +39|86 +39|93 +39|47 +39|15 +39|75 +39|97 +39|83 +39|25 +39|81 +39|67 +39|69 +39|23 +39|89 +39|12 +39|79 +39|59 +17|53 +17|69 +17|35 +17|41 +17|22 +17|84 +17|78 +17|81 +17|39 +17|21 +17|75 +17|77 +17|57 +17|37 +17|59 +17|67 +17|23 +17|97 +17|46 +17|86 +17|52 +17|79 +17|99 +17|93 +95|97 +95|79 +95|22 +95|17 +95|59 +95|39 +95|69 +95|81 +95|46 +95|41 +95|67 +95|75 +95|86 +95|21 +95|53 +95|23 +95|37 +95|93 +95|99 +95|78 +95|52 +95|57 +95|84 +95|35 +54|75 +54|37 +54|22 +54|67 +54|79 +54|21 +54|53 +54|52 +54|97 +54|99 +54|93 +54|81 +54|23 +54|17 +54|69 +54|84 +54|46 +54|39 +54|86 +54|78 +54|35 +54|95 +54|57 +54|41 +47|83 +47|14 +47|13 +47|28 +47|12 +47|17 +47|71 +47|58 +47|87 +47|74 +47|27 +47|33 +47|92 +47|25 +47|54 +47|16 +47|24 +47|95 +47|94 +47|15 +47|31 +47|42 +47|89 +47|19 +41|16 +41|78 +41|12 +41|87 +41|92 +41|25 +41|89 +41|69 +41|94 +41|46 +41|59 +41|83 +41|15 +41|19 +41|81 +41|67 +41|14 +41|74 +41|33 +41|27 +41|77 +41|28 +41|71 +41|47 +67|71 +67|94 +67|59 +67|15 +67|25 +67|16 +67|69 +67|12 +67|33 +67|13 +67|58 +67|89 +67|87 +67|47 +67|42 +67|92 +67|83 +67|14 +67|27 +67|19 +67|77 +67|78 +67|28 +67|74 +77|14 +77|94 +77|92 +77|13 +77|83 +77|33 +77|24 +77|42 +77|89 +77|16 +77|31 +77|12 +77|19 +77|58 +77|74 +77|87 +77|71 +77|28 +77|27 +77|54 +77|25 +77|15 +77|95 +77|47 +15|53 +15|54 +15|33 +15|87 +15|17 +15|52 +15|74 +15|71 +15|35 +15|58 +15|27 +15|13 +15|14 +15|21 +15|19 +15|94 +15|16 +15|24 +15|22 +15|42 +15|95 +15|28 +15|31 +15|92 +78|42 +78|14 +78|87 +78|28 +78|69 +78|74 +78|16 +78|31 +78|89 +78|83 +78|77 +78|58 +78|92 +78|12 +78|19 +78|27 +78|33 +78|47 +78|71 +78|13 +78|59 +78|15 +78|94 +78|25 +71|33 +71|28 +71|95 +71|86 +71|52 +71|24 +71|92 +71|99 +71|79 +71|22 +71|35 +71|21 +71|39 +71|57 +71|97 +71|31 +71|54 +71|17 +71|42 +71|23 +71|53 +71|13 +71|58 +71|84 +23|47 +23|77 +23|12 +23|25 +23|87 +23|94 +23|78 +23|89 +23|37 +23|75 +23|15 +23|59 +23|16 +23|74 +23|41 +23|81 +23|69 +23|83 +23|93 +23|67 +23|46 +23|27 +23|19 +23|97 +13|37 +13|54 +13|99 +13|17 +13|95 +13|41 +13|75 +13|79 +13|42 +13|57 +13|22 +13|31 +13|97 +13|23 +13|35 +13|39 +13|24 +13|86 +13|46 +13|52 +13|93 +13|84 +13|21 +13|53 +28|37 +28|93 +28|13 +28|21 +28|79 +28|97 +28|39 +28|24 +28|33 +28|42 +28|54 +28|31 +28|23 +28|86 +28|17 +28|35 +28|95 +28|57 +28|58 +28|84 +28|22 +28|99 +28|53 +28|52 +16|27 +16|14 +16|87 +16|92 +16|33 +16|53 +16|39 +16|54 +16|24 +16|22 +16|52 +16|13 +16|71 +16|84 +16|58 +16|21 +16|99 +16|31 +16|95 +16|19 +16|17 +16|42 +16|28 +16|35 +75|69 +75|28 +75|27 +75|25 +75|47 +75|16 +75|19 +75|67 +75|15 +75|59 +75|89 +75|14 +75|46 +75|94 +75|74 +75|12 +75|81 +75|78 +75|41 +75|71 +75|92 +75|77 +75|83 +75|87 +27|17 +27|22 +27|52 +27|33 +27|99 +27|35 +27|54 +27|42 +27|84 +27|79 +27|71 +27|87 +27|53 +27|57 +27|28 +27|58 +27|39 +27|13 +27|24 +27|92 +27|95 +27|21 +27|31 +27|14 +21|78 +21|97 +21|77 +21|86 +21|79 +21|46 +21|69 +21|37 +21|39 +21|81 +21|25 +21|35 +21|59 +21|23 +21|57 +21|84 +21|75 +21|12 +21|47 +21|93 +21|83 +21|99 +21|67 +21|41 +53|75 +53|79 +53|77 +53|52 +53|97 +53|67 +53|81 +53|57 +53|22 +53|47 +53|78 +53|99 +53|86 +53|84 +53|35 +53|37 +53|69 +53|23 +53|46 +53|21 +53|93 +53|41 +53|59 +53|39 +86|94 +86|75 +86|89 +86|25 +86|74 +86|15 +86|93 +86|27 +86|78 +86|81 +86|77 +86|97 +86|59 +86|41 +86|83 +86|37 +86|69 +86|19 +86|16 +86|47 +86|67 +86|23 +86|46 +86|12 +59|92 +59|16 +59|12 +59|83 +59|31 +59|14 +59|25 +59|74 +59|33 +59|28 +59|15 +59|24 +59|94 +59|27 +59|87 +59|58 +59|89 +59|19 +59|54 +59|42 +59|77 +59|13 +59|47 +59|71 +46|16 +46|47 +46|89 +46|74 +46|67 +46|14 +46|19 +46|92 +46|25 +46|94 +46|81 +46|15 +46|71 +46|83 +46|12 +46|28 +46|78 +46|69 +46|58 +46|33 +46|27 +46|77 +46|87 +46|59 +24|79 +24|21 +24|93 +24|78 +24|52 +24|46 +24|17 +24|39 +24|84 +24|67 +24|86 +24|97 +24|53 +24|95 +24|81 +24|22 +24|57 +24|35 +24|75 +24|54 +24|37 +24|99 +24|23 +24|41 +19|42 +19|87 +19|28 +19|39 +19|24 +19|14 +19|54 +19|21 +19|99 +19|31 +19|84 +19|53 +19|95 +19|79 +19|52 +19|17 +19|92 +19|35 +19|33 +19|58 +19|13 +19|71 +19|22 +19|27 +83|42 +83|87 +83|89 +83|53 +83|24 +83|16 +83|52 +83|58 +83|95 +83|54 +83|14 +83|28 +83|31 +83|22 +83|92 +83|74 +83|15 +83|33 +83|19 +83|13 +83|71 +83|17 +83|27 +97|12 +97|19 +97|89 +97|87 +97|75 +97|15 +97|74 +97|81 +97|16 +97|69 +97|47 +97|14 +97|78 +97|46 +97|27 +97|67 +97|94 +97|37 +97|77 +97|83 +97|25 +97|93 +35|77 +35|93 +35|23 +35|78 +35|39 +35|86 +35|89 +35|99 +35|83 +35|46 +35|69 +35|12 +35|57 +35|59 +35|75 +35|67 +35|47 +35|97 +35|81 +35|25 +35|37 +92|53 +92|95 +92|24 +92|42 +92|39 +92|13 +92|79 +92|21 +92|86 +92|58 +92|54 +92|35 +92|33 +92|37 +92|84 +92|99 +92|52 +92|31 +92|17 +92|28 +89|19 +89|24 +89|52 +89|16 +89|15 +89|71 +89|33 +89|27 +89|42 +89|21 +89|53 +89|31 +89|28 +89|58 +89|92 +89|87 +89|13 +89|54 +89|22 +52|77 +52|69 +52|75 +52|86 +52|23 +52|93 +52|46 +52|57 +52|41 +52|84 +52|21 +52|67 +52|25 +52|35 +52|79 +52|99 +52|47 +52|59 +87|39 +87|92 +87|22 +87|54 +87|79 +87|71 +87|84 +87|33 +87|86 +87|95 +87|57 +87|42 +87|99 +87|17 +87|24 +87|28 +87|31 +93|81 +93|25 +93|19 +93|15 +93|92 +93|67 +93|87 +93|27 +93|41 +93|75 +93|89 +93|78 +93|46 +93|59 +93|47 +93|14 +69|15 +69|24 +69|13 +69|14 +69|59 +69|12 +69|25 +69|33 +69|16 +69|58 +69|71 +69|19 +69|87 +69|74 +69|89 +42|39 +42|99 +42|46 +42|57 +42|31 +42|24 +42|17 +42|97 +42|93 +42|95 +42|52 +42|84 +42|22 +42|35 +25|83 +25|89 +25|14 +25|28 +25|87 +25|15 +25|74 +25|16 +25|27 +25|42 +25|33 +25|24 +25|54 +94|19 +94|31 +94|21 +94|42 +94|33 +94|71 +94|58 +94|28 +94|13 +94|53 +94|16 +94|74 +14|42 +14|52 +14|92 +14|58 +14|31 +14|57 +14|95 +14|22 +14|71 +14|17 +14|28 +57|59 +57|12 +57|25 +57|94 +57|47 +57|41 +57|75 +57|89 +57|69 +57|23 +99|69 +99|47 +99|25 +99|57 +99|39 +99|93 +99|37 +99|12 +99|75 +58|79 +58|41 +58|35 +58|17 +58|23 +58|57 +58|24 +58|84 +31|41 +31|54 +31|39 +31|52 +31|17 +31|37 +31|75 +37|69 +37|93 +37|83 +37|25 +37|77 +37|27 +79|25 +79|83 +79|93 +79|23 +79|74 +74|14 +74|99 +74|53 +74|16 +22|93 +22|21 +22|39 +84|57 +84|83 +12|89 + +13,42,54,22,37 +19,24,74,71,22,53,16,94,95,33,27,15,54,14,87,31,13,17,89 +89,15,74,16,27,14,33,58,13,42,24,54,95,22,52 +15,94,74,19,92,58,13,42,24,54,95,17,53,22,21 +78,69,59,77,47,25,12,83,89,94,74,16,19,27,87,14,71,92,28,33,58,13,42 +67,86,41,69,35,59,39,83,47,37,25,57,97 +99,39,57,86,23,97,37,75,41,46,67,69,59,83,89 +81,77,47,12,94,16,19,28,58 +53,22,21,35,99,39,79,57,86,23,97,93,75,46,67,78,77 +25,12,83,89,15,74,19,27,87,14,92,33,58 +22,53,57,52,95,79,24,97,39,81,46,41,84,93,35,54,23,67,21,17,99,37,75 +81,24,57,17,54,23,35,99,37,31,22,84,46 +95,53,22,52,39,23,97,37,93,75,46,81,67,78,69 +52,21,35,57,86,23,97,37,75,46,67,78,69,47,25 +81,59,78,75,89,57,67,25,83,12,41,69,16,74,97,94,93,37,15,23,46,77,86 +12,83,23,69,75,99,93,97,41,67,89,79,86 +21,79,24,22,39,28,95,13,33,35,86,84,31 +14,28,58,95,52,21,35,39,86 +54,46,84,57,78,21,37,39,99 +39,46,67,77,81,59,57,97,69,37,99,84,53,52,41,78,22,79,35,86,75,23,21 +92,28,33,58,42,31,24,54,95,17,52,21,35,99,84,39,79 +16,87,13,31,33,19,21,17,52,42,28,15,71,74,58,94,54,95,24 +37,93,81,67,59,74,16,27,87 +87,58,31,54,99,84,57 +39,42,53,93,31 +21,57,23,22,92,39,52,97,79,31,28,17,33,86,42 +37,77,84,79,52,22,67,97,78,47,75,93,39,57,81 +95,52,35,99,84,79,97,41,46,81,67,78,69 +67,46,81,78,47,94,79,15,93,25,97,12,23,39,75 +23,97,37,93,75,41,46,81,67,78,59,77,47,25,12,83,89,15,94,74,16,19,27 +46,39,59,47,75,69,86,57,21,37,12,41,23,35,99,77,81,79,97,78,25 +19,13,16,74,95,92,27,89,14,15,31,71,58,17,87,54,12,33,42,83,28 +13,12,19,92,16,94,83,47,87,59,42,27,78 +54,17,53,52,79,86,97,93,41,46,78 +97,84,69,86,81,23,21,52,47,37,78,75,59,41,22,57,67,99,35,39,46 +28,53,13,95,99,17,37 +94,74,16,19,27,87,14,71,92,33,58,13,42,31,54,95,53,22,52,21,35 +12,15,74,71,58,87,28,16,27,53,92 +93,94,12,71,87,83,77,47,41 +92,99,14,21,16,54,19,42,71,87,24,84,35,52,28,53,95,27,13,58,17,33,31 +46,47,93,16,59,97,83,27,37,69,87,25,12,78,67 +71,92,33,42,24,17,52,21,84,79,86 +22,86,69,59,47,41,75 +97,86,83,81,67,12,77,23,59,37,89,25,47 +19,27,28,42,52,99,39 +27,87,14,71,92,28,33,58,13,42,31,24,54,95,17,53,22,21,35,99,84,39,79 +15,94,74,16,19,27,14,71,92,28,33,58,13,42,31,24,54,95,17,53,22,52,21 +21,39,23,46,77,47,12 +93,75,81,67,78,69,77,47,25,12,83,89,15,94,74,19,27,87,14 +39,79,57,86,23,97,93,75,41,46,67,78,69,59,77,25,83,89,94 +52,58,19,28,27,94,95,92,89,13,31,87,33,42,15 +46,78,93,17,52,21,95,41,57 +19,71,33,54,95,53,84 +67,78,69,59,47,12,83,89,15,94,74,19,28,58,13 +22,52,35,99,79,97,37,75,41,78,59 +99,84,39,79,57,86,23,97,37,93,75,41,46,81,67,78,69,59,77,47,25,83,89 +78,69,59,47,25,12,83,89,15,74,16,27,87,14,92,28,33,58,42 +57,86,23,37,93,75,41,46,81,67,78,59,77,47,25,12,83,89,15,74,16 +81,39,95,21,99,67,78,41,23 +37,12,59,83,79,75,99,57,41,86,25,77,78,35,67 +15,28,95,42,83,33,14,19,24,12,53 +84,81,41,86,97,67,57,35,46,52,23 +23,33,17,84,22,71,52,42,39 +17,53,22,52,35,84,39,79,57,86,23,97,37,93,75,81,67,78,59 +46,78,81,41,71,89,19,67,92,28,15,27,77,47,94,14,25,16,12 +37,93,46,78,77,87,14 +92,16,46,69,14,89,87,74,81,19,15,47,94,12,67,25,83,33,28,78,27,77,59 +86,79,52,21,69,78,67,39,81,23,99,77,84,41,59,35,47,37,97,46,93,25,57 +94,74,19,27,87,14,28,33,58,13,42,31,24,54,95,17,53,22,52,21,35 +94,74,19,27,14,71,92,28,33,58,42,24,54,95,17,53,22,52,21 +15,14,25,31,12,94,33,16,27,69,77,42,13,47,59,74,28 +67,78,69,25,89,94,19,27,87,92,28,33,13 +41,46,67,47,83,19,27 +95,17,53,22,52,21,35,99,79,57,86,23,97,37,93,75,41,46,81,78,69 +89,87,92,58,31,71,33,42,15,25,28,24,59 +25,83,74,16,71,28,13,42,24,95,17 +52,58,92,87,71,24,39,17,84,19,28,13,21,14,35,99,54,31,95 +97,37,75,81,67,78,69,59,47,12,94,16,87 +92,28,33,13,42,24,95,17,53,22,52,21,35,99,39,57,86,23,97 +71,28,33,58,13,31,24,54,17,53,52,99,39,79,57,86,23 +23,97,39,58,52 +21,46,37,75,79,52,84,81,23,35,31 +31,24,54,21,35,99,39,79,86,23,97,37,41,46,81 +77,47,12,89,15,94,74,16,19,14,92,28,33,58,42,24,54 +93,75,78,69,77 +33,59,94,19,92,89,47,27,13 +83,89,94,74,27,71,58,42,17,53,22 +19,25,89,14,92,15,42,83,24,74,59,33,28,77,31,47,13,94,12,27,16 +79,23,37,31,86,17,95,39,75,35,58,54,84,42,97,22,53,21,57 +92,22,52,15,54,71,19,27,16,31,13 +12,83,74,28,33,58,53 +33,13,42,31,24,95,53,52,21,35,99,84,79,23,97,37,93 +77,83,15,27,28,33,42 +15,89,59,94,77,83,47,75,46,37,41,69,12,74,19,27,93,16,97,78,81,67,23 +22,21,35,99,39,23,37,41,46,81,67,78,69,77,47 +69,47,25,12,94,74,16,19,87,14,71,28,33,58,13,42,31 +39,86,97,93,41,46,69,77,47,12,89,15,94 +41,46,15,94,71,92,28 +87,78,27,94,12,14,16,33,28,47,19,15,69 +84,39,57,23,97,37,93,41,81,67,78,59,47,25,15 +79,89,77,67,74,94,12 +25,89,27,71,42,54,17 +24,21,31,87,28,33,71,52,74,27,17,22,42 +12,15,27,14,71,92,42,31,53 +93,23,46,75,37,67,17,99,84,35,57,52,95,86,54,53,81,79,78,41,97,21,39 +89,59,19,31,87,16,28,12,13,47,27,94,24,42,14,74,25 +84,57,59,69,93,23,21,12,78,41,25,77,35,99,75,47,67,97,86,37,39,81,46 +16,19,27,87,14,92,28,33,58,13,42,31,24,95,17,53,22 +77,15,74,16,14,92,28 +69,59,25,12,83,89,15,16,19,27,87,71,28,13,31 +84,37,47,59,93,46,41,69,25,57,21,78,12,75,77,81,79,97,23,39,67 +92,58,31,24,95,52,21,84,97 +16,37,41,14,27,59,77,81,75,83,74,47,15 +78,12,27,71,58,14,89,67,19,83,16,15,59,28,69,47,92,81,94 +57,86,23,97,37,93,75,41,46,81,67,78,69,59,77,47,12,83,89,15,94,74,16 +46,16,15,23,67,89,94,27,77,69,97,47,81 +52,84,79,57,97,67,25 +35,99,84,79,86,41,46,81,67,78,59,77,47,12,83 +14,71,33,42,54,21,99,84,86 +69,77,81,57,21,59,53,52,86 +16,27,14,71,92,58,13,42,22,21,84 +93,41,81,67,78,69,77,25,12,83,89,15,74,16,19,27,87,14,71 +84,17,31,92,99 +35,81,84,57,46,79,97,39,99,86,54,95,21,17,23,67,52,53,37,78,41,22,75 +22,52,99,39,79,57,86,23,97,37,93,75,41,46,67,78,69,59,47 +13,42,31,54,95,17,53,22,52,21,35,99,84,39,79,57,86,37,41 +74,16,19,71,92,28,33,58,13,42,31,24,54,95,17,52,21,35,99 +87,71,92,58,13,42,31,24,54,95,53,22,52,21,35,99,84,79,57 +78,69,59,77,83,89,15,94,16,19,87,14,71,28,58,13,42 +75,46,81,78,77,12,83,89,15,94,74,16,19,27,14 +54,94,13,53,83,87,12,71,33,15,14,58,74,24,17,42,95 +17,53,22,21,99,79,23,97,37,75,46,81,78,69,59 +41,22,93,99,84,23,67,46,39,79,81,75,37,35,57,17,24 +81,23,41,37,86,75,83,79,77,89,15,39,25,97,93 +17,52,54,37,75,41,97,42,46 +15,83,87,89,97,69,77,37,25 +86,22,53,75,93,95,99,31,35,23,46,37,81,54,21,57,79,97,52,39,84,41,17 +86,39,79,54,21,31,22,99,42,24,13,58,35,84,52,37,33,23,95 +24,84,79,41,46,81,67 +83,94,92,42,54 +24,54,53,52,21,99,84,79,57,86,97,41,67 +33,58,54,83,17,31,28,71,14,25,13 +13,42,31,54,95,53,52,21,35,99,39,79,86,23,37,75,41 +99,84,39,57,86,23,37,75,46,78,69,59,77,25,12 +86,23,97,93,75,41,78,59,77,47,12,83,15,16,19 +77,47,83,89,15,94,74,16,19,27,14,71,92,28,33,58,13,42,31,24,54 +47,25,12,83,89,94,74,16,19,27,87,14,71,92,28,58,13,42,31,24,95 +74,16,19,87,14,71,33,58,13,42,24,54,95,17,53,52,21,35,99 +46,87,14,92,15,59,69,33,77 +22,16,89,95,54,27,94,17,71,53,24,87,52,33,15 +75,41,46,81,69,59,77,47,25,12,89,15,94,74,16,19,27,71,92 +13,28,69,78,42,94,47,83,25 +86,54,33,42,35,22,71,57,17,13,14 +95,21,24,39,53,81,67,84,75,22,79,52,46 +75,86,93,78,69,46,81,22,23,97,39,52,35,95,57,21,79 +21,35,99,84,39,79,57,86,23,37,93,75,41,81,78,69,59,77,47,25,12 +58,24,15,95,22,28,54,92,42,52,87,53,16,14,33,13,94,19,27 +83,47,84,59,46 +14,71,92,28,58,13,42,24,54,95,17,22,52,21,99,84,39,79,86 +71,92,33,58,54,95,22,52,21,84,79 +58,13,42,54,95,53,22,21,35,79,23,93,75 +84,39,79,57,86,23,97,37,93,75,41,46,81,78,69,59,77,47,25,12,83 +67,84,46,39,78,41,47,79,23,86,75,59,57 +14,19,59,25,67,28,94,77,81,16,78,83,89,74,41,69,15,92,87,71,27,12,47 +52,35,99,39,79,86,97,37,93,75,41,81,78,69,59,47,25 +37,93,75,41,46,81,67,78,69,77,47,25,12,83,15,74,16,19,27,87,14 +92,33,58,13,95,17,53,22,52,21,84,39,79,57,97 +28,94,24,54,16,83,22,17,74 +13,57,86,21,39,42,53,24,41 +83,25,86,94,23,12,15,75,41,77,69,78,89,81,79,37,74 +53,22,37,93,75,46,81,67,78,69,77 +92,87,17,52,27,54,53,95,71,24,74,21,16,33,58,94,14,31,22,19,35,28,13 +31,79,22,17,53,41,42,75,84,86,97,54,93,23,21 +79,52,84,53,92,95,71,21,28,54,39,13,31,17,86 +27,42,25,28,95,92,47,33,16,71,12,87,58 +52,21,99,84,39,86,37,75,41,46,81,67,78,59,25 +27,12,24,19,92,16,74,14,31,94,87,54,47,15,95,33,83,42,89 +74,92,28,33,58,54,22 +53,35,84,57,86,97,93,75,41,81,78,59,77 +22,52,99,84,23,37,81 +31,54,81,79,22,97,21 +42,47,13,25,74,15,58,69,19 +92,33,58,42,31,24,17,53,22,35,99,84,39,86,97 +31,24,95,17,22,21,23,97,37 +41,75,78,81,99,21,52,79,53,69,59,77,39 +57,93,59,99,84,17,35,69,21 +92,31,25,24,58,59,71,87,27,94,16,14,15,28,12,47,89,19,33,13,77,74,42 +75,41,46,81,67,78,69,59,77,47,25,12,83,89,15,94,74,19,27,87,14,71,92 +75,46,67,78,69,59,77,47,83,89,15,16,27,87,14,71,92 +21,35,99,84,39,79,57,86,23,93,75,41,46,81,67,78,69,59,47,25,12 +35,86,75,46,67 +81,94,78,27,23,97,47,77,16,19,89 +81,67,78,69,59,77,47,25,83,89,15,94,74,16,27,87,14,71,92,33,58 +47,71,94,92,87,25,12,67,27,15,77,14,83,59,16,78,74,75,81,69,41,19,89 +35,99,84,79,97,37,93,75,78,69,47,12,83 +23,81,67,78,25,12,27 +86,17,54,35,81,52,31 +99,24,14,39,79,21,17,87,54,53,92,58,52,28,27 +52,21,35,99,39,79,93,69,25 +41,27,97,89,94,47,93,19,81,16,77,75,74,25,12,67,83,69,23,78,37 \ No newline at end of file diff --git a/src/test/kotlin/SolutionsTest.kt b/src/test/kotlin/SolutionsTest.kt index e8a23e4..34e6fbd 100644 --- a/src/test/kotlin/SolutionsTest.kt +++ b/src/test/kotlin/SolutionsTest.kt @@ -1,7 +1,4 @@ -import days.Day1 -import days.Day2 -import days.Day3 -import days.Day4 +import days.* import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.DynamicTest @@ -16,6 +13,7 @@ class SolutionsTest { Day2(InputReader.getInputAsList(2)) to Pair(559, 601), Day3(InputReader.getInputAsList(3)) to Pair(178886550, 87163705), Day4(InputReader.getInputAsList(4)) to Pair(2414, 1871), + Day5(InputReader.getInputAsList(5)) to Pair(6041, 4884), ) .map { (day, answers) -> DynamicTest.dynamicTest("${day.javaClass.simpleName} -> ${answers.first} / ${answers.second}") { diff --git a/src/test/kotlin/days/Day5Test.kt b/src/test/kotlin/days/Day5Test.kt new file mode 100644 index 0000000..350253e --- /dev/null +++ b/src/test/kotlin/days/Day5Test.kt @@ -0,0 +1,59 @@ +package days + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +@DisplayName("Day 5") +class Day5Test { + val rules = """ + 47|53 + 97|13 + 97|61 + 97|47 + 75|29 + 61|13 + 75|53 + 29|13 + 97|29 + 53|29 + 61|53 + 97|53 + 61|29 + 47|13 + 75|47 + 97|75 + 47|61 + 75|61 + 47|29 + 75|13 + 53|13 + + 75,47,61,53,29 + 97,61,53,29,13 + 75,29,13 + 75,97,47,61,53 + 61,13,29 + 97,13,75,29,47""".trimIndent().lines() + + @Nested + @DisplayName("Part 1") + inner class Part1 { + + @Test + fun `What do you get if you add up the middle page number from those correctly-ordered updates`() { + assertThat(Day5(rules).partOne()).isEqualTo(143) + } + } + + @Nested + @DisplayName("Part 2") + inner class Part2 { + @Test + fun `What do you get if you add up the middle page numbers after correctly ordering just those updates`() { + assertThat(Day5(rules).partTwo()).isEqualTo(123) + } + + } +} \ No newline at end of file