-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.go
98 lines (77 loc) · 1.79 KB
/
day25.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
package main
import (
"os"
"panic"
"readers"
)
const (
cucumberRight = uint8('>')
cucumberDown = uint8('v')
empty = uint8('.')
)
type cucumberMove struct {
x, y, next int
}
func Day25Part1() int {
file, err := os.Open("assets/day25.txt")
panic.Check(err)
seafloor, err := readers.ReadStrings(file)
panic.Check(err)
steps := 0
yMax := len(seafloor)
xMax := len(seafloor[0])
for true {
steps++
didMove := false
moves := make([]cucumberMove, 0)
for y := 0; y < yMax; y++ {
for x := 0; x < xMax; x++ {
if seafloor[y][x] == cucumberRight {
next := (x + 1) % xMax
if seafloor[y][next] == empty {
moves = append(moves, cucumberMove{x, y, next})
didMove = true
}
}
}
}
for _, move := range moves {
seafloor[move.y] = replaceAtIndex(seafloor[move.y], empty, move.x)
seafloor[move.y] = replaceAtIndex(seafloor[move.y], cucumberRight, move.next)
}
moves = make([]cucumberMove, 0)
for x := 0; x < xMax; x++ {
for y := 0; y < yMax; y++ {
if seafloor[y][x] == cucumberDown {
next := (y + 1) % yMax
if seafloor[next][x] == empty {
moves = append(moves, cucumberMove{x, y, next})
didMove = true
}
}
}
}
for _, move := range moves {
seafloor[move.y] = replaceAtIndex(seafloor[move.y], empty, move.x)
seafloor[move.next] = replaceAtIndex(seafloor[move.next], cucumberDown, move.x)
}
if !didMove {
return steps
}
}
return -1
}
//goland:noinspection GoUnusedFunction
func printSeafloor(seafloor []string) string {
output := ""
for _, s := range seafloor {
output += s + "\n"
}
return output
}
func replaceAtIndex(str string, replacement uint8, index int) string {
return str[:index] + string(replacement) + str[index+1:]
}
func Day25Part2() string {
return "Marry X-Mas"
}