-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (57 loc) · 1.27 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
package main
import (
"fmt"
"github.com/abates/AdventOfCode/2016/alg"
"github.com/abates/AdventOfCode/2016/util"
"strings"
)
func part1(grid *Grid) {
count := 0
grid.Iterate(func(l *Node) {
grid.Iterate(func(r *Node) {
if l != r && l.used > 0 && l.used <= r.avail {
count++
}
})
})
fmt.Printf("Number of pairs: %d\n", count)
}
func part2(grid *Grid) {
var endState *Grid
var p []alg.Node
alg.Traverse(grid, func(level int, path []alg.Node) bool {
p = path
position := path[len(path)-1]
if grid, ok := position.(*Grid); ok {
endState = grid
return grid.free.X == 37 && grid.free.Y == 0
}
return false
})
fmt.Printf("%v %v\n", len(p)-1, endState)
grid = NewGrid()
grid.grid[0] = endState.grid[0]
grid.grid[1] = endState.grid[1]
grid.free = endState.free
grid.data = endState.data
alg.Traverse(grid, func(level int, path []alg.Node) bool {
p = path
position := path[len(path)-1]
if grid, ok := position.(*Grid); ok {
endState = grid
return grid.data.X == 0 && grid.data.Y == 0
}
return false
})
fmt.Printf("%v %v\n", len(p)-1, endState)
}
func main() {
grid := NewGrid()
for _, line := range util.ReadInput() {
if strings.HasPrefix(line, "/dev") {
grid.AddNode(NewNodeFromString(line))
}
}
part1(grid)
part2(grid)
}