adamratzman
12/15/2020, 4:55 AMDavid Whittaker
12/15/2020, 6:03 AMprivate fun runProgram() {
val last = mutableMapOf<Int,Int>()
val previous = mutableMapOf<Int,Int>()
last[2] = 1
last[0] = 2
last[1] = 3
last[7] = 4
last[4] = 5
last[14] = 6
last[18] = 7
var wasfirst = true
var current = 18
var pos = 8
while (pos <= 30000000) {
if (wasfirst) {
last[current] = pos - 1
current = 0
if (last.containsKey(0)) {
previous[0] = last[0]!!
}
last[0] = pos
wasfirst = false
} else {
val d = last[current]!! - previous[current]!!
if (last.containsKey(d)) {
previous[d] = last[d]!!
} else {
wasfirst = true
}
current = d
last[d] = pos
}
pos++
}
println("Puzzla answer: ${current}")
}
adamratzman
12/15/2020, 6:04 AMDavid Whittaker
12/15/2020, 6:06 AMd
in there - all my solutions start with single letter variables to save time. Forgot to change that one.adamratzman
12/15/2020, 6:58 AMpackage aoc2020
import common.*
class Aoc2020Day15 : Problem(2020, 15) {
val numbers = input.split(",").map { it.toInt() }
val numberGenerator = sequence {
val memo = mutableMapOf<Int, MutableList<Int>>()
numbers.forEachIndexed { index, i -> memo.insertOrAppend(i, index); yield(i) }
var current = numbers.last()
var i = numbers.size
while (true) {
val currentIndices = memo.getValue(current)
val difference = if (currentIndices.size != 1) currentIndices[currentIndices.lastIndex] - currentIndices[currentIndices.lastIndex - 1]
else 0
yield(difference)
memo.insertOrAppend(difference, i)
memo[difference] = memo[difference]!!.takeLast(2).toMutableList()
current = difference
i++
}
}
override fun solvePart1(): Any {
return numberGenerator.take(2020).last()
}
override fun solvePart2(): Any {
return numberGenerator.take(30000000).last()
}
}
fun main() {
Aoc2020Day15().solve()
}
andyb
12/15/2020, 9:46 AMandyb
12/15/2020, 10:04 AMval num = counter - previousNumbers.getOrDefault(speak, counter)
Jakub Gwóźdź
12/15/2020, 12:09 PMephemient
12/15/2020, 1:47 PMephemient
12/15/2020, 1:49 PMephemient
12/15/2020, 1:50 PMNir
12/15/2020, 2:58 PMval input = listOf(8,0,17,4,1,12)
fun run(lastTurn: Int): Int {
val state = mutableMapOf<Int, Int>().also { map ->
input.subList(0, input.size-1).withIndex().associateTo(map) { it.value to it.index+1 }
}
var lastNumber = input.last()
for (turn in (input.size) until lastTurn) {
val curNumber = turn - state.getOrPut(lastNumber) { turn }
lastNumber = curNumber
}
return lastNumber
}
fun part1() = run(2020)
fun part2() = run(30000000)
todd.ginsberg
12/15/2020, 4:07 PMclass Day15(input: String) {
private val startingNumbers = input.split(",").map { it.toInt() }
fun solve(turns: Int): Int =
memoryGame().drop(turns-1).first()
private fun memoryGame(): Sequence<Int> = sequence {
yieldAll(startingNumbers)
val memory = startingNumbers.mapIndexed { index, i -> i to index }.toMap().toMutableMap()
var turns = startingNumbers.size
var sayNext = 0
while(true) {
yield(sayNext)
val lastTimeSpoken = memory[sayNext] ?: turns
memory[sayNext] = turns
sayNext = turns - lastTimeSpoken
turns++
}
}
}
todd.ginsberg
12/15/2020, 4:08 PMFredrik Rødland
12/15/2020, 4:42 PMbjonnh
12/15/2020, 7:44 PMbjonnh
12/15/2020, 7:44 PMbjonnh
12/15/2020, 7:44 PMNir
12/15/2020, 7:45 PMNir
12/15/2020, 7:45 PMlastNum = cache[lastNum]?.let { pos - it } ?: 0L
cache[oldNum] = pos
Nir
12/15/2020, 7:45 PMgetOrPut
call insteadNir
12/15/2020, 7:46 PMbjonnh
12/15/2020, 8:53 PMbjonnh
12/15/2020, 8:53 PMbjonnh
12/15/2020, 8:53 PMNir
12/15/2020, 8:54 PMNir
12/15/2020, 8:58 PMNir
12/15/2020, 8:59 PMbjonnh
12/15/2020, 8:59 PMbjonnh
12/15/2020, 8:59 PMbjonnh
12/15/2020, 9:00 PMNir
12/15/2020, 9:01 PMNir
12/15/2020, 9:01 PMbjonnh
12/15/2020, 9:01 PMNir
12/15/2020, 9:01 PMbjonnh
12/15/2020, 9:02 PMNir
12/15/2020, 9:02 PMNir
12/15/2020, 9:02 PMNir
12/15/2020, 9:02 PMbjonnh
12/15/2020, 9:02 PMbjonnh
12/15/2020, 9:03 PMNir
12/15/2020, 9:03 PMNir
12/15/2020, 9:04 PMNir
12/15/2020, 9:05 PMNir
12/15/2020, 9:05 PMbjonnh
12/15/2020, 9:06 PMbjonnh
12/15/2020, 9:07 PMbjonnh
12/15/2020, 9:09 PMbjonnh
12/15/2020, 9:09 PMbjonnh
12/15/2020, 9:09 PMNir
12/15/2020, 9:10 PMturn - (state.put(lastNumber, turn) ?: turn)
Nir
12/15/2020, 9:10 PMstate.put(lastNumber, turn)?.let { turn - it } ?: 0
bjonnh
12/15/2020, 9:11 PMNir
12/15/2020, 9:12 PMbjonnh
12/15/2020, 9:13 PMbjonnh
12/15/2020, 9:13 PMNir
12/15/2020, 9:13 PMNir
12/15/2020, 9:14 PMassociateTo
there unless there's another function that does thatbjonnh
12/15/2020, 9:14 PMNir
12/15/2020, 9:14 PMNir
12/15/2020, 9:14 PMNir
12/15/2020, 9:15 PMbjonnh
12/15/2020, 9:15 PMfun
val cache = mutableMapOf<Int, Int>().also { it.putAll(list.mapIndexed { idx, it -> it to idx }) }
val num = turns - 1
if (num < list.size) return list[num]
var lastNum = list.last()
(list.size - 1 until num).forEach { pos -> lastNum = pos - (cache.put(lastNum, pos) ?: pos) }
return lastNum
}
fun main() {
val input = listOf(9, 3, 1, 0, 8, 4)
println(finalNumCached(input, 2020))
println(finalNumCached(input, 30000000))
}
bjonnh
12/15/2020, 9:15 PMNir
12/15/2020, 9:17 PMNir
12/15/2020, 9:17 PMfun MutableMap<K, V>.foo(k: K, v: V) = put(k, v) ?: v
bjonnh
12/15/2020, 9:20 PMbjonnh
12/15/2020, 9:25 PMNir
12/15/2020, 9:33 PMNir
12/15/2020, 9:33 PMNir
12/15/2020, 9:33 PMNir
12/15/2020, 9:33 PMbjonnh
12/15/2020, 9:35 PMbjonnh
12/15/2020, 9:35 PMNir
12/15/2020, 9:36 PMput
already exists it makes sensebjonnh
12/15/2020, 9:42 PMNir
12/15/2020, 9:45 PMNir
12/15/2020, 9:45 PMNir
12/15/2020, 9:45 PMNir
12/15/2020, 9:46 PMfun <K, V> MutableMap<K, V>.update(k: K, transform: (V?) -> V) = set(k, transform(get(k)))
bjonnh
12/15/2020, 9:47 PMNir
12/15/2020, 9:47 PMfoo.update(key) { (it ?: 0) + 1}
Nir
12/15/2020, 9:56 PMbjonnh
12/15/2020, 9:57 PMbjonnh
12/15/2020, 9:57 PMbjonnh
12/15/2020, 9:57 PMNir
12/15/2020, 9:58 PMNir
12/15/2020, 9:58 PMbjonnh
12/15/2020, 9:58 PMbjonnh
12/15/2020, 10:00 PMbjonnh
12/15/2020, 10:01 PMbjonnh
12/15/2020, 10:12 PMbjonnh
12/15/2020, 10:12 PMNir
12/15/2020, 10:13 PMNir
12/15/2020, 10:14 PMNir
12/15/2020, 10:14 PMbjonnh
12/15/2020, 10:28 PMbjonnh
12/15/2020, 10:28 PMNir
12/15/2020, 10:29 PMbjonnh
12/15/2020, 10:30 PMbjonnh
12/15/2020, 10:33 PMbjonnh
12/15/2020, 10:33 PMbjonnh
12/15/2020, 10:33 PMbjonnh
12/15/2020, 10:33 PMbjonnh
12/15/2020, 10:38 PMbjonnh
12/16/2020, 12:08 AMNir
12/16/2020, 12:25 AMbjonnh
12/16/2020, 12:30 AMbjonnh
12/16/2020, 12:30 AMNir
12/16/2020, 12:38 AMNir
12/16/2020, 12:39 AMbjonnh
12/16/2020, 12:40 AMbjonnh
12/16/2020, 12:41 AMbjonnh
12/16/2020, 12:41 AMbjonnh
12/16/2020, 12:41 AMNir
12/16/2020, 12:47 AMNir
12/16/2020, 12:47 AMNir
12/16/2020, 12:47 AMNir
12/16/2020, 12:48 AMNir
12/16/2020, 12:48 AMbjonnh
12/16/2020, 12:49 AMbjonnh
12/16/2020, 12:49 AMbjonnh
12/16/2020, 12:49 AMephemient
12/16/2020, 12:23 PMephemient
12/16/2020, 12:24 PMperf stat
to measure a bare-bones C solution I whipped up, I'm pretty sure it's not possible for me to do better than that on this hardwareephemient
12/16/2020, 12:26 PMbjonnh
12/16/2020, 5:16 PMbjonnh
12/16/2020, 5:16 PMbjonnh
12/16/2020, 5:17 PMephemient
12/16/2020, 5:18 PMbjonnh
12/16/2020, 5:19 PMbjonnh
12/16/2020, 5:20 PMbjonnh
12/16/2020, 5:20 PMbjonnh
12/16/2020, 5:20 PMbjonnh
12/16/2020, 5:21 PMephemient
12/16/2020, 5:22 PMperf stat -d
will give some more detail (such as L1 hit rates)bjonnh
12/16/2020, 5:23 PMbjonnh
12/16/2020, 5:24 PMbjonnh
12/16/2020, 5:24 PMephemient
12/16/2020, 5:24 PMephemient
12/16/2020, 5:25 PMephemient
12/16/2020, 5:25 PMbjonnh
12/16/2020, 5:26 PMbjonnh
12/16/2020, 5:26 PMbjonnh
12/16/2020, 5:27 PMbjonnh
12/16/2020, 5:28 PMbjonnh
12/16/2020, 5:28 PMbjonnh
12/16/2020, 5:28 PMbjonnh
12/16/2020, 5:28 PMephemient
12/16/2020, 5:29 PMephemient
12/16/2020, 5:29 PMbjonnh
12/16/2020, 5:31 PMbjonnh
12/16/2020, 5:31 PMbjonnh
12/16/2020, 5:32 PMbjonnh
12/16/2020, 5:33 PM