-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoc15.go
131 lines (107 loc) · 1.97 KB
/
aoc15.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"fmt"
"os"
"slices"
"strings"
)
func main() {
boxes := newBoxes()
sum := 0
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
ops := strings.Split(input.Text(), ",")
for i, s := range ops {
sum += hash(ops[i])
op := mkop(i, s)
boxes[hash(op.name)].enqueue(op)
}
}
pwr := 0
for i := range boxes {
if len(boxes[i].add) > 0 {
pwr += boxes[i].process()
}
}
fmt.Println(sum, pwr)
}
func hash(s string) (h int) {
for i := range s {
h = ((h + int(s[i])) * 17) & 0xff
}
return
}
func newBoxes() []*queue {
boxes := make([]*queue, 256)
for i := range boxes {
q := new(queue)
q.idx = i
q.del = make(map[string]int, 16) // max == 9
q.add = make([]ops, 0, 64) // max == 48
boxes[i] = q
}
return boxes
}
type ops struct {
name string
idx, val int
}
func mkop(i int, s string) ops {
op := ops{idx: i}
switch s[len(s)-1] {
case '-':
op.name = s[:len(s)-1]
default:
args := strings.Split(s, "=")
op.name, op.val = args[0], atoi(args[1])
}
return op
}
type queue struct {
del map[string]int
add []ops
idx int
}
// var MAXDEL = 9
// var MAXADD = 48
func (q *queue) enqueue(op ops) {
if op.val == 0 {
q.del[op.name] = op.idx
return
}
if i, ok := q.del[op.name]; !ok || (ok && op.idx > i) {
q.add = append(q.add, op)
}
return
}
func (q *queue) process() int {
slots := make([]ops, 0, len(q.add))
for _, op := range q.add {
if i, ok := q.del[op.name]; !ok || (ok && op.idx > i) {
if i := index(slots, op); i >= 0 {
slots[i].val = op.val
} else {
slots = append(slots, op)
}
}
}
sum := 0
for i := range slots {
if slots[i].name != "" {
sum += (q.idx + 1) * (i + 1) * slots[i].val
}
}
return sum
}
func index(list []ops, o ops) int {
return slices.IndexFunc(list, func(x ops) bool { return x.name == o.name })
}
// strconv.Atoi simplified core loop
// s is ^\d+$
func atoi(s string) (n int) {
for i := range s {
n = 10*n + int(s[i]-'0')
}
return
}