diff --git a/src/main/kotlin/com/basdado/adventofcode/day16/Day16.kt b/src/main/kotlin/com/basdado/adventofcode/day16/Day16.kt new file mode 100644 index 0000000..812cdde --- /dev/null +++ b/src/main/kotlin/com/basdado/adventofcode/day16/Day16.kt @@ -0,0 +1,64 @@ +package com.basdado.adventofcode.day16 + +import com.basdado.adventofcode.line +import java.util.stream.IntStream +import kotlin.math.abs + +const val DAY16_INPUT_PATH = "/day/16/input.txt" + +fun main() { + Day16.puzzle1() + Day16.puzzle2() + +} + +object Day16 { + + val pattern = intArrayOf(0, 1, 0, -1) + + fun puzzle1() { + val input = line(DAY16_INPUT_PATH) + .chars().map { it - '0'.toInt() } + .toArray() + + var code = input + IntStream.range(0, 100) + .peek { phase -> + code = IntStream.range(0, code.size).parallel() + .map { output -> lastDigit(IntStream.range(0, code.size) + .map { i -> code[i] * patternAt(output, i) } + .sum())} + .toArray() + } +// .peek { phase -> println("Phase $phase: ${code.joinToString()} ") } + .skip(99) + .findFirst()!! + + println(code.joinToString("")) + } + + fun puzzle2() { + val input = line(DAY16_INPUT_PATH).repeat(10) + .chars().map { it - '0'.toInt() } + .toArray() + + var code = input + IntStream.range(0, 100) + .peek { phase -> + code = IntStream.range(0, code.size).parallel() + .map { output -> lastDigit(IntStream.range(0, code.size) + .map { i -> code[i] * patternAt(output, i) } + .sum())} + .toArray() + } +// .peek { phase -> println("Phase $phase: ${code.joinToString()} ") } + .skip(99) + .findFirst()!! + + println(code.joinToString("")) + } + + + fun patternAt(output: Int, i: Int) = pattern[((i + 1) / (output + 1)) % pattern.size] + fun lastDigit(n: Int) = abs(n % 10) +} \ No newline at end of file diff --git a/src/main/resources/day/16/ex1.txt b/src/main/resources/day/16/ex1.txt new file mode 100644 index 0000000..e9a9ea1 --- /dev/null +++ b/src/main/resources/day/16/ex1.txt @@ -0,0 +1 @@ +12345678 \ No newline at end of file diff --git a/src/main/resources/day/16/input.txt b/src/main/resources/day/16/input.txt new file mode 100644 index 0000000..6603f5b --- /dev/null +++ b/src/main/resources/day/16/input.txt @@ -0,0 +1 @@ +59727310424796235189476878806940387435291429226818921130171187957262146115559932358924341808253400617220924411865224341744614706346865536561788244183609411225788501102400269978290670307147139438239865673058478091682748114942700860895620690690625512670966265975462089087644554004423208369517716075591723905075838513598360188150158989179151879406086757964381549720210763972463291801513250953430219653258827586382953297392567981587028568433943223260723561880121205475323894070000380258122357270847092900809245133752093782889315244091880516672127950518799757198383131025701009960944008679555864631340867924665650332161673274408001712152664733237178121872 \ No newline at end of file