Day16: Puzzle 1 is done, pretty easy, don't know how to do part 2 yet

This commit is contained in:
2019-12-17 01:13:50 +01:00
parent c90804bc0a
commit a491a69ee6
3 changed files with 66 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -0,0 +1 @@
12345678

View File

@@ -0,0 +1 @@
59727310424796235189476878806940387435291429226818921130171187957262146115559932358924341808253400617220924411865224341744614706346865536561788244183609411225788501102400269978290670307147139438239865673058478091682748114942700860895620690690625512670966265975462089087644554004423208369517716075591723905075838513598360188150158989179151879406086757964381549720210763972463291801513250953430219653258827586382953297392567981587028568433943223260723561880121205475323894070000380258122357270847092900809245133752093782889315244091880516672127950518799757198383131025701009960944008679555864631340867924665650332161673274408001712152664733237178121872