-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.go
58 lines (49 loc) · 1019 Bytes
/
day10.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
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
)
func runCycles(lines []string, cycles []int, w http.ResponseWriter) int {
sum := 0
cycleCounter := 1
frequency := 0
instruction := make(map[int]int)
x := 1
readNext := true
for i := 0; cycleCounter <= 240; cycleCounter++ {
if readNext {
if i == (len(lines)) {
i = 0
}
parts := strings.Split(lines[i], " ")
if parts[0] == "addx" {
value, _ := strconv.Atoi(parts[1])
instruction[cycleCounter+1] = value
readNext = false
} else {
i++
}
}
currentPosition := cycleCounter%40 - 1
if x >= currentPosition-1 && x <= currentPosition+1 {
fmt.Fprintf(w, "#")
} else {
fmt.Fprintf(w, ".")
}
if cycleCounter <= cycles[len(cycles)-1] && cycleCounter == cycles[frequency] {
sum += x * cycles[frequency]
frequency++
}
if instruction[cycleCounter] != 0 {
x += instruction[cycleCounter]
i++
readNext = true
}
if cycleCounter%40 == 0 {
fmt.Fprintln(w)
}
}
return sum
}