-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (119 loc) · 2.53 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
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func parseItem(item string) (dir uint8, length int) {
dir = item[0]
length, err := strconv.Atoi(item[1:])
if err != nil {
panic(err)
}
return
}
type pos struct {
x, y int
}
func (p pos) equals(other pos) bool {
return p.x == other.x && p.y == other.y
}
func (p pos) dist(other pos) int {
xDist := p.x - other.x
if xDist < 0 {
xDist *= -1
}
yDist := p.y - other.y
if yDist < 0 {
yDist *= -1
}
return xDist + yDist
}
func parse(r io.Reader) [][]pos {
csvReader := csv.NewReader(r)
csvReader.FieldsPerRecord = -1
rows, err := csvReader.ReadAll()
if err != nil {
panic(err)
}
wires := make([][]pos, len(rows))
for wireNum, row := range rows {
wires[wireNum] = transform(row)
}
return wires
}
func transform(wireDesc []string) []pos {
wire := []pos{{1, 1}}
for _, item := range wireDesc {
dir, length := parseItem(item)
orig := wire[len(wire)-1]
newX, newY := orig.x, orig.y
xDir := 1
yDir := 1
switch dir {
case 'R':
newX += length
case 'L':
newX -= length
xDir = -1
case 'U':
newY += length
case 'D':
newY -= length
yDir = -1
}
if newX != orig.x {
for i := orig.x; i != newX; {
i += xDir
wire = append(wire, pos{i, orig.y})
}
} else if newY != orig.y {
for i := orig.y; i != newY; {
i += yDir
wire = append(wire, pos{orig.x, i})
}
} else {
panic("no x or y change")
}
}
return wire
}
func minIntersect(firstWire []pos, secondWire []pos) int {
var intersections []pos
for _, firstWirePos := range firstWire {
for _, secondWirePos := range secondWire {
if !firstWirePos.equals(pos{1, 1}) && firstWirePos.equals(secondWirePos) {
intersections = append(intersections, firstWirePos)
}
}
}
if len(intersections) == 0 {
panic("no intersections")
}
set := false
minDist := 0
for _, intersection := range intersections {
dist := intersection.dist(pos{1, 1})
if !set || dist < minDist {
minDist = dist
set = true
}
}
return minDist
}
func main() {
test1 := parse(strings.NewReader("R75,D30,R83,U83,L12,D49,R71,U7,L72\nU62,R66,U55,R34,D71,R55,D58,R83"))
fmt.Println(minIntersect(test1[0], test1[1]))
test2 := parse(strings.NewReader("R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51\nU98,R91,D20,R16,D67,R40,U7,R15,U6,R7"))
fmt.Println(minIntersect(test2[0], test2[1]))
input, err := os.Open(path.Join("2019", "3", "input.txt"))
if err != nil {
panic(err)
}
wires := parse(input)
fmt.Println(minIntersect(wires[0], wires[1]))
}