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

Commit 17a985a

Browse files
committed
Add solution day 9 part 1
1 parent 6bda09c commit 17a985a

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

solutions/day09/main.go

+84-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,92 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
56
)
67

8+
type block struct {
9+
id int
10+
size int
11+
}
12+
13+
func (b *block) isFile() bool {
14+
return b.id >= 0
15+
}
16+
717
func main() {
8-
common.Setup(9, nil, nil)
18+
common.Setup(9, part1, nil)
19+
}
20+
21+
func parseBlocks(
22+
input string,
23+
) []block {
24+
isFile := true
25+
out := make([]block, len(input))
26+
27+
for idx, c := range input {
28+
var id int
29+
if isFile {
30+
id = idx / 2
31+
} else {
32+
id = -1
33+
}
34+
35+
out[idx] = block{
36+
id: id,
37+
size: int(c - '0'),
38+
}
39+
40+
isFile = !isFile
41+
}
42+
return out
43+
}
44+
45+
func part1(
46+
input string,
47+
) string {
48+
blocks := parseBlocks(input)
49+
sum := 0
50+
51+
pos := 0
52+
for len(blocks) > 0 {
53+
b := blocks[0]
54+
if b.isFile() {
55+
i := 0
56+
for i < b.size {
57+
}
58+
}
59+
}
60+
61+
return fmt.Sprintf("I don't know!")
62+
}
63+
64+
func popLastBlock(
65+
blocks []block,
66+
) (block, []block) {
67+
l := len(blocks)
68+
lastIdx := l - 1
69+
70+
if l <= 1 {
71+
// We will panic with array index out of bounds if blocks is 0,
72+
// this is on purpose to make errors consistent with go built-ins
73+
return blocks[0], []block{}
74+
}
75+
76+
lastBlock := blocks[lastIdx]
77+
newBlocks := blocks[:lastIdx]
78+
if lastBlock.isFile() {
79+
// Last block is a file, all is great
80+
return lastBlock, newBlocks
81+
}
82+
83+
// Grab blocks until we get one that is a file
84+
trailingSpace := block{id: -1, size: lastBlock.size}
85+
l = len(newBlocks)
86+
for !lastBlock.isFile() && l > 0 {
87+
lastBlock = newBlocks[len(newBlocks)-1]
88+
l = len(newBlocks)
89+
}
90+
91+
return lastBlock, newBlocks
992
}

0 commit comments

Comments
 (0)