-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10p2.go
60 lines (47 loc) · 1.16 KB
/
10p2.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
package main
import (
"fmt"
"strconv"
"aoc2022/utils"
)
func D10P2() {
instructions := utils.ReadLines("inputs/10.txt")
registerValue := 1
cycleNumber := 1
screen := []string{}
for _, instruction := range instructions {
if instruction[0] == 'a' {
drawPixel(&screen, cycleNumber, registerValue)
cycleNumber++
drawPixel(&screen, cycleNumber, registerValue)
cycleNumber++
instructionValue, _ := strconv.Atoi(instruction[5:])
registerValue += instructionValue
continue
}
drawPixel(&screen, cycleNumber, registerValue)
cycleNumber++
}
for _, line := range screen {
fmt.Println(line)
}
}
func drawPixel(screen *[]string, cycleNumber int, registerValue int) {
screenX := cycleNumber % 40
pixelValue := "."
if screenX == 0 {
screenX = 40
}
// Check if sprite position collides with the current pixel
switch screenX {
case registerValue, registerValue + 1, registerValue + 2:
pixelValue = "#"
}
// If starting a new line, add a new line to the screen
if screenX == 1 {
*screen = append(*screen, pixelValue)
return
}
// Otherwise, add the pixel to the end of the current line
(*screen)[len(*screen)-1] += pixelValue
}