Advent of Code 2021 day 9
12/09/2022, 5:00 AMMarcin Wisniowski
12/09/2022, 5:32 AMMarcin Wisniowski
12/09/2022, 5:32 AMMarcin Wisniowski
12/09/2022, 5:33 AMPoint
class, Direction
class, Point.move(Direction)
method, and Point.getAdjacent()
.Monster Brain
12/09/2022, 5:35 AMBin Wang
12/09/2022, 5:35 AMx == x
or y == y
, just do
(tailPos.x + (headPos.x - tailPos.x).sign) to (tailPos.y + (headPos.y - tailPos.y).sign)
Marcin Wisniowski
12/09/2022, 5:36 AMMarcin Wisniowski
12/09/2022, 5:37 AMMarcin Wisniowski
12/09/2022, 5:39 AMMarcin Wisniowski
12/09/2022, 5:39 AMAnirudh
12/09/2022, 5:42 AMhead.move(step.first).let { tail.follow(it) }
😂Jacob Moulton
12/09/2022, 5:50 AMSergei Petunin
12/09/2022, 5:52 AMwindowed
function and for the `Int.sign`:Jacob Moulton
12/09/2022, 5:57 AMJan Durovec
12/09/2022, 5:58 AM.indices.windowed
and then decided that i, i+1
is not something that would justify having to create a list of pairs in memory so i ended up with the following. I also spent quite some time searching for Int.sign
since kotlin.math.sign
works just with Double
and there are no Int
overloads :(Cognitive Gear
12/09/2022, 5:59 AMKroppeb
12/09/2022, 6:09 AMKroppeb
12/09/2022, 6:09 AMKroppeb
12/09/2022, 6:09 AMCognitive Gear
12/09/2022, 6:10 AMclass Day09 : AdventDay(2022, 9) {
data class Knot(var x: Int, var y: Int) {
var moveCallback : ((Pair<Int, Int>) -> Unit)? = null
fun moveBy(diffX : Int, diffY: Int) {
x += diffX
y += diffY
moveCallback?.invoke(x to y)
}
}
private val allTail1Positions : MutableSet<Pair<Int, Int>> = mutableSetOf((0 to 0))
private val allTail9Positions : MutableSet<Pair<Int, Int>> = mutableSetOf((0 to 0))
private val knots : List<Knot> = List(10) {
Knot(0, 0)
}.apply {
get(1).moveCallback = { allTail1Positions.add(it) }
get(9).moveCallback = { allTail9Positions.add(it) }
}
private fun moveTail(head : Knot, tail : Knot) {
val lenX = (head.x - tail.x)
val lenY = (head.y - tail.y)
if (max(lenX.absoluteValue, lenY.absoluteValue) > 1) {
tail.moveBy(lenX.coerceIn(-1..1), lenY.coerceIn(-1..1))
}
}
init {
val lines = input.lines()
val ropeCallOrder = knots.windowed(2, 1)
fun List<List<Knot>>.timeEvolution(diffX: Int, diffY: Int, number: Int) {
repeat(number) {
knots[0].moveBy(diffX, diffY)
forEach { (head, tail) -> moveTail(head, tail) }
}
}
lines.forEach {
val number = it.grabInts().first()
when (it.take()) {
"U" -> ropeCallOrder.timeEvolution(0, 1, number)
"D" -> ropeCallOrder.timeEvolution(0, -1, number)
"L" -> ropeCallOrder.timeEvolution(-1, 0, number)
"R" -> ropeCallOrder.timeEvolution(1, 0, number)
}
}
}
override fun part1(): String {
return allTail1Positions.size.toString()
}
override fun part2(): String {
return allTail9Positions.size.toString()
}
}
nkiesel
12/09/2022, 6:11 AMprivate fun onetwo(input: List<String>, length: Int): Int {
val rope = List(length) { Pos(0, 0) }
val visited = mutableSetOf(rope.last().copy())
for (line in input) {
val cmd = line.split(" ")
repeat(cmd[1].toInt()) {
rope.first().move(cmd[0])
rope.windowed(2).forEach { (h, t) -> t.follow(h) }
visited.add(rope.last().copy())
}
}
return visited.size
}
(with implementations for move
and follow
being very similar to code posted here, but updating the Pos objects.Dan Fingal-Surma
12/09/2022, 6:11 AMephemient
12/09/2022, 6:13 AMKroppeb
12/09/2022, 6:16 AMMichael de Kaste
12/09/2022, 6:20 AMMichael de Kaste
12/09/2022, 6:20 AMMichael de Kaste
12/09/2022, 6:21 AMdata class Point(val y: Int, val x: Int)
object Day9 : Challenge() {
val parsed = input
.lineSequence()
.map { it.split(" ").let { (a, b) -> a to b.toInt() } }
.flatMap { (move, amount) -> generateSequence { move }.take(amount) }
.map(::moveToDif)
.runningFold(List(10) { Point(0, 0) }, ::moveRope)
private fun moveToDif(move: String) = when (move) {
"U" -> Point(-1, 0)
"R" -> Point(0, 1)
"D" -> Point(1, 0)
"L" -> Point(0, -1)
else -> error("unknown move")
}
private fun moveRope(rope: List<Point>, direction: Point): List<Point> = rope
.drop(1)
.runningFold(Point(rope.first().y + direction.y, rope.first().x + direction.x), ::fixTail)
private fun fixTail(head: Point, tail: Point): Point {
val yDif = head.y - tail.y
val xDif = head.x - tail.x
return when (max(yDif.absoluteValue, xDif.absoluteValue)) {
2 -> Point(tail.y + yDif.sign, tail.x + xDif.sign)
else -> tail
}
}
override fun part1() = parsed.distinctBy { it[1] }.count()
override fun part2() = parsed.distinctBy { it[9] }.count()
}
not sure about the fixtail part, could probably be better?Michael de Kaste
12/09/2022, 6:24 AMrkechols
12/09/2022, 6:35 AMCognitive Gear
12/09/2022, 6:41 AMJonathan Kolberg
12/09/2022, 6:42 AMKroppeb
12/09/2022, 7:09 AMscan
is also in the stdlib, it just delegates to runningFoldDan Fingal-Surma
12/09/2022, 7:10 AMDan Fingal-Surma
12/09/2022, 7:26 AMRiccardo Lippolis
12/09/2022, 7:32 AMMichael de Kaste
12/09/2022, 7:34 AMMutableList(10){ Point(0, 0) }
is probably what you want 😄Kroppeb
12/09/2022, 7:34 AMMichael de Kaste
12/09/2022, 7:35 AMRiccardo Lippolis
12/09/2022, 7:35 AMList(10) { Point(0, 0) }
works too 🙂Michael de Kaste
12/09/2022, 7:35 AMRiccardo Lippolis
12/09/2022, 7:36 AMBrian Hartvigsen
12/09/2022, 7:36 AMMutableList(10) { Point(0, 0) }
and Int.sign
definitely adding those to my backlog 🙂 Int.sign
is so much better then max(-1, min(n, 1))
...Brian Hartvigsen
12/09/2022, 7:40 AMPoint
class:
fun walkTo(other: Point): Sequence<Point> {
return sequence {
var curr = this@Point
while (curr != other) {
curr += Point((other.x - x).sign, (other.y - y).sign)
yield(curr)
}
}
}
Tobias Suchalla
12/09/2022, 7:47 AMabs
and coerceIn
to the rescue. I thought about using something with Int.sign
as well but this feels way nicer 🙂Michael Böiers
12/09/2022, 7:49 AMpackage aoc2022
import kotlin.math.abs
fun main() {
data class Pos(val x: Int, val y: Int) {
fun diagonals() = sequenceOf(Pos(x - 1, y - 1), Pos(x + 1, y + 1), Pos(x - 1, y + 1), Pos(x + 1, y - 1))
fun straights() = sequenceOf(Pos(x - 1, y), Pos(x + 1, y), Pos(x, y + 1), Pos(x, y - 1))
fun dist(p: Pos) = abs(x - p.x) + abs(y - p.y)
fun follow(head: Pos) = when {
dist(head) >= 3 -> diagonals().minBy { it.dist(head) }
dist(head) == 2 && head !in diagonals() -> straights().minBy { it.dist(head) }
else -> this
}
}
fun List<Pair<String, Int>>.move(knots: Int): Int {
val rope = MutableList(knots) { Pos(0, 0) }
val tailHistory = mutableSetOf(rope.last())
for ((dir, n) in this) repeat(n) {
with(rope.first()) {
rope[0] = when (dir) {
"R" -> copy(x = x + 1)
"L" -> copy(x = x - 1)
"U" -> copy(y = y - 1)
else -> copy(y = y + 1)
}
}
for (i in rope.indices.drop(1)) rope[i] = rope[i].follow(rope[i - 1])
tailHistory += rope.last()
}
return tailHistory.size
}
with(generateSequence { readlnOrNull() }.toList().map { it.split(" ").let { (a, b) -> a to b.toInt() } }) {
listOf(2, 10).forEach { println(move(it)) }
}
}
xxfast
12/09/2022, 7:52 AMAnirudh
12/09/2022, 7:53 AMbuildSet, fold, generateSequence, when's, heave.move, tail.follow(head)
here's my part 1: https://github.com/aneroid/advent-of-code-2022-kotlin/blob/943f72305886b1b12b879faeb7a99af033739425/src/Day09.kt
think I can shimmy part 2 into that "let tail follow head" part, with windowed or something.phldavies
12/09/2022, 8:08 AMChristian Ricardo
12/09/2022, 8:14 AMNeil Banman
12/09/2022, 8:14 AMrunningFold
. What a great addition to the standard library! I had previously used fold
with a MutableList
as the accumulator, then returning acc.apply { add... }
.
But that was really kludgy so then I started just using buildList { myCollection.forEach { add... } }
. But runningFold
is what I wanted all along!Neil Banman
12/09/2022, 8:39 AMMichael de Kaste
12/09/2022, 8:40 AMNeil Banman
12/09/2022, 8:45 AMDavio
12/09/2022, 8:51 AMif (head == tail || (abs(head.x - tail.x) <= 1 && abs(head.y - tail.y) <= 1)) return Point.ZERO
return Point(head.x.compareTo(tail.x), head.y.compareTo(tail.y))
Davio
12/09/2022, 8:51 AMDavio
12/09/2022, 8:52 AMphldavies
12/09/2022, 8:53 AMDavio
12/09/2022, 8:55 AMNeil Banman
12/09/2022, 8:59 AMx.coerceIn(-1..1)
, but others pointed out that x.sign
does the same thing.Davio
12/09/2022, 9:00 AMDavio
12/09/2022, 9:07 AMDavio
12/09/2022, 9:07 AMMichael Böiers
12/09/2022, 9:16 AMdata class Pos(val x: Int, val y: Int) {
fun diagonals() = sequenceOf(Pos(x - 1, y - 1), Pos(x + 1, y + 1), Pos(x - 1, y + 1), Pos(x + 1, y - 1))
fun straights() = sequenceOf(Pos(x - 1, y), Pos(x + 1, y), Pos(x, y + 1), Pos(x, y - 1))
fun dist(p: Pos) = abs(x - p.x) + abs(y - p.y)
fun follow(head: Pos) = when {
dist(head) >= 3 -> diagonals().minBy { it.dist(head) }
dist(head) == 2 && head !in diagonals() -> straights().minBy { it.dist(head) }
else -> this
}
fun move(dir: String) = when (dir) {
"R", "L" -> copy(x = x + if (dir == "R") 1 else -1)
else -> copy(y = y + if (dir == "U") 1 else -1)
}
}
Davio
12/09/2022, 9:24 AMJoris PZ
12/09/2022, 9:43 AMPosition
, Vector
and Heading
classes from earlier years.
val p09 = suspend {
val input = readInput("09.txt")
solve(input, 2).print { "Part 1: $it" }
solve(input, 10).print { "Part 2: $it" }
}
private fun solve(input: String, numKnots: Int): Int {
val knots = Array(numKnots) { Position(0, 0) }
val visited = hashSetOf(knots[0])
input.lines().forEach { step ->
val heading = when (step.substring(0, 1)) {
"U" -> Heading.N
"L" -> Heading.W
"R" -> Heading.E
"D" -> Heading.S
else -> error("")
}
val numSteps = step.substring(2).toInt()
repeat(numSteps) {
knots[numKnots - 1] = knots[numKnots - 1].move(heading)
(numKnots - 2 downTo 0).forEach {
if (knots[it + 1].notAdjacentTo(knots[it])) {
knots[it] = knots[it] + (knots[it + 1] - knots[it]).sign
}
}
visited.add(knots[0])
}
}
return visited.size
}
Anirudh
12/09/2022, 9:48 AMthink I can shimmy part 2 into that "let tail follow head" part, with windowed or something. (edited)haha - thanks to lunch and more coffee, added a runningFold and now my part2 is nearly identical to my part1 💪 (and now the possible refactoring for part 1 & 2 is obvious; probably for everyone's solutions) https://github.com/aneroid/advent-of-code-2022-kotlin/blob/main/src/Day09.kt
Grzegorz Aniol
12/09/2022, 9:52 AMDavio
12/09/2022, 9:59 AMDavio
12/09/2022, 10:01 AMPoint
class in a separate util file or library, I did once and needed it at least oncee every year since then 😄Jakub Gwóźdź
12/09/2022, 10:03 AMMichael Böiers
12/09/2022, 10:03 AMDavio
12/09/2022, 10:06 AMMichael Böiers
12/09/2022, 10:21 AMphldavies
12/09/2022, 10:32 AMMichael Böiers
12/09/2022, 10:38 AMMichael Böiers
12/09/2022, 10:40 AMclass State($STATE_CONSTRUCTOR$) {
fun neighbors(): Iterable<State> {
return $NEIGHBORS$
}
}
var s = State()
val v = mutableSetOf<State>()
val u = mutableSetOf<State>()
val d = mutableMapOf(s to 0)
while (true) {
s.neighbors().filter { it !in v }.forEach { n ->
u += n
val distance = d.getValue(s) + 1
d.compute(n) { _, old -> min(distance, old ?: Int.MAX_VALUE) }
}
v += s
u -= s
s = u.randomOrNull() ?: break
}
Simon Birt
12/09/2022, 11:22 AMJakub Gwóźdź
12/09/2022, 11:48 AMDay 1 part 1: 70698 in 0.000335s
Day 1 part 2: 206643 in 0.000387s
Day 2 part 1: 15337 in 0.000344s
Day 2 part 2: 11696 in 0.000336s
Day 3 part 1: 8109 in 0.000377s
Day 3 part 2: 2738 in 0.000314s
Day 4 part 1: 459 in 0.000417s
Day 4 part 2: 779 in 0.000388s
Day 5 part 1: ZRLJGSCTR in 0.000432s
Day 5 part 2: PRTTGRFPB in 0.000380s
Day 6 part 1: 1080 in 0.000191s
Day 6 part 2: 3645 in 0.001114s
Day 7 part 1: 1648397 in 0.001207s
Day 7 part 2: 1815525 in 0.001300s
Day 8 part 1: 1843 in 0.000738s
Day 8 part 2: 180000 in 0.002459s
Day 9 part 1: 6266 in 0.514819s
Day 9 part 2: 2369 in 0.166540s
Notice that the part 1 of day 9 executes OVER HALF A SECOND… (when the rope is just 2 knots long) and 1/3 of that time in part 2 (when it’s a 10 knots long). Is it the same in your case?
The culprit is of course the Set<Points> that collects the trail left by the last knot.Fredrik Rødland
12/09/2022, 12:06 PMFredrik Rødland
12/09/2022, 12:07 PMFredrik Rødland
12/09/2022, 12:08 PMDavio
12/09/2022, 12:09 PM2427
Took 148 ms
5513
Took 12 ms
This is with part 2 run first, then part 1 is a breezeDavio
12/09/2022, 12:10 PMJakub Gwóźdź
12/09/2022, 12:10 PMmoves.fold(State) {...}
that created new state (with set inside) on every move to just a moves.forEach() {...}
that modifies a mutableSet in only one instance, suddenly it’s back to under reasonable 7msFredrik Rødland
12/09/2022, 12:10 PMFredrik Rødland
12/09/2022, 12:11 PMthe “bonuses” of functional programming
- yuppDavio
12/09/2022, 12:11 PM5513
Took 3 ms
2427
Took 16 ms
Which is part 1 first, then part 2Fredrik Rødland
12/09/2022, 12:12 PMdiff.y.absoluteValue <= 1 && diff.x.absoluteValue <= 1 -> tail
- but this had no affectDavio
12/09/2022, 12:13 PMFredrik Rødland
12/09/2022, 12:14 PMFredrik Rødland
12/09/2022, 12:15 PMJakub Gwóźdź
12/09/2022, 12:15 PMFredrik Rødland
12/09/2022, 12:15 PMMichael Böiers
12/09/2022, 12:20 PMDavio
12/09/2022, 12:24 PMephemient
12/09/2022, 12:35 PMMichael de Kaste
12/09/2022, 12:36 PMMichael de Kaste
12/09/2022, 12:37 PMphldavies
12/09/2022, 12:39 PMRope
structure and foldRunning
and another using an IntArray
for state.
year 2022 day 9 warmup (3000 iterations) took 22.824483343s
year 2022 day 9 part 1 (Arrays) took 1.186119ms: 6037
year 2022 day 9 part 1 (ImmutableRope) took 2.227789ms: 6037
year 2022 day 9 part 2 (Arrays) took 1.114141ms: 2485
year 2022 day 9 part 2 (ImmutableRope) took 2.874673ms: 2485
basic benchmarking - not accurate by any stretch.Jakub Gwóźdź
12/09/2022, 12:41 PMFredrik Rødland
12/09/2022, 12:50 PMmoves
is so nice - which I had thought of just converting to a path right away instead of iterating "commands". very nice!Davio
12/09/2022, 12:54 PMephemient
12/09/2022, 12:55 PMHasan Gilmanov
12/09/2022, 12:56 PMimport lib.Point
fun getMoves(input: List<String>): List<Pair<Char, Int>> {
return input.map {
val (dir, moveSize) = it.split(" ")
Pair(dir.first(), moveSize.toInt())
}
}
fun part1(input: List<String>): Int {
return followHead(2, getMoves(input))
}
fun part2(input: List<String>): Int {
return followHead(10, getMoves(input))
}
fun Point.updateHead(dir: Char): Point {
return when (dir) {
'R' -> toRight()
'L' -> toLeft()
'U' -> higher()
'D' -> lower()
else -> error("Invalid direction")
}
}
fun Point.followIfBeyond(f: Point, distance: Double = 1.5): Point? {
return if (this.distanceTo(f) < distance) {
null
} else Point(
x + sign(f.x - x.toDouble()).toInt() * min(abs(f.x - x), 1),
y + sign(f.y - y.toDouble()).toInt() * min(abs(f.y - y), 1)
)
}
fun followHead(ropeSize: Int, moves: List<Pair<Char, Int>>): Int {
val rope = MutableList(ropeSize) { Point(0, 0) }
val tailPoints = HashSet<Point>().apply {
add(rope.last())
}
for ((dir, steps) in moves) {
repeat(steps) {
rope[0] = rope[0].updateHead(dir)
for (i in 1 until ropeSize) {
rope[i].followIfBeyond(rope[i - 1])?.let {
rope[i] = it
if (i == ropeSize - 1) {
tailPoints.add(rope.last())
}
} ?: break
}
}
}
return tailPoints.count()
}
ephemient
12/09/2022, 12:56 PMephemient
12/09/2022, 12:56 PMDavio
12/09/2022, 12:56 PMephemient
12/09/2022, 12:57 PMbreak
out of a regular for
loop, but yes it shouldn't be hard 🙂Davio
12/09/2022, 12:57 PMR 0
😄ephemient
12/09/2022, 12:58 PMDavio
12/09/2022, 12:58 PMephemient
12/09/2022, 12:58 PMR -1
as well, but that's definitely unnecessary 😄Davio
12/09/2022, 12:58 PMMichael de Kaste
12/09/2022, 12:58 PMgenerateSequence{ move }.take(count)
part in my code 😂phldavies
12/09/2022, 1:25 PMRope
compared -
year 2022 day 9 warmup (1000 iterations) took 12.776440358s
year 2022 day 9 part 1
Arrays took 1.160782ms: 6037
Sequences took 1.669952ms: 6037
ImmutableRope took 2.652892ms: 6037
year 2022 day 9 part 2
Arrays took 1.220136ms: 2485
Sequences took 2.152197ms: 2485
ImmutableRope took 3.810463ms: 2485
Paul Woitaschek
12/09/2022, 1:25 PMCharles Flynn
12/09/2022, 1:40 PMcoerceIn
and sign
which would have helped a lot. Ah well.
import kotlin.math.abs
private val input = readFileAsLines("9.txt")
.map {
val (direction, amount) = it.split(" ")
direction to amount.toInt()
}
fun main() {
println("Part 1: ${part1()}")
println("Part 2: ${part2()}")
}
private fun part1() = simulateRope(2)
private fun part2() = simulateRope(10)
fun simulateRope(size: Int): Int {
val tailCoordinates = mutableSetOf<Coordinate>()
val coordinates = MutableList(size) { Coordinate(0, 0) }
for ((direction, amount) in input) {
repeat(amount) {
when (direction) {
"U" -> coordinates[0] = coordinates[0].above()
"D" -> coordinates[0] = coordinates[0].below()
"L" -> coordinates[0] = coordinates[0].toLeft()
"R" -> coordinates[0] = coordinates[0].toRight()
}
for (i in 1 until coordinates.size) {
coordinates[i] = coordinates[i].moveTowards(coordinates[i - 1])
}
tailCoordinates.add(coordinates.last())
}
}
return tailCoordinates.size
}
fun Coordinate.toRight() = Coordinate(x + 1, y)
fun Coordinate.toLeft() = Coordinate(x - 1, y)
fun Coordinate.above() = Coordinate(x, y + 1)
fun Coordinate.below() = Coordinate(x, y - 1)
fun Coordinate.moveTowards(other: Coordinate): Coordinate {
if (abs(x - other.x) > 1 || abs(y - other.y) > 1) {
val around = getSurroundingCoordinatesIncDiagonals().toSet()
val aroundOther = other.getSurroundingCoordinatesIncDiagonals().toSet()
val intersected = (around intersect aroundOther).toList()
return intersected.firstOrNull { it.x == x && it.x == other.x }
?: intersected.firstOrNull { it.y == y && it.y == other.y }
?: intersected.first { it.x != x && it.y != y }
}
return this
}
Anirudh
12/09/2022, 1:57 PMAnirudh
12/09/2022, 2:44 PMfollow
to just:
private fun Point.follow(head: Point): Point =
if ((head.x - x).absoluteValue > 1 || (head.y - y).absoluteValue > 1)
Point(x + (head.x - x).sign, y + (head.y - y).sign)
else
this
the other equality checks for x & y are not needed since (head.x - x).sign
is 0
when they're the same
https://github.com/aneroid/advent-of-code-2022-kotlin/commit/6a35d287Anirudh
12/09/2022, 2:47 PM100 knots test : 1
100 knots testP2: 1
100 knots actual: 354
Kevin Del Castillo
12/09/2022, 3:07 PMscanIndexed
? Full code herewakingrufus
12/09/2022, 3:48 PMdata class Location(val x: Int, val y: Int) {
fun moveUp(): Location {
return this.copy(y = y + 1)
}
fun moveDown(): Location {
return this.copy(y = this.y - 1)
}
fun moveLeft(): Location {
return this.copy(x = x - 1)
}
fun moveRight(): Location {
return this.copy(x = x + 1)
}
fun chase(otherLocation: Location): Location {
val xDelta = otherLocation.x - x
val yDelta = otherLocation.y - y
if (abs(xDelta) > 1) {
return Location(
x = x + xDelta.sign,
y = y + yDelta.sign
)
}
if (abs(yDelta) > 1) {
return Location(
x = x + xDelta.sign,
y = y + yDelta.sign
)
}
return this
}
}
phldavies
12/09/2022, 3:50 PMrunningFold
(which is the an alias for scan
) but not sure I’ve seen any runningFoldIndexed
or scanIndexed
Luke Armitage
12/09/2022, 4:08 PMInt.sign
from kotlin.math.
time to finish day 8 part 2 😅Neil Banman
12/09/2022, 5:17 PMSequence
for follow
. Now my code is a world's tour of Sequences
, using asSequence {}
, sequence {}
, and generateSequence()
in different parts. It also features a Sequence
within a Sequence
!Kevin Del Castillo
12/09/2022, 5:18 PMscanIndexed
, but you mentioned two other methods, could they replace scanIndexed
in my solution?phldavies
12/09/2022, 5:21 PMscan{Indexed}
was renamed to runningFold{Indexed}
to be more consistent with fold{Indexed}
phldavies
12/09/2022, 5:22 PM@SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class)
public fun <T, R> Sequence<T>.scan(initial: R, operation: (acc: R, T) -> R): Sequence<R> {
return runningFold(initial, operation)
}
Karloti
12/09/2022, 5:36 PMDan Fingal-Surma
12/09/2022, 5:46 PMMutableList(10){ Point(0, 0) }
… I kept reaching for mutableListOf(10) { Point(0, 0) }
which did not workNeil Banman
12/09/2022, 6:49 PMphldavies
12/09/2022, 6:59 PMmutableSetOf<Int>
for a BitSet
and cantor-pairing the zigzag-encoded coordinates. Part2 is now less than 0.5ms
year 2022 day 9 warmup (2000 iterations) took 16.602969683s
year 2022 day 9 part 1
Arrays took 191.41us: 6037
Sequences took 1.445318ms: 6037
ImmutableRope took 2.111331ms: 6037
year 2022 day 9 part 2
Arrays took 427.744us: 2485
Sequences took 1.945550ms: 2485
ImmutableRope took 2.256112ms: 2485
Karloti
12/09/2022, 8:45 PMxxfast
12/09/2022, 11:18 PMNeil Banman
12/09/2022, 11:21 PMLuke Armitage
12/09/2022, 11:30 PMNeil Banman
12/09/2022, 11:34 PMNeil Banman
12/09/2022, 11:38 PMIterable<Char>.printToConsole()
utility function, but my code calculated each tail successively. It was difficult to make make all the knots appear on the same step. I eventually figured it out, but it was frustrating.Jacob
12/11/2022, 12:49 AM