File tree Expand file tree Collapse file tree 4 files changed +118
-16
lines changed Expand file tree Collapse file tree 4 files changed +118
-16
lines changed Original file line number Diff line number Diff line change
1
+ 2333133121414131402
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "fmt"
6
+ "os"
7
+
8
+ "github.com/fxnn/adventofcode2024/util"
9
+ )
10
+
11
+ const FREE = - 1
12
+
13
+ func calculateDiskBlocks (diskMap string ) []int {
14
+ var diskBlocks = []int {}
15
+
16
+ var freeSpace = false
17
+ var fileId = 0
18
+ for _ , b := range diskMap {
19
+ var blockCount = util .Atoi (string (b ))
20
+ var blockUsage int
21
+ if freeSpace {
22
+ blockUsage = FREE
23
+ } else {
24
+ blockUsage = fileId
25
+ fileId ++
26
+ }
27
+ diskBlocks = append (diskBlocks , util .Times (blockCount , blockUsage )... )
28
+ freeSpace = ! freeSpace
29
+ }
30
+
31
+ return diskBlocks
32
+ }
33
+
34
+ func printDiskBlocks (diskBlocks []int ) {
35
+ for _ , b := range diskBlocks {
36
+ if b == FREE {
37
+ fmt .Print ("." )
38
+ } else {
39
+ fmt .Printf ("%d" , b )
40
+ }
41
+ }
42
+ }
43
+
44
+ func compact (in []int ) []int {
45
+ var out = make ([]int , len (in ))
46
+ var j = len (out ) - 1
47
+ for i := range in {
48
+ for j >= 0 && in [j ] == FREE {
49
+ j --
50
+ }
51
+ if i > j {
52
+ out [i ] = FREE
53
+ } else if in [i ] == FREE {
54
+ out [i ] = in [j ]
55
+ j --
56
+ } else {
57
+ out [i ] = in [i ]
58
+ }
59
+ }
60
+ return out
61
+ }
62
+
63
+ func checksum (diskBlocks []int ) int {
64
+ var checksum = 0
65
+ for i , b := range diskBlocks {
66
+ if b != FREE {
67
+ checksum += i * b
68
+ }
69
+ }
70
+ return checksum
71
+ }
72
+
73
+ func main () {
74
+ scanner := bufio .NewScanner (os .Stdin )
75
+ scanner .Split (bufio .ScanLines )
76
+
77
+ scanner .Scan ()
78
+ var diskMap = scanner .Text ()
79
+
80
+ var diskBlocks = calculateDiskBlocks (diskMap )
81
+ printDiskBlocks (diskBlocks )
82
+ fmt .Println ()
83
+
84
+ diskBlocks = compact (diskBlocks )
85
+ printDiskBlocks (diskBlocks )
86
+ fmt .Println ()
87
+
88
+ var checksum = checksum (diskBlocks )
89
+ fmt .Printf ("checksum: %d\n " , checksum )
90
+ }
Original file line number Diff line number Diff line change
1
+ package util
2
+
3
+ import "fmt"
4
+
5
+ func Times [T any ](size int , value T ) []T {
6
+ a := make ([]T , size )
7
+ for i := range a {
8
+ a [i ] = value
9
+ }
10
+ return a
11
+ }
12
+
13
+ // RemoveElement removes element with given index from slice
14
+ func RemoveElement (slice []int , index int ) ([]int , error ) {
15
+ if index < 0 || index >= len (slice ) {
16
+ return nil , fmt .Errorf ("index out of range" )
17
+ }
18
+
19
+ // Create a new slice and copy the elements before the index
20
+ newSlice := make ([]int , 0 , len (slice )- 1 )
21
+ newSlice = append (newSlice , slice [:index ]... )
22
+
23
+ // Append the elements after the index
24
+ newSlice = append (newSlice , slice [index + 1 :]... )
25
+
26
+ return newSlice , nil
27
+ }
Original file line number Diff line number Diff line change @@ -52,19 +52,3 @@ func Sign(x int) int {
52
52
}
53
53
return - 1
54
54
}
55
-
56
- // RemoveElement removes element with given index from slice
57
- func RemoveElement (slice []int , index int ) ([]int , error ) {
58
- if index < 0 || index >= len (slice ) {
59
- return nil , fmt .Errorf ("index out of range" )
60
- }
61
-
62
- // Create a new slice and copy the elements before the index
63
- newSlice := make ([]int , 0 , len (slice )- 1 )
64
- newSlice = append (newSlice , slice [:index ]... )
65
-
66
- // Append the elements after the index
67
- newSlice = append (newSlice , slice [index + 1 :]... )
68
-
69
- return newSlice , nil
70
- }
You can’t perform that action at this time.
0 commit comments