From 34259208d562960601c91744983bf662820648f3 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 10 Dec 2020 08:47:44 +0100 Subject: [PATCH] Day10 --- days/src/main/kotlin/Day10.kt | 50 +++++++++++++++++ days/src/main/resources/day10.txt | 93 +++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 days/src/main/kotlin/Day10.kt create mode 100644 days/src/main/resources/day10.txt diff --git a/days/src/main/kotlin/Day10.kt b/days/src/main/kotlin/Day10.kt new file mode 100644 index 0000000..0208091 --- /dev/null +++ b/days/src/main/kotlin/Day10.kt @@ -0,0 +1,50 @@ +package be.vandewalleh.aoc.days + +import be.vandewalleh.aoc.utils.input.Day +import be.vandewalleh.aoc.utils.input.Input +import be.vandewalleh.aoc.utils.input.Lines +import be.vandewalleh.aoc.utils.input.createDay +import org.eclipse.collections.api.list.primitive.MutableIntList +import org.eclipse.collections.impl.factory.primitive.IntLists +import org.eclipse.collections.impl.factory.primitive.IntLongMaps + +@Day(10) +class Day10(@Lines val input: Input) { + + fun part1(): Int { + val sorted = IntLists.mutable.of(0, *input.value).apply { + sortThis() + add(last + 3) + }.toArray().toList() + + var ones = 0 + var threes = 0 + + sorted.zipWithNext().forEach { (a, b) -> + if (a + 1 == b) ones++ + else if (a + 3 == b) threes++ + } + + return ones * threes + } + + fun part2(): Long { + val sorted: MutableIntList = IntLists.mutable.of(*input.value).apply { sortThis() } + + val map = IntLongMaps.mutable.empty().apply { + put(0, 1L) + } + + sorted.forEach { i -> + map.put(i, map.get(i - 1) + map.get(i - 2) + map.get(i - 3)) + } + + return map.get(sorted.last) + } + +} + +fun main() = with(createDay()) { + println(part1()) + println(part2()) +} diff --git a/days/src/main/resources/day10.txt b/days/src/main/resources/day10.txt new file mode 100644 index 0000000..e4c0d57 --- /dev/null +++ b/days/src/main/resources/day10.txt @@ -0,0 +1,93 @@ +47 +99 +115 +65 +10 +55 +19 +73 +80 +100 +71 +110 +64 +135 +49 +3 +1 +98 +132 +2 +38 +118 +66 +116 +104 +87 +79 +114 +40 +37 +44 +97 +4 +140 +60 +86 +56 +133 +7 +146 +85 +111 +134 +53 +121 +77 +117 +21 +12 +81 +145 +129 +107 +93 +22 +48 +11 +54 +92 +78 +67 +20 +138 +125 +57 +96 +26 +147 +124 +34 +74 +143 +13 +28 +126 +50 +29 +70 +39 +63 +41 +91 +32 +84 +144 +27 +139 +33 +88 +72 +23 +103 +16