-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday14_sand.go
152 lines (129 loc) · 3.38 KB
/
day14_sand.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
package main
import (
"fmt"
"os"
"strings"
)
type xyMinMax struct {
xMin int
xMax int
yMin int
yMax int
}
type SandProblem struct {
grid map[Point]string
bounds xyMinMax
sandStart Point
}
func parseRockData(filePath string, addFloor bool) *SandProblem {
// +x is right; +y is down
input, _ := os.ReadFile(filePath)
rockPaths := strings.Split(strings.TrimSpace(string(input)), "\n")
grid := make(map[Point]string)
for _, rockPath := range rockPaths {
rockPoints := strings.Split(rockPath, " -> ")
prev := PointFromString(rockPoints[0])
for i := 1; i < len(rockPoints); i++ {
curr := PointFromString(rockPoints[i])
lineVector := curr.sub(*prev)
lineLength := IntAbs(lineVector.x) + IntAbs(lineVector.y)
normalizedLineVector := Point{x: lineVector.x / lineLength, y: lineVector.y / lineLength}
for j := 0; j <= lineLength; j++ {
vectorMultipliedByLoopIndex := normalizedLineVector.scalerMult(j)
newRockPoint := prev.add(vectorMultipliedByLoopIndex)
grid[newRockPoint] = "#"
}
prev = curr
}
}
// get rock boudnaries
xMin := 500
xMax := 500
yMin := 0
yMax := 0
for point := range grid {
if point.x < xMin {
xMin = point.x
} else if point.x > xMax {
xMax = point.x
}
if point.y < yMin {
yMin = point.y
} else if point.y > yMax {
yMax = point.y
}
}
bounds := xyMinMax{xMin, xMax, yMin, yMax}
if addFloor {
yFloor := yMax + 2
for x := xMin - 5000; x <= xMax+5000; x++ {
grid[Point{x: x, y: yFloor}] = "#"
}
}
sandStart := Point{x: 500, y: 0}
grid[sandStart] = "+"
return &SandProblem{grid, bounds, sandStart}
}
func simulateSand(problem *SandProblem, part string) int {
var DIRECTIONS_TO_TRY []Point
DIRECTIONS_TO_TRY = append(DIRECTIONS_TO_TRY, Point{x: 0, y: 1})
DIRECTIONS_TO_TRY = append(DIRECTIONS_TO_TRY, Point{x: -1, y: 1})
DIRECTIONS_TO_TRY = append(DIRECTIONS_TO_TRY, Point{x: +1, y: 1})
numSandAtRest := 0
for {
// drop new piece of sand
currSandPos := problem.sandStart
// track sand movement
for {
var candidateSandPos Point
sandMoved := false
// find out if sand can move
for _, dirVector := range DIRECTIONS_TO_TRY {
candidateSandPos = currSandPos.add(dirVector)
result, exists := problem.grid[candidateSandPos]
if !exists {
sandMoved = true
break
}
if result == "#" || result == "o" {
continue
}
}
if sandMoved {
currSandPos = candidateSandPos
if part == "a" && currSandPos.y > problem.bounds.yMax {
return numSandAtRest
}
} else {
problem.grid[currSandPos] = "o"
numSandAtRest++
if part == "b" && currSandPos == problem.sandStart {
return numSandAtRest
}
break
}
}
}
}
func day14() {
var problem *SandProblem
var numSandAtRest int
// sample data
problem = parseRockData("2022/data/day14_sample.txt", false)
numSandAtRest = simulateSand(problem, "a")
if numSandAtRest != 24 {
panic("Part 1 example is failing")
}
problem = parseRockData("2022/data/day14_sample.txt", true)
numSandAtRest = simulateSand(problem, "b")
if numSandAtRest != 93 {
panic("Part 1 example is failing")
}
// real data
problem = parseRockData("2022/data/day14_input.txt", false)
numSandAtRest = simulateSand(problem, "a")
fmt.Println("Part 1:", numSandAtRest)
problem = parseRockData("2022/data/day14_input.txt", true)
numSandAtRest = simulateSand(problem, "b")
fmt.Println("Part 2:", numSandAtRest)
}