-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_exec.go
78 lines (64 loc) · 1.58 KB
/
08_exec.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
package main
import (
"fmt"
"metalim/advent/2017/lib/source"
"strconv"
. "github.com/logrusorgru/aurora"
)
var test1 = `b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10`
/*
0 1 2 - 4 5 6
c inc -20 if c == 10`
*/
type regs map[string]int
type comp func(rs regs, r string, v int) bool
type op func(rs regs, r string, v int)
var comps = map[string]comp{
"==": func(rs regs, r string, v int) bool { return rs[r] == v },
"!=": func(rs regs, r string, v int) bool { return rs[r] != v },
"<=": func(rs regs, r string, v int) bool { return rs[r] <= v },
">=": func(rs regs, r string, v int) bool { return rs[r] >= v },
"<": func(rs regs, r string, v int) bool { return rs[r] < v },
">": func(rs regs, r string, v int) bool { return rs[r] > v },
}
var ops = map[string]op{
"inc": func(rs regs, r string, v int) { rs[r] += v },
"dec": func(rs regs, r string, v int) { rs[r] -= v },
}
func main() {
var ins source.Inputs
ins = ins.Test(1, test1, `1`)
ins = ins.Test(2, test1, `10`)
for p := range ins.Advent(2017, 8) {
fmt.Println(Brown("\n" + p.Name))
ssw := p.Lines().Words()
fmt.Println(len(ssw), Black(ssw[0]).Bold())
var maxmax int
mr := regs{}
for _, l := range ssw {
val, _ := strconv.Atoi(l[2])
cval, _ := strconv.Atoi(l[6])
if comps[l[5]](mr, l[4], cval) {
ops[l[1]](mr, l[0], val)
if maxmax < mr[l[0]] {
maxmax = mr[l[0]]
}
}
}
if p.Part(1) {
var max int
for _, val := range mr {
if max < val {
max = val
}
}
p.SubmitInt(1, max)
}
if p.Part(2) {
p.SubmitInt(2, maxmax)
}
}
}