-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (123 loc) Β· 2.56 KB
/
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"math"
"os"
"time"
"github.com/sanderploegsma/advent-of-code/2019/go/utils"
)
const (
Up int = iota
Right
Down
Left
)
const (
Black = 0
White = 1
)
func main() {
instructions, err := utils.ReadIntCode("input.txt")
if err != nil {
fmt.Printf("failed to read input: %v\n", err)
os.Exit(1)
}
start := time.Now()
result := Paint(instructions, Black)
fmt.Printf("[PART 1] panels painted: %d (took %s)\n", len(result), time.Since(start))
start = time.Now()
result = Paint(instructions, White)
fmt.Printf("[PART 2] panels painted: %d (took %s)\n", len(result), time.Since(start))
fmt.Println("Registration identifier:")
draw(result)
}
type Point struct{ x, y int }
func Paint(instructions []int, startingColor int) map[Point]int {
in := make(chan int, 1)
defer close(in)
out := make(chan int)
go utils.RunIntCode(in, out, instructions)
painted := make(map[Point]int)
position := Point{0, 0}
direction := Up
painted[position] = startingColor
if c, ok := painted[position]; ok {
in <- c
} else {
in <- Black
}
for color := range out {
turn := <-out
painted[position] = color
position, direction = move(position, direction, turn)
if c, ok := painted[position]; ok {
in <- c
} else {
in <- Black
}
}
return painted
}
func move(pos Point, dir, turn int) (newPos Point, newDir int) {
if turn == 0 {
switch dir {
case Up:
newDir = Left
newPos = Point{pos.x - 1, pos.y}
case Right:
newDir = Up
newPos = Point{pos.x, pos.y + 1}
case Down:
newDir = Right
newPos = Point{pos.x + 1, pos.y}
case Left:
newDir = Down
newPos = Point{pos.x, pos.y - 1}
}
} else {
switch dir {
case Up:
newDir = Right
newPos = Point{pos.x + 1, pos.y}
case Right:
newDir = Down
newPos = Point{pos.x, pos.y - 1}
case Down:
newDir = Left
newPos = Point{pos.x - 1, pos.y}
case Left:
newDir = Up
newPos = Point{pos.x, pos.y + 1}
}
}
return newPos, newDir
}
func draw(points map[Point]int) {
minX, minY := math.MaxInt64, math.MaxInt64
maxX, maxY := math.MinInt64, math.MinInt64
for p := range points {
minX = utils.Min(minX, p.x)
maxX = utils.Max(maxX, p.x)
minY = utils.Min(minY, p.y)
maxY = utils.Max(maxY, p.y)
}
grid := make([][]int, maxY-minY+1)
for i := range grid {
grid[i] = make([]int, maxX-minX+1)
}
for p, v := range points {
if v > 0 {
grid[p.y-minY][p.x-minX] = v
}
}
for i := len(grid) - 1; i >= 0; i-- {
for j := range grid[i] {
if grid[i][j] == 1 {
fmt.Print("β")
} else {
fmt.Print(" ")
}
}
fmt.Printf("\n")
}
}