Skip to content

Commit cbf3c72

Browse files
committed
feat(2024): add day 9 part 1
1 parent e7ccf7f commit cbf3c72

File tree

4 files changed

+127
-10
lines changed

4 files changed

+127
-10
lines changed

go/2024/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
2222
| [Day 6: Guard Gallivant](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day06/main.go) | 🌟 | 4778 | 🌟 | 1618 |
2323
| [Day 7: Bridge Repair](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day07/main.go) | 🌟 | 1399219271639 | 🌟 | 275791737999003 |
2424
| [Day 8: Resonant Collinearity](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day08/main.go) | 🌟 | 220 | 🌟 | 813 |
25+
| [Day 9: Disk Fragmenter](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day09/main.go) | 🌟 | 6384282079460 | | |
2526

2627
## Benchmarks
2728

2829
Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#hdr-Benchmarks) package. Computer is a 2021 MacBook Pro M1 Pro, 32 GB RAM.
2930

30-
| Day | #1 | #2 | Improvement\* |
31-
| --- | -------------: | ---------------: | ------------------- |
32-
| 1 | 116264 ns/op | 131233 ns/op | `3.53%` / `68.43%` |
33-
| 2 | 310935 ns/op | 723512 ns/op | |
34-
| 3 | 336448 ns/op | 785320 ns/op | - / `36.98%` |
35-
| 4 | 523315 ns/op | 217584 ns/op | `81.73%` / `26.09%` |
36-
| 5 | 778880 ns/op | 3129873 ns/op | `53.34%` / `81.91%` |
37-
| 6 | 312461 ns/op | 1153391125 ns/op | |
38-
| 7 | 16480300 ns/op | 842853000 ns/op | `87.01%` / `91.67%` |
39-
| 8 | 58749 ns/op | 121247 ns/op | |
31+
| Day | #1 | #2 | Improvement\* |
32+
| --- | --------------: | ---------------: | ------------------- |
33+
| 1 | 116264 ns/op | 131233 ns/op | `3.53%` / `68.43%` |
34+
| 2 | 310935 ns/op | 723512 ns/op | |
35+
| 3 | 336448 ns/op | 785320 ns/op | - / `36.98%` |
36+
| 4 | 523315 ns/op | 217584 ns/op | `81.73%` / `26.09%` |
37+
| 5 | 778880 ns/op | 3129873 ns/op | `53.34%` / `81.91%` |
38+
| 6 | 312461 ns/op | 1153391125 ns/op | |
39+
| 7 | 16480300 ns/op | 842853000 ns/op | `87.01%` / `91.67%` |
40+
| 8 | 58749 ns/op | 121247 ns/op | |
41+
| 9 | 381476181 ns/op | | |
4042

4143
\* compared to first solution
4244

go/2024/puzzles/day09/main.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/believer/aoc-2024/utils"
10+
"github.com/believer/aoc-2024/utils/files"
11+
)
12+
13+
func main() {
14+
fmt.Println("Part 1: ", part1("input.txt"))
15+
fmt.Println("Part 2: ", part2("input.txt"))
16+
}
17+
18+
func part1(name string) int {
19+
line := strings.TrimSpace(files.Read(name))
20+
blocked := []string{}
21+
fileId := 0
22+
fileBlocks := 0
23+
24+
// Build files and free space
25+
for i, digit := range strings.Split(line, "") {
26+
digitAsInt := utils.MustIntFromString(digit)
27+
28+
for range digitAsInt {
29+
if i%2 == 0 {
30+
blocked = append(blocked, strconv.Itoa(fileId))
31+
fileBlocks += 1
32+
} else {
33+
blocked = append(blocked, ".")
34+
}
35+
}
36+
37+
if i%2 == 0 {
38+
fileId++
39+
}
40+
}
41+
42+
// Compact file system
43+
for i := 1; i < len(blocked); i++ {
44+
rune := blocked[len(blocked)-i]
45+
46+
if rune == "." {
47+
continue
48+
}
49+
50+
firstEmpty := slices.Index(blocked, ".")
51+
52+
// The blocks have been compacted
53+
if firstEmpty == fileBlocks {
54+
break
55+
}
56+
57+
blocked[firstEmpty] = rune
58+
blocked[len(blocked)-i] = "."
59+
}
60+
61+
// Calculate checksum
62+
checksum := 0
63+
64+
for i, rune := range blocked {
65+
if rune == "." {
66+
break
67+
}
68+
69+
digit := utils.MustIntFromString(string(rune))
70+
71+
checksum += i * digit
72+
}
73+
74+
return checksum
75+
}
76+
77+
func part2(name string) int {
78+
return 0
79+
}

go/2024/puzzles/day09/main_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPart1(t *testing.T) {
10+
t.Run("Part 1", func(t *testing.T) {
11+
expected := 1928
12+
actual := part1("test-input.txt")
13+
assert.Equal(t, expected, actual)
14+
})
15+
}
16+
17+
func TestPart2(t *testing.T) {
18+
t.Run("Part 2", func(t *testing.T) {
19+
expected := 0
20+
actual := part2("test-input.txt")
21+
assert.Equal(t, expected, actual)
22+
})
23+
}
24+
25+
func BenchmarkPart1(b *testing.B) {
26+
for i := 0; i < b.N; i++ {
27+
part1("input.txt")
28+
}
29+
}
30+
31+
func BenchmarkPart2(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
part2("input.txt")
34+
}
35+
}

go/2024/puzzles/day09/test-input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2333133121414131402

0 commit comments

Comments
 (0)