-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09_1.go
55 lines (48 loc) · 1.04 KB
/
day09_1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package day09
import (
"strings"
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
func compactFiles(files *[]int) []int {
fileIdx := len((*files)) - 1
gapIdx := 0
for gapIdx < fileIdx {
val := (*files)[gapIdx]
if val == -1 {
for fileIdx > gapIdx && (*files)[fileIdx] == -1 {
fileIdx--
}
if fileIdx > gapIdx {
fileVal := (*files)[fileIdx]
(*files)[gapIdx] = fileVal
(*files)[fileIdx] = -1
}
} else {
gapIdx++
}
}
return (*files)[:gapIdx]
}
func Executepart1() int {
var result int = 0
var fileName string = "./day09/day09.txt"
if fileContent, err := utilities.ReadFileAsText(fileName); err == nil {
fileIdx := 0
var tmpIdx int
disk := make([]int, 0)
for cIdx, r := range strings.Split(fileContent, "") {
if cIdx%2 == 0 {
tmpIdx = fileIdx
fileIdx++
} else {
tmpIdx = -1
}
for sIdx := 0; sIdx < utilities.StringToInt(r); sIdx++ {
disk = append(disk, tmpIdx)
}
}
compacteddisk := compactFiles(&disk)
result = int(checksum(&compacteddisk))
}
return result
}