todd.ginsberg
12/01/2020, 9:20 PMtodd.ginsberg
12/01/2020, 9:20 PMtodd.ginsberg
12/01/2020, 9:20 PMAstronaut4449
12/01/2020, 9:59 PMephemient
12/01/2020, 10:49 PM.asSequence()
is slower than just using List<Int>
ephemient
12/01/2020, 10:49 PMtodd.ginsberg
12/01/2020, 11:21 PMtodd.ginsberg
12/01/2020, 11:21 PMtodd.ginsberg
12/01/2020, 11:38 PMprivate val input = data.sorted()
fun solvePart1(): Int =
input.asSequence().mapIndexedNotNull { idx, a ->
input.asSequence().drop(idx).firstOrNull { a + it == 2020 }?.let { a * it }
}.first()
fun solvePart2(): Int =
input.asSequence().mapIndexedNotNull { aIdx, a ->
input.asSequence().drop(aIdx).mapIndexedNotNull { bIdx, b ->
input.asSequence().drop(bIdx).firstOrNull { a + b + it == 2020 }?.let { a * b * it }
}.firstOrNull()
}.first()
todd.ginsberg
12/01/2020, 11:38 PMephemient
12/01/2020, 11:40 PMtodd.ginsberg
12/01/2020, 11:40 PMephemient
12/01/2020, 11:44 PM.drop().asSequence()
to perform better than .asSequence().drop()
, since the former should be calling .subList()
underneath and the latter actually iterates… but there's only 200 so it probably doesn't make a big difference here eitherephemient
12/01/2020, 11:46 PM.drop(idx + 1)
, otherwise you could be including the same element twice, but we don't actually have duplicates in our solutions so 🤷 it also doesn't really mattertodd.ginsberg
12/01/2020, 11:50 PMtodd.ginsberg
12/01/2020, 11:51 PM.drop(idx+1).dropWhile { a + it < 2020 }.take(1).firstOrNull { a + it == 2020 }...
todd.ginsberg
12/01/2020, 11:52 PMephemient
12/01/2020, 11:52 PMtodd.ginsberg
12/02/2020, 12:03 AMAstronaut4449
12/02/2020, 7:59 AMtodd.ginsberg
12/02/2020, 1:24 PM