-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.go
163 lines (153 loc) · 4.07 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
type Pipe struct {
pipe rune
directions [][]int
value int
visited bool
map_value string
loop bool
scanned_loop string
}
func createPipe(pipe rune) Pipe {
if pipe == '|' {
return Pipe{
pipe: '|', directions: [][]int{{1, 0}, {-1, 0}}, value: 0, visited: false, map_value: "|", loop: false,
}
} else if pipe == '-' {
return Pipe{
pipe: '-', directions: [][]int{{0, 1}, {0, -1}}, value: 0, visited: false, map_value: "-", loop: false,
}
} else if pipe == 'L' {
return Pipe{
pipe: 'L', directions: [][]int{{-1, 0}, {0, 1}}, value: 0, visited: false, map_value: "L", loop: false,
}
} else if pipe == 'J' {
return Pipe{
pipe: 'J', directions: [][]int{{-1, 0}, {0, -1}}, value: 0, visited: false, map_value: "J", loop: false,
}
} else if pipe == '7' {
return Pipe{
pipe: '7', directions: [][]int{{1, 0}, {0, -1}}, value: 0, visited: false, map_value: "7", loop: false,
}
} else if pipe == 'F' {
return Pipe{
pipe: 'F', directions: [][]int{{1, 0}, {0, 1}}, value: 0, visited: false, map_value: "F", loop: false,
}
} else if pipe == '.' {
return Pipe{
pipe: '.', directions: [][]int{}, value: 0, visited: false, map_value: ".", loop: false,
}
} else {
return Pipe{
pipe: 'S', directions: [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, value: 0, visited: false, map_value: "S", loop: true,
}
}
}
func translateCoords(one []int, two []int) []int {
return []int{one[0] + two[0], one[1] + two[1]}
}
func connect(dest_pipe Pipe, connect []int) bool {
for _, connection := range dest_pipe.directions {
if connection[0]*-1 == connect[0] && connection[1]*-1 == connect[1] {
return true
}
}
return false
}
func main() {
lines := utils.ReadInput("input.txt")
grid_pipes := make(map[int]map[int]Pipe)
start := []int{0, 0}
fmt.Println(len(lines[1]))
for y, line := range lines {
for x, pipe := range line {
if _, ok := grid_pipes[y]; ok {
grid_pipes[y][x] = createPipe(pipe)
} else {
grid_pipes[y] = make(map[int]Pipe)
grid_pipes[y][x] = createPipe(pipe)
}
if pipe == 'S' {
start = []int{y, x}
}
}
}
current_coords := [][]int{start}
jumps := []int{}
for len(current_coords) != 0 {
processed_pipe := current_coords[0]
pipe := grid_pipes[processed_pipe[0]][processed_pipe[1]]
pipe.visited = true
grid_pipes[processed_pipe[0]][processed_pipe[1]] = pipe
for _, next := range pipe.directions {
new_coord := translateCoords(processed_pipe, next)
new_pipe := grid_pipes[new_coord[0]][new_coord[1]]
if connect(new_pipe, next) && (new_pipe.value <= pipe.value) && !new_pipe.visited {
current_coords = append(current_coords, new_coord)
new_pipe.value = pipe.value + 1
new_pipe.map_value = fmt.Sprint(new_pipe.value)
new_pipe.loop = true
grid_pipes[new_coord[0]][new_coord[1]] = new_pipe
}
}
current_coords = current_coords[1:]
}
for y := 0; y < len(grid_pipes); y++ {
for x := 0; x < len(grid_pipes[y]); x++ {
if grid_pipes[y][x].loop {
fmt.Print("*")
} else {
fmt.Print(".")
}
jumps = append(jumps, grid_pipes[y][x].value)
}
fmt.Println()
}
for y := 0; y < len(grid_pipes); y++ {
for x := 0; x < len(grid_pipes[y]); x++ {
pipe := grid_pipes[y][x]
if pipe.loop {
if pipe.pipe == '-' || pipe.pipe == 'L' || pipe.pipe == 'J' {
pipe.scanned_loop = "."
grid_pipes[y][x] = pipe
fmt.Print(".")
} else if pipe.pipe == 'S' {
fmt.Print("S")
} else {
pipe.scanned_loop = "*"
grid_pipes[y][x] = pipe
fmt.Print("*")
}
} else {
pipe.scanned_loop = "."
grid_pipes[y][x] = pipe
fmt.Print(".")
}
}
fmt.Println()
}
inside := 0
for y := 0; y < len(grid_pipes); y++ {
crossed := 0
for x := 0; x < len(grid_pipes[y]); x++ {
pipe := grid_pipes[y][x]
if pipe.scanned_loop == "*" {
crossed++
fmt.Print("*")
} else if pipe.loop {
fmt.Print(string(pipe.pipe))
} else if crossed%2 == 0 {
fmt.Print("O")
} else {
inside++
fmt.Print("I")
}
}
fmt.Println()
}
fmt.Println(inside)
}