-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
137 lines (120 loc) · 2.75 KB
/
main.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
package main
import (
"io"
"strings"
aoc "github.com/teivah/advent-of-code"
)
type cellType int
const (
empty cellType = iota
roundRock
cubeRock
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
board, rows, cols := parse(lines)
for row := 1; row < rows; row++ {
for col := 0; col < cols; col++ {
move(aoc.Position{Row: row, Col: col}, board, aoc.Up)
}
}
return getResult(board, rows)
}
func parse(lines []string) (map[aoc.Position]cellType, int, int) {
m := make(map[aoc.Position]cellType)
for row, line := range lines {
for col, c := range line {
pos := aoc.Position{Row: row, Col: col}
switch c {
case '.':
m[pos] = empty
case 'O':
m[pos] = roundRock
case '#':
m[pos] = cubeRock
default:
panic(c)
}
}
}
return m, len(lines), len(lines[0])
}
func move(pos aoc.Position, board map[aoc.Position]cellType, dir aoc.Direction) {
if board[pos] != roundRock {
return
}
cur := pos
for {
next := cur.Move(dir, 1)
if t, exists := board[next]; exists && t == empty {
cur = next
continue
}
board[pos], board[cur] = board[cur], board[pos]
return
}
}
func getResult(board map[aoc.Position]cellType, rows int) int {
res := 0
for pos, t := range board {
if t == roundRock {
res += rows - pos.Row
}
}
return res
}
func fs2(input io.Reader, count int) int {
lines := aoc.ReaderToStrings(input)
board, rows, cols := parse(lines)
existingPositions := make(map[string]int)
for i := 0; i < count; i++ {
// North
for row := 1; row < rows; row++ {
for col := 0; col < cols; col++ {
move(aoc.Position{Row: row, Col: col}, board, aoc.Up)
}
}
// West (left)
for col := 1; col < cols; col++ {
for row := 0; row < rows; row++ {
move(aoc.Position{Row: row, Col: col}, board, aoc.Left)
}
}
// South
for row := rows - 2; row >= 0; row-- {
for col := 0; col < cols; col++ {
move(aoc.Position{Row: row, Col: col}, board, aoc.Down)
}
}
// East (right)
for col := cols - 2; col >= 0; col-- {
for row := 0; row < rows; row++ {
move(aoc.Position{Row: row, Col: col}, board, aoc.Right)
}
}
h := hashPosition(board, rows, cols)
if v, exists := existingPositions[h]; exists {
delta := i - v
// Speed up the computation by jumping as close as possible to count.
i += (count - i) / delta * delta
} else {
existingPositions[h] = i
}
}
return getResult(board, rows)
}
func hashPosition(board map[aoc.Position]cellType, rows int, cols int) string {
sb := strings.Builder{}
sb.Grow(rows * cols)
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
switch board[aoc.Position{Row: row, Col: col}] {
case empty, cubeRock:
sb.WriteRune('0')
case roundRock:
sb.WriteRune('1')
}
}
}
return sb.String()
}