-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (174 loc) · 4.98 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func main() {
positions, err := readElves("input.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(partOne(positions))
positions, err = readElves("input.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(partTwo(positions))
}
func partOne(positions map[[2]int]bool) int {
for i := 0; i < 10; i++ {
proposedPositions := map[[2]int][2]int{}
proposedPositionCount := map[[2]int]int{}
for position := range positions {
if notSurrounded(position, positions) {
proposedPositions[position] = position
proposedPositionCount[position]++
continue
}
newPositionFound := false
for j := 0; j < 4; j++ {
if canMove(position, positions, (i+j)%4) {
newPosition := findNewPosition(position, (i+j)%4)
proposedPositions[position] = newPosition
proposedPositionCount[newPosition]++
newPositionFound = true
break
}
}
if !newPositionFound {
proposedPositions[position] = position
proposedPositionCount[position]++
}
}
newPositions := map[[2]int]bool{}
for position := range positions {
proposedPosition := proposedPositions[position]
if proposedPositionCount[proposedPosition] > 1 {
newPositions[position] = true
} else {
newPositions[proposedPosition] = true
}
}
positions = newPositions
}
var minRow, maxRow, minCol, maxCol int
for position := range positions {
minRow, maxRow, minCol, maxCol = position[0], position[0], position[1], position[1]
break
}
for position := range positions {
if position[0] < minRow {
minRow = position[0]
} else if position[0] > maxRow {
maxRow = position[0]
}
if position[1] < minCol {
minCol = position[1]
} else if position[1] > maxCol {
maxCol = position[1]
}
}
return (maxRow - minRow + 1) * (maxCol - minCol + 1) - len(positions)
}
func findNewPosition(position [2]int, direction int) [2]int {
if direction == 0 {
return [2]int{position[0] + 1, position[1]}
} else if direction == 1 {
return [2]int{position[0] - 1, position[1]}
} else if direction == 2 {
return [2]int{position[0], position[1] - 1}
} else {
return [2]int{position[0], position[1] + 1}
}
}
func canMove(position [2]int, positions map[[2]int]bool, direction int) bool {
row, col := position[0], position[1]
if direction == 0 {
return !positions[[2]int{row + 1, col - 1}] && !positions[[2]int{row + 1, col}] && !positions[[2]int{row + 1, col + 1}]
} else if direction == 1 {
return !positions[[2]int{row - 1, col - 1}] && !positions[[2]int{row - 1, col}] && !positions[[2]int{row - 1, col + 1}]
} else if direction == 2 {
return !positions[[2]int{row - 1, col - 1}] && !positions[[2]int{row, col - 1}] && !positions[[2]int{row + 1, col - 1}]
} else {
return !positions[[2]int{row - 1, col + 1}] && !positions[[2]int{row, col + 1}] && !positions[[2]int{row + 1, col + 1}]
}
}
func notSurrounded(position [2]int, positions map[[2]int]bool) bool {
row, col := position[0], position[1]
return !positions[[2]int{row - 1, col - 1}] && !positions[[2]int{row - 1, col}] &&
!positions[[2]int{row - 1, col + 1}] && !positions[[2]int{row, col - 1}] &&
!positions[[2]int{row, col + 1}] && !positions[[2]int{row + 1, col - 1}] &&
!positions[[2]int{row + 1, col}] && !positions[[2]int{row + 1, col + 1}]
}
func partTwo(positions map[[2]int]bool) int {
for i := 0; i >= 0; i++ {
proposedPositions := map[[2]int][2]int{}
proposedPositionCount := map[[2]int]int{}
for position := range positions {
if notSurrounded(position, positions) {
proposedPositions[position] = position
proposedPositionCount[position]++
continue
}
newPositionFound := false
for j := 0; j < 4; j++ {
if canMove(position, positions, (i+j)%4) {
newPosition := findNewPosition(position, (i+j)%4)
proposedPositions[position] = newPosition
proposedPositionCount[newPosition]++
newPositionFound = true
break
}
}
if !newPositionFound {
proposedPositions[position] = position
proposedPositionCount[position]++
}
}
newPositions := map[[2]int]bool{}
moved := false
for position := range positions {
proposedPosition := proposedPositions[position]
if proposedPositionCount[proposedPosition] > 1 {
newPositions[position] = true
} else {
newPositions[proposedPosition] = true
if proposedPosition != position {
moved = true
}
}
}
if !moved {
return i + 1
}
positions = newPositions
}
return -1
}
func readElves(fileName string) (map[[2]int]bool, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
elves := [][]string{}
for scanner.Scan() {
elves = append(elves, strings.Split(scanner.Text(), ""))
}
if err := scanner.Err(); err != nil {
return nil, err
}
positions := map[[2]int]bool{}
for rowIndex, row := range elves {
for columnIndex, position := range row {
if position == "#" {
positions[[2]int{-rowIndex, columnIndex}] = true
}
}
}
return positions, nil
}