Advent of Code 2023 day 9
12/09/2024, 5:00 AMJonathan Kolberg
12/09/2024, 6:08 AMbj0
12/09/2024, 6:11 AMMichael de Kaste
12/09/2024, 6:40 AMkingsley
12/09/2024, 7:44 AMbj0
12/09/2024, 7:49 AMDan Fingal-Surma
12/09/2024, 8:02 AMDan Fingal-Surma
12/09/2024, 8:02 AMDan Fingal-Surma
12/09/2024, 8:02 AMDan Fingal-Surma
12/09/2024, 8:03 AMList<Int>
(of blocks) that just contained each file number of -1
for free space, in order to make part 2 tractable.
Like:
00...111...2...333.44.5555.6666.777.888899
With .
= -1
To start with part 1 I used forward/reverse iteration only, no mutations.
In part 2, linear search for free space. Performs just fine.Dan Fingal-Surma
12/09/2024, 8:05 AMDan Fingal-Surma
12/09/2024, 8:44 AMNeil Banman
12/09/2024, 9:03 AMNeil Banman
12/09/2024, 9:12 AMMax Thiele
12/09/2024, 10:25 AMJaap Beetstra
12/09/2024, 11:51 AMephemient
12/09/2024, 12:35 PMephemient
12/09/2024, 12:36 PMAnirudh
12/09/2024, 12:52 PMdata class Block(val count: Int, val fileId: Int?)
with fileId being null
for the free spaces.
so apart from checking for spaces and size, had to code in the math for big pieces being broken into smaller ones and then swapping and updating, etc.Anirudh
12/09/2024, 12:54 PM12345
input would be parsed as only:
[Block(count=1, fileId=0), Block(count=2, fileId=null), Block(count=3, fileId=1), Block(count=4, fileId=null), Block(count=5, fileId=2)]
and using the long version only for pretty printing while debugging:
0..111....22222
also, I realise both parts defragging (I called it sorting) could be doing with indexing and not actually re-arrangingephemient
12/09/2024, 1:07 PMIntArray
directly from the input: even indices are file ID * 2, odd indices are free spaces, values are sizes. then I mutate it throughout the solution, narrowing in from the left and right.
part 2 I split the files from spaces and added another array for offsets. didn't expand anything into individual blocks or create any classes or objectsAnirudh
12/09/2024, 1:16 PM[1, 2, 3, 4, 5]
?
hmm, I definitely over-engineered it 🤦
finally have to do the same math with sizes, free/non-free etc. anyway; but with more data structures getting in the way 😕Renette Ros
12/09/2024, 1:31 PMList<Int?>
containing either the file id or null for a blank size and moves the last non-null value earlier while it can
Part 2 was originally built on the same idea, but trying to keep track of the empty spaces got incredibly messy. I cleaned it up by making a data class to keep track of file attributes (id, size, start position) and started keep track of only the files. When I find a gap (by looking at the difference between the end and start positions of two consecutive files), I remove the file from the list, update its start position, and re-insert it in the correct place.El Anthony
12/09/2024, 1:55 PMMichael Böiers
12/09/2024, 2:48 PMnerses
12/09/2024, 3:02 PMMichael Böiers
12/09/2024, 3:10 PMnerses
12/09/2024, 3:11 PMMichael Böiers
12/09/2024, 3:11 PMMichael Böiers
12/09/2024, 3:12 PMnerses
12/09/2024, 3:14 PM233313312141413140213
is going to
00...111...2...333.44.5555.6666.777.888899.101
or
00...111...2...333.44.5555.6666.777.888899.101010
Michael Böiers
12/09/2024, 3:15 PMnerses
12/09/2024, 3:19 PMEach file on disk also has an ID number based on the order of the files as they appear before they are rearrangedas per this line the id of the last
3
is 10nerses
12/09/2024, 3:20 PMMichael Böiers
12/09/2024, 3:21 PMephemient
12/09/2024, 3:23 PMnerses
12/09/2024, 3:30 PMAnirudh
12/09/2024, 4:01 PMephemient
12/09/2024, 4:05 PMAnirudh
12/09/2024, 4:07 PMJakub Gwóźdź
12/09/2024, 4:15 PMwarming up for 5s
warmed up for 4.898349709s (136 times)
Parsing took 332.417us
Part 1 took 578.333us, result is 6378826667552
Part 2 took 39.445083ms, result is 6413328569890
Basically I'm chunking whole input into pairs "file,free", and then create sectors with a set size and a mutable list of chunks (files or their parts).nerses
12/09/2024, 4:55 PMPaul Woitaschek
12/09/2024, 4:58 PMbj0
12/09/2024, 5:42 PMbj0
12/09/2024, 5:51 PM.sumOf
instead of fold
Jakub Gwóźdź
12/09/2024, 6:08 PMtodd.ginsberg
12/10/2024, 12:23 AMNeil Banman
12/10/2024, 2:01 AMOzioma Ogbe
12/10/2024, 3:30 AMOzioma Ogbe
12/10/2024, 3:31 AMimport java.io.File
import java.util.LinkedList
import java.util.Stack
fun main() {
val input = File("/Users/oogbe/IdeaProjects/AdventOfCodeSolutions/solutions/src/2024/input.txt").readText()
println(input.map { it.digitToInt().toLong() }.sum())
var abs = 0
val diskInputs = input.mapIndexed { index, c ->
val x = if (index % 2 == 0) FileBlock(index / 2, c.digitToInt(), abs) else FreeSpace(c.digitToInt(), abs)
abs += c.digitToInt()
x
}
val deque = LinkedList<DiskInput>().also { it.addAll(diskInputs) }
var result = 0L
while (deque.isNotEmpty()) {
val back = deque.pollLast()
if (back is FreeSpace) continue
val diskBlock = back as FileBlock
val temp = Stack<DiskInput>()
var found = false
while (deque.isNotEmpty()) {
val front = deque.pollFirst()
if (front is FreeSpace && front.frequency >= diskBlock.frequency) {
if (front.frequency > diskBlock.frequency) {
deque.addFirst(
FreeSpace(
front.frequency - diskBlock.frequency,
front.absIndex + diskBlock.frequency
)
)
}
deque.addFirst(FileBlock(back.id, back.frequency, front.absIndex))
found = true
break
} else {
temp.push(front)
}
}
while (temp.isNotEmpty()) {
deque.addFirst(temp.pop())
}
if(!found) {
(diskBlock.absIndex until (diskBlock.absIndex + diskBlock.frequency)).forEach {
result += it * diskBlock.id
}
}
}
println(result)
}
sealed interface DiskInput {
val frequency: Int
val absIndex: Int
}
data class FileBlock(val id: Int, override val frequency: Int, override val absIndex: Int) : DiskInput
data class FreeSpace(override val frequency: Int, override val absIndex: Int) : DiskInput
Ozioma Ogbe
12/10/2024, 3:32 AMritesh
12/10/2024, 9:52 AMManel Martos
01/02/2025, 7:53 PMJakub Gwóźdź
01/03/2025, 5:21 AMManel Martos
01/03/2025, 7:21 AMManel Martos
01/03/2025, 7:49 AMJakub Gwóźdź
01/03/2025, 7:50 AMTobias Wohlfarth
01/10/2025, 8:00 PMManel Martos
01/10/2025, 8:39 PMephemient
01/10/2025, 8:41 PMTobias Wohlfarth
01/10/2025, 9:26 PM