-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_part1.go
104 lines (88 loc) · 1.85 KB
/
day8_part1.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
package day8_part1
import (
"strings"
"GoAdventOfCode/util"
)
const (
screenWidth = 50
screenHeight = 6
)
type Screen [][screenWidth]int
func getAnswer(lines []string) int {
screen := make(Screen, screenHeight)
for _, line := range lines {
parts := strings.Fields(line)
cmd := parts[0]
if cmd == "rect" {
numbers := strings.Split(parts[1], "x")
a := util.ConvertStringToInt(numbers[0])
b := util.ConvertStringToInt(numbers[1])
screen = doCommandRect(screen, a, b)
continue
}
if cmd == "rotate" {
numbers := strings.Split(parts[2], "=")
d := util.ConvertStringToInt(numbers[1])
n := util.ConvertStringToInt(parts[4])
if parts[1] == "column" {
screen = doCommandRotateColumn(screen, d, n)
} else {
screen = doCommandRotateRow(screen, d, n)
}
continue
}
}
printScreen(screen)
return countVisiblePixels(screen)
}
func doCommandRect(screen Screen, a int, b int) Screen {
for y := 0; y < b; y++ {
for x := 0; x < a; x++ {
screen[y][x] = 1
}
}
return screen
}
func doCommandRotateColumn(screen Screen, x int, n int) Screen {
for i := 0; i < n; i++ {
temp := screen[screenHeight-1][x]
for j := screenHeight - 1; j > 0; j-- {
screen[j][x] = screen[j-1][x]
}
screen[0][x] = temp
}
return screen
}
func doCommandRotateRow(screen Screen, y int, n int) Screen {
for i := 0; i < n; i++ {
temp := screen[y][screenWidth-1]
for j := screenWidth - 1; j > 0; j-- {
screen[y][j] = screen[y][j-1]
}
screen[y][0] = temp
}
return screen
}
func countVisiblePixels(screen Screen) int {
total := 0
for _, row := range screen {
for _, pixel := range row {
if pixel == 1 {
total++
}
}
}
return total
}
func printScreen(screen Screen) {
for _, row := range screen {
for _, pixel := range row {
if pixel == 1 {
print("#")
} else {
print(".")
}
}
println()
}
}