-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (98 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
// Package day03 - Solution for Advent of Code 2019/03
// Problem Link: http://adventofcode.com/2019/day/03
package day03
import (
_ "embed"
"fmt"
"math"
"strconv"
"strings"
)
//go:embed input.txt
var input string
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (int, int) {
wire1, wire2 := parse(input)
return solvePart1(wire1, wire2), solvePart2(wire1, wire2)
}
func solvePart1(wire1, wire2 wire) int {
shortestDistance := math.MaxInt
for pos := range wire1.visits {
if _, ok := wire2.visits[pos]; ok {
if distance := manhattanDistance(pos); shortestDistance > distance {
shortestDistance = distance
}
}
}
return shortestDistance
}
func solvePart2(wire1, wire2 wire) int {
nearestIntersection := math.MaxInt
for pos := range wire1.visits {
if _, ok := wire2.visits[pos]; ok {
if steps := wire1.visits[pos] + wire2.visits[pos]; steps < nearestIntersection {
nearestIntersection = steps
}
}
}
return nearestIntersection
}
func parse(input string) (wire, wire) {
var units1, units2 []position
wires := strings.Split(input, "\n")
for _, by := range strings.Split(wires[0], ",") {
units1 = append(units1, pullWire(by)...)
}
for _, by := range strings.Split(wires[1], ",") {
units2 = append(units2, pullWire(by)...)
}
return pulledWires(units1, units2)
}
type position [2]int
type wire struct {
current position
visits map[position]int
}
func (w *wire) walk(units []position) {
for step, unit := range units {
w.current = translate(w.current, unit)
w.visits[w.current] = step + 1
}
}
func pulledWires(units1, units2 []position) (wire, wire) {
wire1, wire2 := wire{visits: map[position]int{}}, wire{visits: map[position]int{}}
wire1.walk(units1)
wire2.walk(units2)
return wire1, wire2
}
func pullWire(by string) (units []position) {
tokens := strings.SplitN(by, "", 2)
if steps, err := strconv.Atoi(tokens[1]); err == nil {
repeatWith := func(vector position) []position {
for i := 0; i < steps; i++ {
units = append(units, vector)
}
return units
}
switch tokens[0] {
case "L":
units = repeatWith(position{-1, 0})
case "R":
units = repeatWith(position{1, 0})
case "U":
units = repeatWith(position{0, 1})
case "D":
units = repeatWith(position{0, -1})
}
}
return units
}
func manhattanDistance(p position) int {
return int(math.Abs(float64(0-p[0]))) + int(math.Abs(float64(0-p[1])))
}
func translate(current, by position) position {
return position{current[0] + by[0], current[1] + by[1]}
}