-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.go
111 lines (102 loc) · 2.87 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
package main
import (
"fmt"
)
func day10() {
grid := getLines("input/10.txt")
startingPoint := findInGrid(grid, 'S')
visited := map[Point]int{startingPoint: 0}
notChecked := []Point{startingPoint}
maxDist := 0
for len(notChecked) > 0 {
current := notChecked[0]
notChecked = notChecked[1:]
next := nextPoints(grid, current)
for _, point := range next {
if _, found := visited[point]; !found {
visited[point] = visited[current] + 1
maxDist = max(maxDist, visited[current]+1)
notChecked = append(notChecked, point)
}
}
}
var result = maxDist
fmt.Println("Day 10 Part 1 Result: ", result)
countInside := 0
for y, row := range grid {
inside := false
for x := 0; x < len(row); x++ {
tile := row[x]
point := Point{x, y}
if tile == 'S' {
tile = findStartTile(point, grid)
}
if _, part := visited[point]; part {
if tile == '|' || tile == 'L' || tile == 'J' {
inside = !inside
}
} else if inside {
countInside++
}
}
}
var result2 = countInside
fmt.Println("Day 10 Part 2 Result: ", result2)
}
func findStartTile(start Point, grid []string) byte {
points := nextPoints(grid, start)
minx, maxx, miny, maxy := min(points[0].x, points[1].x), max(points[0].x, points[1].x), min(points[0].y, points[1].y), max(points[0].y, points[1].y)
if points[0].x == points[1].x {
return '|'
} else if points[0].y == points[1].y {
return '-'
} else if minx < start.x && miny < start.y {
return 'J'
} else if maxx > start.x && maxy > start.y {
return 'F'
} else if maxx > start.x && miny < start.y {
return 'L'
} else if minx < start.x && maxy > start.y {
return '7'
}
return '.'
}
func nextPoints(grid []string, p Point) []Point {
points := []Point{}
switch grid[p.y][p.x] {
case '|':
points = append(points, Point{p.x, p.y + 1})
points = append(points, Point{p.x, p.y - 1})
case '-':
points = append(points, Point{p.x + 1, p.y})
points = append(points, Point{p.x - 1, p.y})
case 'L':
points = append(points, Point{p.x, p.y - 1})
points = append(points, Point{p.x + 1, p.y})
case 'J':
points = append(points, Point{p.x, p.y - 1})
points = append(points, Point{p.x - 1, p.y})
case '7':
points = append(points, Point{p.x, p.y + 1})
points = append(points, Point{p.x - 1, p.y})
case 'F':
points = append(points, Point{p.x, p.y + 1})
points = append(points, Point{p.x + 1, p.y})
case '.':
case 'S':
down, right, up, left := grid[p.y+1][p.x], grid[p.y][p.x+1], grid[p.y-1][p.x], grid[p.y][p.x-1]
if down == '|' || down == 'L' || down == 'J' {
points = append(points, Point{p.x, p.y + 1})
}
if right == '-' || right == '7' || right == 'J' {
points = append(points, Point{p.x + 1, p.y})
}
if up == '|' || up == '7' || up == 'F' {
points = append(points, Point{p.x, p.y - 1})
}
if left == '-' || left == 'L' || left == 'F' {
points = append(points, Point{p.x - 1, p.y})
}
}
return points
}