-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.07 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"slices"
"strings"
"github.com/devkvlt/aoc"
)
var steps = strings.Split(aoc.Lines("input")[0], ",")
func hash(s string) int {
n := 0
for _, ch := range s {
n += int(ch)
n *= 17
n %= 256
}
return n
}
func part1() {
sum := 0
for _, s := range steps {
sum += hash(s)
}
fmt.Println(sum)
}
func part2() {
boxes := make([][]string, 256)
focalLengths := map[string]int{}
for _, s := range steps {
if s[len(s)-1] == '-' {
label := s[:len(s)-1]
bi := hash(label)
li := slices.Index(boxes[bi], label)
if li != -1 {
boxes[bi] = append(boxes[bi][:li], boxes[bi][li+1:]...)
}
delete(focalLengths, label)
} else {
label := s[:len(s)-2]
bi := hash(label)
li := slices.Index(boxes[bi], label)
if li == -1 {
boxes[bi] = append(boxes[bi], label)
}
focalLengths[label] = aoc.Atoi(s[len(s)-1:])
}
}
sum := 0
for i, box := range boxes {
for j, lens := range box {
sum += (i + 1) * (j + 1) * focalLengths[lens]
}
}
fmt.Println(sum)
}
func main() {
part1() // 515974
part2() // 265894
}