Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Solutions for day 09 #4

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion solutions/day09/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,92 @@
package main

import (
"fmt"
"github.com/terminalnode/adventofcode2024/common"
)

type block struct {
id int
size int
}

func (b *block) isFile() bool {
return b.id >= 0
}

func main() {
common.Setup(9, nil, nil)
common.Setup(9, part1, nil)
}

func parseBlocks(
input string,
) []block {
isFile := true
out := make([]block, len(input))

for idx, c := range input {
var id int
if isFile {
id = idx / 2
} else {
id = -1
}

out[idx] = block{
id: id,
size: int(c - '0'),
}

isFile = !isFile
}
return out
}

func part1(
input string,
) string {
blocks := parseBlocks(input)
sum := 0

Check failure on line 49 in solutions/day09/main.go

View workflow job for this annotation

GitHub Actions / Check Go Formatting

declared and not used: sum

pos := 0

Check failure on line 51 in solutions/day09/main.go

View workflow job for this annotation

GitHub Actions / Check Go Formatting

declared and not used: pos
for len(blocks) > 0 {
b := blocks[0]
if b.isFile() {
i := 0
for i < b.size {
}
}
}

return fmt.Sprintf("I don't know!")
}

func popLastBlock(
blocks []block,
) (block, []block) {
l := len(blocks)
lastIdx := l - 1

if l <= 1 {
// We will panic with array index out of bounds if blocks is 0,
// this is on purpose to make errors consistent with go built-ins
return blocks[0], []block{}
}

lastBlock := blocks[lastIdx]
newBlocks := blocks[:lastIdx]
if lastBlock.isFile() {
// Last block is a file, all is great
return lastBlock, newBlocks
}

// Grab blocks until we get one that is a file
trailingSpace := block{id: -1, size: lastBlock.size}

Check failure on line 84 in solutions/day09/main.go

View workflow job for this annotation

GitHub Actions / Check Go Formatting

declared and not used: trailingSpace
l = len(newBlocks)
for !lastBlock.isFile() && l > 0 {
lastBlock = newBlocks[len(newBlocks)-1]
l = len(newBlocks)
}

return lastBlock, newBlocks
}
Loading