-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (51 loc) · 1.01 KB
/
main.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
56
57
58
package main
import (
_ "embed"
"fmt"
"slices"
)
//go:embed input.txt
var input string
const empty = -1
func partOne(fileBlocks []int) int {
for leftIdx, rightIdx := 0, len(fileBlocks)-1; leftIdx < rightIdx; {
if fileBlocks[leftIdx] != empty {
leftIdx += 1
continue
}
if fileBlocks[rightIdx] == empty {
rightIdx -= 1
continue
}
fileBlocks[leftIdx] = fileBlocks[rightIdx]
fileBlocks[rightIdx] = empty
leftIdx, rightIdx = leftIdx+1, rightIdx-1
}
sum := 0
for i, val := range fileBlocks {
if val == empty {
break
}
sum += i * val
}
return sum
}
func partTwo() int {
return 0
}
func parseInputToBlocks(i string) []int {
res := []int{}
for idx, char := range i {
if idx%2 == 0 {
res = append(res, slices.Repeat([]int{idx / 2}, int(char-'0'))...)
} else {
res = append(res, slices.Repeat([]int{empty}, int(char-'0'))...)
}
}
return res
}
func main() {
fileBlocks := parseInputToBlocks(input)
fmt.Println("Part 1: ", partOne(fileBlocks))
fmt.Println("Part 2: ", partTwo())
}