-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.go
129 lines (113 loc) · 2.22 KB
/
day14.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
const TRIM = 300
const X_SAND_DROP = 500
func print_map(mapa [][]string) {
for j := 0; j < len(mapa[0]); j++ {
for i := TRIM; i < len(mapa); i++ {
char := mapa[i][j]
if char == "" {
char = "."
}
fmt.Print(char)
}
fmt.Println()
}
}
func draw_line(mapa [][]string, xa, ya, xb, yb string) [][]string {
xai, _ := strconv.Atoi(xa)
yai, _ := strconv.Atoi(ya)
xbi, _ := strconv.Atoi(xb)
ybi, _ := strconv.Atoi(yb)
if yai > ybi {
t := yai
yai = ybi
ybi = t
}
if xai > xbi {
t := xai
xai = xbi
xbi = t
}
for j := yai; j <= ybi; j++ {
for i := xai; i <= xbi; i++ {
mapa[i][j] = "#"
}
}
return mapa
}
func add_sand(mapa [][]string) bool {
x_size := len(mapa)
y_size := len(mapa[0])
x := X_SAND_DROP
if mapa[x][0] != "" {
return true //if it reaches the source
}
for y := 0; y < y_size-1; y++ {
if mapa[x][y+1] != "" {
if x-1 >= 0 && mapa[x-1][y+1] == "" {
x--
continue
}
if x+1 < x_size && mapa[x+1][y+1] == "" {
x++
continue
}
mapa[x][y] = "o"
return false
}
}
return true //if it goes off the map
}
func main() {
input, _ := os.ReadFile("./input/input14.txt")
// didn't feel like parsing twice 😅:
x_count := 700
y_count := 163
y_floor := 162
mapa1 := make([][]string, x_count)
for i := 0; i < x_count; i++ {
mapa1[i] = make([]string, y_count)
}
lines := strings.Split(string(input), "\r\n")
for _, line := range lines {
broken := strings.Split(line, " -> ")
for i := 0; i < len(broken)-1; i++ {
a := strings.Split(broken[i], ",")
b := strings.Split(broken[i+1], ",")
mapa1 = draw_line(mapa1, a[0], a[1], b[0], b[1])
}
}
mapa2 := make([][]string, x_count)
for i := 0; i < x_count; i++ {
mapa2[i] = make([]string, y_count)
copy(mapa2[i], mapa1[i])
}
part1 := 0
finished := false
for {
finished = add_sand(mapa1)
if finished {
break
}
part1++
}
draw_line(mapa2, strconv.Itoa(0), strconv.Itoa(y_floor), strconv.Itoa(x_count-1), strconv.Itoa(y_floor)) // 🤦
part2 := 0
finished = false
for {
finished = add_sand(mapa2)
if finished {
break
}
part2++
}
//print_map(mapa2)
fmt.Println("Part 1:", part1)
fmt.Println("Part 2:", part2)
}