2019. Day 3. Part 2

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2019-12-04 16:53:48 +01:00
parent bc8e2be648
commit d2607f5c85
2 changed files with 47 additions and 17 deletions

View File

@@ -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)
}

View File

@@ -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