This repository was archived by the owner on Dec 28, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +53
-2
lines changed Expand file tree Collapse file tree 3 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ automatically rebuilt and redeployed ever time the `common` module or their own
53
53
| 06 | ⭐ ⭐ | 19 | |
54
54
| 07 | ⭐ ⭐ | 20 | |
55
55
| 08 | ⭐ ⭐ | 21 | |
56
- | 09 | | 22 | |
56
+ | 09 | ⭐ | 22 | |
57
57
| 10 | ⭐ ⭐ | 23 | |
58
58
| 11 | ⭐ ⭐ | 24 | |
59
59
| 12 | ⭐ ⭐ | 25 | |
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "fmt"
4
5
"github.com/terminalnode/adventofcode2024/common"
5
6
)
6
7
7
8
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 )
9
38
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments