-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
60 lines (48 loc) · 1.09 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
package main
import (
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, m map[int]int) int {
s := lib.ReaderToString(input)
codes := lib.StringsToInts(strings.Split(s, ","))
for k, v := range m {
codes[k] = v
}
for i := 0; i < len(codes); i += 4 {
if codes[i] == 1 {
codes[codes[i+3]] = codes[codes[i+1]] + codes[codes[i+2]]
} else if codes[i] == 2 {
codes[codes[i+3]] = codes[codes[i+1]] * codes[codes[i+2]]
} else if codes[i] == 99 {
break
}
}
return codes[0]
}
func fs2(input io.Reader) int {
s := lib.ReaderToString(input)
codes := lib.StringsToInts(strings.Split(s, ","))
for noun := 0; noun < len(codes); noun++ {
for verb := 0; verb < len(codes); verb++ {
codes[1] = noun
codes[2] = verb
c := make([]int, len(codes))
copy(c, codes)
for i := 0; i < len(c); i += 4 {
if c[i] == 1 {
c[c[i+3]] = c[c[i+1]] + c[c[i+2]]
} else if c[i] == 2 {
c[c[i+3]] = c[c[i+1]] * c[c[i+2]]
} else if c[i] == 99 {
break
}
}
if c[0] == 19690720 {
return 100*noun + verb
}
}
}
return -1
}