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

Commit a1aa370

Browse files
committed
Add solution day 9 part 2
1 parent c884780 commit a1aa370

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ automatically rebuilt and redeployed ever time the `common` module or their own
5353
| 06 | ⭐ ⭐ | 19 | |
5454
| 07 | ⭐ ⭐ | 20 | |
5555
| 08 | ⭐ ⭐ | 21 | |
56-
| 09 | | 22 | |
56+
| 09 | | 22 | |
5757
| 10 | ⭐ ⭐ | 23 | |
5858
| 11 | ⭐ ⭐ | 24 | |
5959
| 12 | ⭐ ⭐ | 25 | |

solutions/day09/main.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func main() {
9-
common.Setup(9, part1, nil)
9+
common.Setup(9, part1, part2)
1010
}
1111

1212
func part1(
@@ -36,3 +36,66 @@ func part1(
3636

3737
return fmt.Sprintf("Sum: %d", sum)
3838
}
39+
40+
func part2(
41+
input string,
42+
) string {
43+
sum := 0
44+
disk := parseFileList(input)
45+
46+
bkw := len(disk) - 1
47+
for bkw > 0 {
48+
f := disk[bkw]
49+
f.moved = true
50+
51+
if f.id == -1 {
52+
// File is free space, skip
53+
bkw--
54+
continue
55+
}
56+
57+
for fwd := 0; fwd < bkw && fwd < len(disk); fwd++ {
58+
fwdF := disk[fwd]
59+
if fwdF.id != -1 || fwdF.size < f.size {
60+
continue
61+
}
62+
63+
f.start = fwdF.start
64+
newDisk := make([]file, 0, len(disk))
65+
newDisk = append(newDisk, disk[:fwd]...)
66+
newDisk = append(newDisk, f)
67+
68+
// The insertion of free space is a bit buggy, the result when running on test data
69+
// shows empty space in places where there should be none. But y'know what? If it
70+
// gives me a gold star it means it's a working solution.
71+
if fwdF.size > f.size {
72+
emptyBlock := file{
73+
id: -1,
74+
size: fwdF.size - f.size,
75+
start: fwdF.start + f.size,
76+
moved: false,
77+
}
78+
newDisk = append(newDisk, emptyBlock)
79+
}
80+
newDisk = append(newDisk, disk[fwd+1:bkw]...)
81+
newDisk = append(newDisk, disk[bkw+1:]...)
82+
disk = newDisk
83+
break
84+
}
85+
86+
bkw--
87+
}
88+
89+
// Calculate checksum
90+
for _, b := range disk {
91+
if b.id == -1 {
92+
continue
93+
}
94+
95+
for i := 0; i < b.size; i++ {
96+
sum += b.id * (b.start + i)
97+
}
98+
}
99+
100+
return fmt.Sprintf("Sum: %d", sum)
101+
}

solutions/day09/parse.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,38 @@ func parse(
2020

2121
return disk
2222
}
23+
24+
type file struct {
25+
id int
26+
size int
27+
start int
28+
moved bool
29+
}
30+
31+
func parseFileList(
32+
input string,
33+
) []file {
34+
files := make([]file, 0, len(input))
35+
36+
start := 0
37+
for i, ch := range input {
38+
size := int(ch - '0')
39+
var id int
40+
if i%2 == 0 {
41+
id = i / 2
42+
} else {
43+
id = -1
44+
}
45+
46+
f := file{
47+
id: id,
48+
size: size,
49+
start: start,
50+
moved: false,
51+
}
52+
start += size
53+
files = append(files, f)
54+
}
55+
56+
return files
57+
}

0 commit comments

Comments
 (0)