From d2607f5c85c11b6c8665f3191a0c0755c3464d3d Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Wed, 4 Dec 2019 16:53:48 +0100 Subject: [PATCH] 2019. Day 3. Part 2 Signed-off-by: Ettore Dreucci --- 2019/day_03/day_03.go | 62 ++++++++++++++++++++++++++++++++---------- 2019/day_03/test_input | 2 -- 2 files changed, 47 insertions(+), 17 deletions(-) delete mode 100644 2019/day_03/test_input diff --git a/2019/day_03/day_03.go b/2019/day_03/day_03.go index 57a0eee..a8df1e2 100644 --- a/2019/day_03/day_03.go +++ b/2019/day_03/day_03.go @@ -34,9 +34,10 @@ func readValues(fileName string) [][]string { return values } -func wireTrace(cableMoves []string) mapset.Set { +func wireTrace(cableMoves []string) ([]point, mapset.Set) { currentPosition := point{0, 0} - cablePath := mapset.NewSet() + cablePath := make([]point, 0) + cableSet := mapset.NewSet() for _, step := range cableMoves { direction := step[0] distance, err := strconv.Atoi(string(step[1:])) @@ -56,33 +57,64 @@ func wireTrace(cableMoves []string) mapset.Set { default: 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 { return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y))) } -func main() { - cablesMoves := readValues("./input") - cable1Path := wireTrace(cablesMoves[0]) - cable2Path := wireTrace(cablesMoves[1]) - - intersectionPoint := cable1Path.Intersect(cable2Path) - if intersectionPoint.Cardinality() == 0 { - log.Fatalln("No intersection point") +func reachIntersect(cablePath []point, intersect point) int { + for i, pos := range cablePath { + if pos.x == intersect.x && pos.y == intersect.y { + return i + 1 + } } + return -1 +} + +func shortestDistance(intersectionPoints mapset.Set) int { origin := point{0, 0} bestDistance := 0 - for p := range intersectionPoint.Iterator().C { + for p := range intersectionPoints.Iterator().C { dist := origin.manhattanDistance(p.(point)) if bestDistance == 0 || dist < bestDistance { bestDistance = dist } } - - fmt.Printf("Part 1: %d\n", bestDistance) + return 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) } diff --git a/2019/day_03/test_input b/2019/day_03/test_input deleted file mode 100644 index f4f32ec..0000000 --- a/2019/day_03/test_input +++ /dev/null @@ -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 \ No newline at end of file