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

Commit c884780

Browse files
committed
Add solution day 9 part 1
1 parent 731aff5 commit c884780

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
package main
22

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

78
func main() {
8-
common.Setup(9, nil, nil)
9+
common.Setup(9, part1, nil)
10+
}
11+
12+
func part1(
13+
input string,
14+
) string {
15+
sum := 0
16+
disk := parse(input)
17+
backIndex := len(disk)
18+
19+
for i, id := range disk {
20+
if i >= backIndex {
21+
break
22+
}
23+
if id == -1 {
24+
backIndex--
25+
for disk[backIndex] == -1 {
26+
backIndex--
27+
}
28+
29+
if i < backIndex {
30+
sum += i * disk[backIndex]
31+
}
32+
} else {
33+
sum += i * id
34+
}
35+
}
36+
37+
return fmt.Sprintf("Sum: %d", sum)
938
}

solutions/day09/parse.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
func parse(
4+
input string,
5+
) []int {
6+
disk := make([]int, 0, len(input)*9)
7+
for i, ch := range input {
8+
size := int(ch - '0')
9+
var id int
10+
if i%2 == 0 {
11+
id = i / 2
12+
} else {
13+
id = -1
14+
}
15+
16+
for chunkI := 0; chunkI < size; chunkI++ {
17+
disk = append(disk, id)
18+
}
19+
}
20+
21+
return disk
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy