This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
/
d8.go
177 lines (149 loc) · 3 KB
/
d8.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package d8
import (
"fmt"
"strconv"
"strings"
"sync"
"golang.org/x/sync/errgroup"
)
// Operation ..
type Operation int
const (
nop Operation = iota
acc
jmp
)
// Instruction ..
type Instruction struct {
Op Operation
Val int
}
// DecodeOne ..
func DecodeOne(s string) (*Instruction, error) {
split := strings.Split(s, " ")
if len(split) != 2 {
return nil, fmt.Errorf("unrecognizable string: %s", s)
}
op, err := func() (Operation, error) {
switch split[0] {
case "nop":
return nop, nil
case "acc":
return acc, nil
case "jmp":
return jmp, nil
}
return nop, fmt.Errorf("unrecognizable operation: %s", split[0])
}()
if err != nil {
return nil, err
}
num, err := strconv.Atoi(split[1])
if err != nil {
return nil, fmt.Errorf("unrecognizable number: %s", split[1])
}
return &Instruction{op, num}, nil
}
// ParseInput ..
func ParseInput(f []byte) ([]*Instruction, error) {
split := strings.Split(string(f), "\n")
out := make([]*Instruction, len(split))
errgrp := &errgroup.Group{}
for ind, line := range split {
errgrp.Go(func(i int, c string) func() error {
return func() error {
ins, err := DecodeOne(c)
if err != nil {
return err
}
out[i] = ins
return nil
}
}(ind, line))
}
err := errgrp.Wait()
if err != nil {
return nil, err
}
return out, nil
}
// SolvePart1 .
func SolvePart1(inst []*Instruction) int {
reg := 0
track := make(map[int]struct{})
curser := 0
for {
if _, ok := track[curser]; ok {
break
}
if curser >= len(inst) {
break
}
track[curser] = struct{}{}
switch inst[curser].Op {
case acc:
reg += inst[curser].Val
curser++
case nop:
curser++
case jmp:
curser += inst[curser].Val
}
}
return reg
}
// SolvePart2 .
func SolvePart2(inst []*Instruction) (int, error) {
indicesToFlip := []int{}
for i, v := range inst {
if v.Op == nop || v.Op == jmp {
indicesToFlip = append(indicesToFlip, i)
}
}
successfulOutputs := []int{}
mu := &sync.Mutex{}
wg := &sync.WaitGroup{}
wg.Add(len(indicesToFlip))
for _, ind := range indicesToFlip {
go func(indToMutate int) {
defer wg.Done()
copied := make([]*Instruction, len(inst))
copy(copied, inst)
switch copied[indToMutate].Op {
case nop:
copied[indToMutate] = &Instruction{jmp, copied[indToMutate].Val}
case jmp:
copied[indToMutate] = &Instruction{nop, copied[indToMutate].Val}
}
reg := 0
track := make(map[int]struct{})
curser := 0
for {
if _, ok := track[curser]; ok {
return
}
if curser >= len(copied) {
mu.Lock()
successfulOutputs = append(successfulOutputs, reg)
mu.Unlock()
return
}
track[curser] = struct{}{}
switch copied[curser].Op {
case acc:
reg += copied[curser].Val
curser++
case nop:
curser++
case jmp:
curser += copied[curser].Val
}
}
}(ind)
}
wg.Wait()
if len(successfulOutputs) != 1 {
return 0, fmt.Errorf("got %v ones that work", len(successfulOutputs))
}
return successfulOutputs[0], nil
}