Advent of Code 2024 - Day 9 - Disk Fragmenter

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ม.ค. 2025

ความคิดเห็น • 3

  • @WayneBagguley
    @WayneBagguley หลายเดือนก่อน +1

    Part 1
    fun calc(input: String): BigInteger {
    val (files,spaces) = input.windowed(2, 2, true) { it[0].digitToInt() to (it.getOrNull(1)?.digitToInt() ?: 0) }.unzip()
    val rev = sequence { files.mapIndexed { i, c -> i to c }.reversed().forEach { p -> repeat(p.second) { yield(p.first) } } }.iterator()
    return files.mapIndexed { i, c -> i to c }.zip(spaces).flatMap { p -> List(p.first.second) { p.first.first } + List(p.second) { rev.next() } }
    .mapIndexed { i, c -> (i * c).toBigInteger() }.take(files.sum()).fold(BigInteger.ZERO){a,b->a+b} }

  • @aaronperl
    @aaronperl หลายเดือนก่อน +1

    I had a lot of fun with this one (even though the time I had to work on it was really broken up over the course of yesterday and this morning). For Part B, I did merge empty spaces together. I felt it made it easier to find the place to put the file. I count backwards from the highest file ID, and look for the first empty space that's big enough to hold the file:
    val fileToMove = this.blockMap.firstOrNull() { it.fileId == fileIdx } ?: continue
    val fileToMoveIdx = this.blockMap.indexOf(fileToMove)
    val firstFreeBlockIndex = this.blockMap.indexOfFirst { it.blockType == BlockType.FREE && it.blockSize >= fileToMove.blockSize }
    If the firstFreeBlock is AFTER the file we're moving, OR there is no free block big enough, then leave the file where it is and continue with the next file ID
    Then:
    - replace the file in the list with a free space block of the same size
    - insert the file before firstFreeBlock
    - shrink the size of firstFreeBlock
    - if firstFreeBlock is size 0 then remove it from the list
    this.blockMap[fileToMoveIdx] = DiskBlock(-1, fileToMove.blockSize, BlockType.FREE)
    this.blockMap.add(firstFreeBlockIndex, fileToMove)
    firstFreeBlock.blockSize -= fileToMove.blockSize
    if (firstFreeBlock.blockSize == 0) {
    this.blockMap.remove(firstFreeBlock)
    }
    And then compact (or I guess "coalesce" is the proper term) the free space blocks
    And then move on to the next file
    As an aside, I haven't needed to use BigInteger, except for some exceptional circumstances in previous years. Almost always the solutions have fit just fine in Longs.

  • @joeycary2104
    @joeycary2104 หลายเดือนก่อน +1

    Great content! I sent you an email regarding consulting. It may have been marked as spam.