-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.go
106 lines (93 loc) · 1.64 KB
/
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
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
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input/day10
var d10 string
type operation struct {
op string
v int
}
func day10(input string) (int, int) {
rows := strings.Split(input, "\n")
instructions := make([]operation, 0, len(rows))
for _, row := range rows {
if row == "" {
continue
}
r := strings.Split(row, " ")
if len(r) == 2 {
instructions = append(instructions, operation{op: r[0], v: mustAtoi(r[1])})
} else {
instructions = append(instructions, operation{op: r[0], v: 0})
}
}
return d10p1(instructions), d10p2(instructions)
}
func d10p2(instructions []operation) int {
cycles := 0
x := 1
for i := 0; i < len(instructions); i++ {
printCRT(cycles, x)
cycles++
if instructions[i].op == "addx" {
printCRT(cycles, x)
cycles++
x += instructions[i].v
}
}
fmt.Println()
return 0
}
func d10p1(instructions []operation) int {
var (
cycles int
pc int
tmpCycle int
strengths int
)
x := 1
for pc < len(instructions) {
printCRT(cycles, x)
cycles++
switch cycles {
case 20, 60, 100, 140, 180, 220:
strengths += x * cycles
}
op := instructions[pc]
switch op.op {
case "addx":
tmpCycle++
if tmpCycle == 2 {
printCRT(cycles, x)
tmpCycle = 0
x += op.v
pc++
}
case "noop":
tmpCycle = 0
pc++
}
}
return strengths
}
func printCRT(cycles, x int) {
if cycles%40 == 0 && cycles <= 220 {
fmt.Println()
}
if x-1 == cycles%40 || x == (cycles%40) || x+1 == (cycles%40) {
fmt.Print("#")
} else {
fmt.Print(".")
}
}
func mustAtoi(s string) int {
v, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return v
}