-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.go
90 lines (85 loc) · 1.61 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
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
f, _ := os.Open("day25_input")
//f, _ = os.Open("day25_input_test")
defer f.Close()
s := bufio.NewScanner(f)
inputs := make([][]string, 0)
for s.Scan() {
row := make([]string, 0)
for _, v := range s.Text() {
if v == '.' {
row = append(row, "")
} else {
row = append(row, string(v))
}
}
inputs = append(inputs, row)
}
// Part1
res := 0
moved := true
for moved {
newInputs := make([][]string, 0)
res++
moved = false
for i := 0; i < len(inputs); i++ {
newInputs = append(newInputs, make([]string, len(inputs[0])))
for j := 0; j < len(inputs[0]); j++ {
if inputs[i][j] == ">" {
curJ := j + 1
if curJ == len(inputs[0]) {
curJ = 0
}
if inputs[i][curJ] == "" {
newInputs[i][curJ] = ">"
newInputs[i][j] = ""
moved = true
} else {
newInputs[i][j] = ">"
}
}
}
}
for i := 0; i < len(inputs); i++ {
for j := 0; j < len(inputs[0]); j++ {
if inputs[i][j] == "v" {
curI := i + 1
if curI == len(inputs) {
curI = 0
}
if inputs[curI][j] != "v" && newInputs[curI][j] != ">" {
newInputs[curI][j] = "v"
newInputs[i][j] = ""
moved = true
} else {
newInputs[i][j] = "v"
}
}
}
}
inputs = newInputs
}
fmt.Println(res)
// Part2
res = 0
fmt.Println(res)
}
func printInputs(inputs [][]string) {
for i := 0; i < len(inputs); i++ {
for j := 0; j < len(inputs[0]); j++ {
if inputs[i][j] == "" {
fmt.Print(".")
} else {
fmt.Print(inputs[i][j])
}
}
fmt.Print("\n")
}
fmt.Print("\n")
}