-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
139 lines (119 loc) · 2.27 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
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
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
lib "github.com/teivah/advent-of-code"
)
type State struct {
reg map[string]int
offset int
mulInstructionInvoked int
}
func (s *State) print() {
fmt.Println(s.offset, s.reg)
}
type ValueReg struct {
i *int
s *string
}
func (vr ValueReg) get(state *State) int {
if vr.i != nil {
return *vr.i
}
return state.reg[*vr.s]
}
func toValueReg(s string) ValueReg {
i, err := strconv.Atoi(s)
if err != nil {
return ValueReg{s: &s}
}
return ValueReg{i: &i}
}
type apply func(state *State)
func set(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] = y.get(state)
}
}
func sub(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] -= y.get(state)
}
}
func mul(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.mulInstructionInvoked++
state.offset++
state.reg[*x.s] *= y.get(state)
}
}
func jnz(x ValueReg, y ValueReg) apply {
return func(state *State) {
v := x.get(state)
if v != 0 {
state.offset += y.get(state)
} else {
state.offset++
}
}
}
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var cmds []apply
for scanner.Scan() {
s := scanner.Text()
del := lib.NewDelimiter(s, " ")
if strings.HasPrefix(s, "set") {
cmds = append(cmds, set(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "sub") {
cmds = append(cmds, sub(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "mul") {
cmds = append(cmds, mul(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "jnz") {
cmds = append(cmds, jnz(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else {
panic(s)
}
}
state := State{
reg: make(map[string]int),
}
for {
if state.offset >= len(cmds) {
break
}
cmds[state.offset](&state)
}
return state.mulInstructionInvoked
}
func fs2() int {
var (
b = 109900
d = 0
f = 0
g = 0
h = 0
)
for {
f = 1
for d = 2; d*d <= b; d++ {
if b%d == 0 {
f = 0
break
}
}
if f == 0 {
h++
}
g = b - 126900
b += 17
if g == 0 {
return h
}
}
}