From e54fa1b1872e3fd04d9c81599586c20de2757e15 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Tue, 22 Dec 2020 06:12:28 +0100 Subject: [PATCH] Day22 --- days/src/main/kotlin/Day21.kt | 2 +- days/src/main/kotlin/Day22.kt | 81 +++++++++++++++++++++++++++++++ days/src/main/resources/day22.txt | 53 ++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 days/src/main/kotlin/Day22.kt create mode 100644 days/src/main/resources/day22.txt diff --git a/days/src/main/kotlin/Day21.kt b/days/src/main/kotlin/Day21.kt index 312fa70..07aad7e 100644 --- a/days/src/main/kotlin/Day21.kt +++ b/days/src/main/kotlin/Day21.kt @@ -25,7 +25,7 @@ class Day21(@Lines val input: Input>) { val occurrences = Bags.mutable.empty() foods.forEach { (ingredients) -> occurrences.addAll(ingredients) } return allIngredients.filter { !dangerousIngredients.containsValue(it) } - .map { occurrences.count { it == it } } + .map { ingredient -> occurrences.count { it == ingredient } } .sum() } diff --git a/days/src/main/kotlin/Day22.kt b/days/src/main/kotlin/Day22.kt new file mode 100644 index 0000000..41bbbc2 --- /dev/null +++ b/days/src/main/kotlin/Day22.kt @@ -0,0 +1,81 @@ +package be.vandewalleh.aoc.days + +import be.vandewalleh.aoc.utils.input.Day +import be.vandewalleh.aoc.utils.input.Groups +import be.vandewalleh.aoc.utils.input.Input +import be.vandewalleh.aoc.utils.input.createDay + +data class PlayedGame(val a: List, val b: List) + +@Day(22) +class Day22(@Groups val input: Input>>) { + private val one = input.value[0].drop(1).map { it.toInt() } + private val two = input.value[1].drop(1).map { it.toInt() } + + fun part1(): Long { + val oneDeque = ArrayDeque().apply { one.forEach { addFirst(it) } } + val twoDeque = ArrayDeque().apply { two.forEach { addFirst(it) } } + + while (oneDeque.isNotEmpty() && twoDeque.isNotEmpty()) { + val a = oneDeque.removeLast() + val b = twoDeque.removeLast() + + if (a > b) { + oneDeque.addFirst(a) + oneDeque.addFirst(b) + } else { + twoDeque.addFirst(b) + twoDeque.addFirst(a) + } + } + + val deque = if (oneDeque.isEmpty()) twoDeque else oneDeque + return deque.mapIndexed { index, value -> (index + 1).toLong() * value.toLong() }.sum() + } + + fun part2(): Long { + val oneDeque = ArrayDeque().apply { one.forEach { addFirst(it) } } + val twoDeque = ArrayDeque().apply { two.forEach { addFirst(it) } } + + val winner = playGame(oneDeque, twoDeque) + val deque = if (winner == 1) oneDeque else twoDeque + return deque.mapIndexed { index, value -> (index + 1).toLong() * value.toLong() }.sum() + } + + private fun playGame(one: ArrayDeque, two: ArrayDeque): Int { + val playedGames = mutableSetOf() + + while (one.isNotEmpty() && two.isNotEmpty()) { + if (!playedGames.add(PlayedGame(one.toList(), two.toList()))) return 1 + + val a = one.removeLast() + val b = two.removeLast() + + val winner = when { + one.size >= a && two.size >= b -> playGame( + ArrayDeque(one.drop(one.size - a)), + ArrayDeque(two.drop(two.size - b)) + ) + a > b -> 1 + else -> 2 + } + + if (winner == 1) { + one.addFirst(a) + one.addFirst(b) + } else { + two.addFirst(b) + two.addFirst(a) + } + + } + + return if (one.isEmpty()) 2 else 1 + } +} + +fun main() = with(createDay()) { + println(part1()) + println(part2()) +} + diff --git a/days/src/main/resources/day22.txt b/days/src/main/resources/day22.txt new file mode 100644 index 0000000..161b046 --- /dev/null +++ b/days/src/main/resources/day22.txt @@ -0,0 +1,53 @@ +Player 1: +21 +48 +44 +31 +29 +5 +23 +11 +12 +27 +49 +22 +18 +7 +15 +20 +2 +45 +14 +17 +40 +35 +6 +24 +41 + +Player 2: +47 +1 +10 +16 +28 +37 +8 +26 +46 +25 +3 +9 +34 +50 +32 +36 +43 +4 +42 +33 +19 +13 +38 +39 +30