-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (133 loc) · 3.05 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
package main
import (
"fmt"
"strings"
"github.com/CZero/gofuncy/lfs"
)
type coord struct {
r int // Row
c int // Column
}
type wiresteps struct {
wire1steps int // Steps for wire 1 to get here
wire2steps int // Steps for wire 2 to get here
}
var grid = make(map[coord]wiresteps)
func main() {
wires, err := lfs.ReadLines("input.txt")
if err != nil {
panic(err)
}
buildgrid(wires)
}
func buildgrid(wires []string) {
for n, wire := range wires {
plotWire(wire, n+1)
}
// for pos, wiresteps := range grid {
// fmt.Printf("Coord: %v, Wiresteps: %v\n", pos, wiresteps)
// }
crossing, dist := findLowestCross()
fmt.Printf("Lowest distance: %d, found at: %v\n", dist, crossing)
}
func plotWire(wire string, n int) {
var (
stepcount int
pos coord
)
steps := strings.Split(wire, ",") // A range of steps, "," separated
for _, step := range steps {
repeats := lfs.SilentAtoi(step[1:])
switch string(step[0]) {
case "U": // r increases
for i := 0; i < repeats; i++ {
stepcount++
pos.r++
if n == 1 {
if grid[pos].wire1steps == 0 {
possteps := grid[pos]
possteps.wire1steps = stepcount
grid[pos] = possteps
}
} else {
if grid[pos].wire2steps == 0 {
possteps := grid[pos]
possteps.wire2steps = stepcount
grid[pos] = possteps
}
}
}
case "D": // r decreases
for i := 0; i < repeats; i++ {
stepcount++
pos.r--
if n == 1 {
if grid[pos].wire1steps == 0 {
possteps := grid[pos]
possteps.wire1steps = stepcount
grid[pos] = possteps
}
} else {
if grid[pos].wire2steps == 0 {
possteps := grid[pos]
possteps.wire2steps = stepcount
grid[pos] = possteps
}
}
}
case "R": // c increases
for i := 0; i < repeats; i++ {
stepcount++
pos.c++
if n == 1 {
if grid[pos].wire1steps == 0 {
possteps := grid[pos]
possteps.wire1steps = stepcount
grid[pos] = possteps
}
} else {
if grid[pos].wire2steps == 0 {
possteps := grid[pos]
possteps.wire2steps = stepcount
grid[pos] = possteps
}
}
}
case "L": // c decreases
for i := 0; i < repeats; i++ {
stepcount++
pos.c--
if n == 1 {
if grid[pos].wire1steps == 0 {
possteps := grid[pos]
possteps.wire1steps = stepcount
grid[pos] = possteps
}
} else {
if grid[pos].wire2steps == 0 {
possteps := grid[pos]
possteps.wire2steps = stepcount
grid[pos] = possteps
}
}
}
}
}
}
func findLowestCross() (coord, int) {
var lowest int // Lowest combined steps
var lowcoord coord // The crossing referenced
for pos, wiresteps := range grid {
if wiresteps.wire1steps > 0 && wiresteps.wire2steps > 0 {
fmt.Println(wiresteps.wire1steps + wiresteps.wire2steps)
if lowest != 0 && wiresteps.wire1steps+wiresteps.wire2steps < lowest {
lowest = wiresteps.wire1steps + wiresteps.wire2steps
lowcoord = pos
}
if lowest == 0 {
lowest = wiresteps.wire1steps + wiresteps.wire2steps
}
}
}
return lowcoord, lowest
}