-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.go
178 lines (162 loc) · 3.72 KB
/
day8.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
178
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
i, err := ioutil.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay8Part1(stringListToSlice(input)))
fmt.Printf("Part 2: %v\n", SolveDay8Part2(stringListToSlice(input)))
fmt.Printf("Part 1r: %v\n", SolveDay8Part1r(stringListToSlice(input)))
fmt.Printf("Part 2r: %v\n", SolveDay8Part2r(stringListToSlice(input)))
}
//SolveDay8Part1 returns the number of the latest number before go to loop
func SolveDay8Part1(i []string) (s int) {
checked := make(map[int]bool)
cur := 0
for {
if checked[cur] {
return
}
checked[cur] = true
if strings.HasPrefix(i[cur], "acc ") {
t := strings.TrimPrefix(i[cur], "acc ")
temp, err := strconv.Atoi(t)
if err != nil {
return 0
}
s = s + temp
} else if strings.HasPrefix(i[cur], "jmp ") {
t := strings.TrimPrefix(i[cur], "jmp ")
temp, err := strconv.Atoi(t)
if err != nil {
return 0
}
cur += temp
continue
}
cur++
}
}
//SolveDay8Part1r returns the number of the latest number before go to loop - refactor
func SolveDay8Part1r(i []string) (s int) {
checked := make(map[int]bool)
tasks := getTasks(i)
cur := 0
for {
if checked[cur] {
return
}
checked[cur] = true
for task, int := range tasks[cur] {
if task == "acc" {
s += int
cur++
break
}
if task == "jmp" {
cur += int
break
}
cur++
}
}
}
//SolveDay8Part2 returns the value after finish with exact one change
func SolveDay8Part2(i []string) (s int) {
input := i
checked := make(map[int]bool)
cur := 0
changed := 0
for {
if len(input) <= cur {
return s
}
if checked[cur] {
s, cur, checked = 0, 0, make(map[int]bool)
changed++
}
checked[cur] = true
if strings.HasPrefix(input[cur], "acc ") {
temp, err := strconv.Atoi(strings.TrimPrefix(input[cur], "acc "))
if err != nil {
return 0
}
s += temp
cur++
} else if strings.HasPrefix(input[cur], "jmp") && cur != changed || (strings.HasPrefix(input[cur], "nop") && cur == changed) {
jump, err := strconv.Atoi(strings.TrimPrefix(input[cur], "jmp "))
if err != nil {
jump, err = strconv.Atoi(strings.TrimPrefix(input[cur], "nop "))
if err != nil {
return 0
}
}
cur += jump
} else if strings.HasPrefix(input[cur], "nop") && cur != changed || (strings.HasPrefix(input[cur], "jmp") && cur == changed) {
cur++
}
}
}
//SolveDay8Part2r returns the value after finish with exact one change - refactor
func SolveDay8Part2r(i []string) (s int) {
input := i
checked := make(map[int]bool)
cur := 0
changed := 0
tasks := getTasks(i)
for {
if len(input) <= cur {
return s
}
if checked[cur] {
s, cur, checked = 0, 0, make(map[int]bool)
changed++
}
checked[cur] = true
for task, int := range tasks[cur] {
if task == "acc" {
s += int
cur++
break
}
if task == "jmp" && cur != changed || task == "nop" && cur == changed {
cur += int
break
}
if task == "nop" && cur != changed || task == "jmp" && cur == changed {
cur++
break
}
}
}
}
//getTasks returns a map with the tasks
func getTasks(input []string) (tasks map[int]map[string]int) {
tasks = make(map[int]map[string]int)
for i, task := range input {
splitTask := strings.Split(task, " ")
steps, err := strconv.Atoi(splitTask[1])
if err != nil {
return nil
}
tasks[i] = map[string]int{}
tasks[i][splitTask[0]] = steps
}
return tasks
}
//Helper functions
//stringListToSlice converts the list of strings (each string one row) to a slice
func stringListToSlice(list string) (s []string) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
s = append(s, line)
}
return
}