mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 11:46:39 +02:00
2019. Day 3. Part 2
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
@@ -34,9 +34,10 @@ func readValues(fileName string) [][]string {
|
|||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
func wireTrace(cableMoves []string) mapset.Set {
|
func wireTrace(cableMoves []string) ([]point, mapset.Set) {
|
||||||
currentPosition := point{0, 0}
|
currentPosition := point{0, 0}
|
||||||
cablePath := mapset.NewSet()
|
cablePath := make([]point, 0)
|
||||||
|
cableSet := mapset.NewSet()
|
||||||
for _, step := range cableMoves {
|
for _, step := range cableMoves {
|
||||||
direction := step[0]
|
direction := step[0]
|
||||||
distance, err := strconv.Atoi(string(step[1:]))
|
distance, err := strconv.Atoi(string(step[1:]))
|
||||||
@@ -56,33 +57,64 @@ func wireTrace(cableMoves []string) mapset.Set {
|
|||||||
default:
|
default:
|
||||||
log.Fatalf("Cannot decode step direction: %c\n", direction)
|
log.Fatalf("Cannot decode step direction: %c\n", direction)
|
||||||
}
|
}
|
||||||
cablePath.Add(currentPosition)
|
cablePath = append(cablePath, currentPosition)
|
||||||
|
cableSet.Add(currentPosition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cablePath
|
return cablePath, cableSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p1 point) manhattanDistance(p2 point) int {
|
func (p1 point) manhattanDistance(p2 point) int {
|
||||||
return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y)))
|
return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func reachIntersect(cablePath []point, intersect point) int {
|
||||||
cablesMoves := readValues("./input")
|
for i, pos := range cablePath {
|
||||||
cable1Path := wireTrace(cablesMoves[0])
|
if pos.x == intersect.x && pos.y == intersect.y {
|
||||||
cable2Path := wireTrace(cablesMoves[1])
|
return i + 1
|
||||||
|
}
|
||||||
intersectionPoint := cable1Path.Intersect(cable2Path)
|
|
||||||
if intersectionPoint.Cardinality() == 0 {
|
|
||||||
log.Fatalln("No intersection point")
|
|
||||||
}
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func shortestDistance(intersectionPoints mapset.Set) int {
|
||||||
origin := point{0, 0}
|
origin := point{0, 0}
|
||||||
bestDistance := 0
|
bestDistance := 0
|
||||||
for p := range intersectionPoint.Iterator().C {
|
for p := range intersectionPoints.Iterator().C {
|
||||||
dist := origin.manhattanDistance(p.(point))
|
dist := origin.manhattanDistance(p.(point))
|
||||||
if bestDistance == 0 || dist < bestDistance {
|
if bestDistance == 0 || dist < bestDistance {
|
||||||
bestDistance = dist
|
bestDistance = dist
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bestDistance
|
||||||
fmt.Printf("Part 1: %d\n", bestDistance)
|
}
|
||||||
|
|
||||||
|
func fewestSteps(cablesPath [][]point, intersectionPoints mapset.Set) int {
|
||||||
|
bestSteps := 0
|
||||||
|
for p := range intersectionPoints.Iterator().C {
|
||||||
|
steps := reachIntersect(cablesPath[0], p.(point)) + reachIntersect(cablesPath[1], p.(point))
|
||||||
|
if bestSteps == 0 || steps < bestSteps {
|
||||||
|
bestSteps = steps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestSteps
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cablesMoves := readValues("./input")
|
||||||
|
cable1Path, cable1Set := wireTrace(cablesMoves[0])
|
||||||
|
cable2Path, cable2Set := wireTrace(cablesMoves[1])
|
||||||
|
|
||||||
|
intersectionPoints := cable1Set.Intersect(cable2Set)
|
||||||
|
if intersectionPoints.Cardinality() == 0 {
|
||||||
|
log.Fatalln("No intersection point")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 1
|
||||||
|
dist := shortestDistance(intersectionPoints)
|
||||||
|
fmt.Printf("Part 1: %d\n", dist)
|
||||||
|
|
||||||
|
//Part 2
|
||||||
|
steps := fewestSteps([][]point{cable1Path, cable2Path}, intersectionPoints)
|
||||||
|
fmt.Printf("Part 2: %d\n", steps)
|
||||||
}
|
}
|
||||||
|
@@ -1,2 +0,0 @@
|
|||||||
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
|
|
||||||
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7
|
|
Reference in New Issue
Block a user