-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent8_test.go
150 lines (135 loc) · 3.16 KB
/
advent8_test.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
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var sampleInput = `b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
`
func TestExitRightAway(t *testing.T) {
assert.Equal(t, 0, advent81(""))
}
func TestSample(t *testing.T) {
assert.Equal(t, 1, advent81(sampleInput))
}
func TestSampleSecondExercice(t *testing.T) {
assert.Equal(t, 10, advent82(sampleInput))
}
func TestEquality(t *testing.T) {
assert.True(t, condition(3, "==", 3))
assert.False(t, condition(2, "==", 3))
}
func TestIncrement(t *testing.T) {
assert.Equal(t, 5, operation(3, "inc", 2))
assert.Equal(t, 1, operation(3, "dec", 2))
}
func TestFirstExercice(t *testing.T) {
text, err := ioutil.ReadFile("./input.txt")
if err != nil {
fmt.Print(err)
}
fmt.Println(advent81(string(text)))
}
func TestSecondExercice(t *testing.T) {
text, err := ioutil.ReadFile("./input.txt")
if err != nil {
fmt.Print(err)
}
fmt.Println(advent82(string(text)))
}
func operation(registerValue int, operator string, value int) int {
fmt.Println(registerValue, operator, value)
switch operator {
case "inc":
return registerValue + value
case "dec":
return registerValue - value
}
return 0
}
func condition(registerValue int, operator string, value int) bool {
fmt.Println(registerValue, operator, value)
switch operator {
case "==":
return registerValue == value
case "!=":
return registerValue != value
case "<=":
return registerValue <= value
case ">=":
return registerValue >= value
case ">":
return registerValue > value
case "<":
return registerValue < value
}
return false
}
func advent81(input string) int {
if input == "" {
return 0
}
registers := map[string]int{}
fmt.Println(input)
for _, line := range strings.Split(input, "\n") {
if line == "" {
break
}
words := strings.Split(line, " ")
fmt.Println(words)
if condition(registers[words[4]], words[5], toInt(words[6])) {
fmt.Println(true)
fmt.Println(words[0], "is", registers[words[0]])
registers[words[0]] = operation(registers[words[0]], words[1], toInt(words[2]))
fmt.Println(words[0], "is now", registers[words[0]])
}
}
biggest := -1
for name, value := range registers {
fmt.Println(name, value)
if value > biggest {
biggest = value
}
}
return biggest
}
func advent82(input string) int {
if input == "" {
return 0
}
registers := map[string]int{}
highest := -1
fmt.Println(input)
for _, line := range strings.Split(input, "\n") {
if line == "" {
break
}
words := strings.Split(line, " ")
fmt.Println(words)
if condition(registers[words[4]], words[5], toInt(words[6])) {
fmt.Println(true)
fmt.Println(words[0], "is", registers[words[0]])
registers[words[0]] = operation(registers[words[0]], words[1], toInt(words[2]))
if registers[words[0]] > highest {
highest = registers[words[0]]
fmt.Println("Highest is now", words[0], "with", registers[words[0]])
}
fmt.Println(words[0], "is now", registers[words[0]])
}
}
return highest
}
func toInt(valueAsString string) int {
value, err := strconv.Atoi(valueAsString)
if err != nil {
fmt.Println(err)
return 0
}
return value
}