-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (59 loc) · 936 Bytes
/
main.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
package main
import (
"fmt"
"strconv"
"github.com/devkvlt/aoc"
)
func part1(lines []string) int {
strength := 0
x := 1
cycle := 1
add := func() {
switch cycle {
case 20, 60, 100, 140, 180, 220:
strength += cycle * x
}
}
for _, line := range lines {
cycle++
add()
if line != "noop" {
v, _ := strconv.Atoi(line[5:])
x += v
cycle++
add()
}
}
return strength
}
func part2(lines []string) {
x := 1
cycle := 1
screen := ""
draw := func() {
pos := (cycle - 1) % 40
if x-1 <= pos && pos <= x+1 {
screen += "#"
} else {
screen += "."
}
}
for _, line := range lines {
draw()
cycle++
if line != "noop" {
draw()
v, _ := strconv.Atoi(line[5:])
x += v
cycle++
}
}
for i := 0; i < 6; i++ {
fmt.Println(screen[i*40 : (i+1)*40])
}
}
func main() {
lines := aoc.Lines("input")
fmt.Println(part1(lines)) // 12980
part2(lines) // BRJLFULP
}