Advent of Code 2023 day 14
12/14/2023, 5:00 AMelizarov
12/14/2023, 5:37 AMMichael de Kaste
12/14/2023, 6:06 AMNorbert Kiesel
12/14/2023, 6:24 AMbj0
12/14/2023, 6:41 AMbj0
12/14/2023, 6:42 AMelizarov
12/14/2023, 7:56 AMephemient
12/14/2023, 8:04 AMNorbert Kiesel
12/14/2023, 8:08 AM.reversed
on a range. Thus, I have 2 tilting methods.
private fun northSouth(area: CharArea, north: Boolean) {
val yRange = if (north) area.yRange else area.yRange.reversed()
for (x in area.xRange) {
val next = ArrayDeque<Int>()
for (y in yRange) {
when (area.get(x, y)) {
'.' -> next.addLast(y)
'#' -> next.clear()
'O' -> if (next.isNotEmpty()) {
area.set(x, next.removeFirst(), 'O')
area.set(x, y, '.')
next.addLast(y)
}
}
}
}
}
ephemient
12/14/2023, 8:09 AM.asReversed().transpose()
, re-using the fun Iterable<String>.transpose(): List<String>
I wrote yesterdayNorbert Kiesel
12/14/2023, 8:10 AMephemient
12/14/2023, 8:22 AMelizarov
12/14/2023, 8:25 AMNorbert Kiesel
12/14/2023, 8:28 AMMichael de Kaste
12/14/2023, 8:31 AMbuildMap {
input.lines().forEachIndexed { y, s ->
s.forEachIndexed { x, c ->
put(y to x, Node(y to x, c, this))
}
}
}
class Node(val pos: Point, var currentItem: Char, graph: Map<Point, Node>) {
private val neighbours by lazy {
Direction.entries.associateBy({ it }, { graph[pos + it.direction] })
}
}
after this I can simply discard looking at a Map<Point, Node> altogether, and work out my logic from there. getting the neighbours lazily prevents needing another loop, meaning I only need to declare the logic once.Marcin Wisniowski
12/14/2023, 8:49 AMelizarov
12/14/2023, 8:50 AMJaap Beetstra
12/14/2023, 8:51 AMJaap Beetstra
12/14/2023, 8:52 AMMarcin Wisniowski
12/14/2023, 8:52 AMandriyo
12/14/2023, 9:52 AMJaap Beetstra
12/14/2023, 9:53 AMprivate tailrec fun Rocks.repeatCycle(repeat: Int, cache: MutableMap<Rocks, Pair<Int, Rocks>>): Long {
if (repeat == 0) return load()
val (prevRep, next) = cache.getOrPut(this) { repeat to cycle() }
val nextRepeat = if (prevRep == repeat) repeat - 1 else (repeat - 1) % (prevRep - repeat)
return next.repeatCycle(nextRepeat, cache)
}
ephemient
12/14/2023, 9:58 AMMutableMap
iterates its entries in original insertion orderandriyo
12/14/2023, 9:58 AMandriyo
12/14/2023, 10:00 AMJaap Beetstra
12/14/2023, 10:02 AMandriyo
12/14/2023, 10:07 AMJaap Beetstra
12/14/2023, 10:08 AMdaugian
12/14/2023, 10:14 AMTomasz Linkowski
12/14/2023, 10:20 AMresults[i % 8]
(starting i
was 1
), iterated 1000 times, and then took results[1000000000 % 8]=results[0]
😮💨kingsley
12/14/2023, 10:42 AMMichael Böiers
12/14/2023, 10:47 AMMichael Böiers
12/14/2023, 11:38 AMMichael de Kaste
12/14/2023, 12:43 PM..O..O..O##..O..OOO.#.O
split
[..O..O..O][][..O..OOO.][.O]
sort
[OOO......][][OOOO.....][O.]
reinsert
OOO......##OOOO.....#O.
but tbf, doing immutable splits and transposing matrices isn't cheap, but its way shorter than my initial solution 😂Michael de Kaste
12/14/2023, 12:45 PMephemient
12/14/2023, 12:53 PMMichael de Kaste
12/14/2023, 12:58 PMMichael Böiers
12/14/2023, 1:26 PMEl Anthony
12/14/2023, 2:02 PMKai Yuan
12/14/2023, 2:32 PMephemient
12/14/2023, 2:45 PM....#
.#...
..O..
#....
...#.
Michael de Kaste
12/14/2023, 2:55 PMfun Array<Array<Boolean?>>.sortWest(){
lines@for (y in indices) {
val width = get(y).size
var walkPointer = 0
var detectOPointer = 0
while (walkPointer < width) {
if (this[y][walkPointer] == false) { // if the current walk pointer is on a '.'
detectOPointer = max(walkPointer, detectOPointer) + 1
while (detectOPointer < width) {
if (this[y][detectOPointer] == true) { // if the detector detect on 'O'
this[y][walkPointer] = true
this[y][detectOPointer] = false // swap them
break
} else if (this[y][detectOPointer] == null) { //if you detect a '#'
walkPointer = detectOPointer // set walker to detector position
break
} else {
detectOPointer++
}
}
}
walkPointer++
}
}
}
turns
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
into
O....#....
OOO.#....#
.....##...
OO.#OO....
OO......#.
O.#O...#.#
O....#OO..
O.........
#....###..
#OO..#....
where 'O' = true, '.' = false, and '#' = null
could probably be even smarterPaul Woitaschek
12/14/2023, 3:36 PMNorbert Kiesel
12/14/2023, 3:57 PMephemient
12/14/2023, 3:58 PM#
, count the number of O
, and rewrite itphldavies
12/14/2023, 4:48 PMList<CharArray>
manipulation (transpose+reverse) and used CharArray.sort
to sort runs between '#'
chars in the resulting lines (so rolled right). I tried implementing it with a CharArray
with mutating the data directly for transpose/reverse/rolling to avoid allocations but it was only marginally faster (~25ms vs ~28ms).Neil Banman
12/14/2023, 5:24 PMbj0
12/14/2023, 6:21 PMphldavies
12/14/2023, 6:22 PMbj0
12/14/2023, 6:32 PMMax Thiele
12/14/2023, 6:40 PMandriyo
12/14/2023, 6:44 PMMax Thiele
12/14/2023, 6:49 PMTomasz Linkowski
12/14/2023, 7:46 PMNeil Banman
12/14/2023, 7:50 PMphldavies
12/14/2023, 7:51 PMphldavies
12/14/2023, 7:51 PMEl Anthony
12/14/2023, 7:52 PMMax Thiele
12/14/2023, 7:56 PMJaap Beetstra
12/14/2023, 8:49 PMJonathan Kolberg
12/20/2023, 11:16 AMMarcin Wisniowski
12/20/2023, 12:33 PMMax Thiele
12/20/2023, 1:29 PMJonathan Kolberg
12/20/2023, 1:30 PMMarcin Wisniowski
12/20/2023, 1:35 PMMax Thiele
12/20/2023, 6:41 PMPaul Woitaschek
12/20/2023, 6:57 PMPaul Woitaschek
12/20/2023, 6:57 PMMarcin Wisniowski
12/20/2023, 8:04 PMephemient
12/20/2023, 8:05 PM