diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-06 23:15:56 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-06 23:15:56 +0800 |
commit | 5c2bc1f0e9c3999db079273dd38fe7cf88cd3fe4 (patch) | |
tree | a527bedc38b226f50aebee04c9e972509ccb58b8 /src | |
parent | 8525575cdb87fc0b265525537635d0aafaada76c (diff) | |
download | advent-of-code-5c2bc1f0e9c3999db079273dd38fe7cf88cd3fe4.tar.gz advent-of-code-5c2bc1f0e9c3999db079273dd38fe7cf88cd3fe4.zip |
match earliest
Diffstat (limited to 'src')
-rw-r--r-- | src/2019/day3/README.md | 27 | ||||
-rw-r--r-- | src/2019/day3/aoc.cpp | 26 | ||||
-rw-r--r-- | src/2019/day3/aoc.h | 2 |
3 files changed, 45 insertions, 10 deletions
diff --git a/src/2019/day3/README.md b/src/2019/day3/README.md index 1b94596..1b1c05d 100644 --- a/src/2019/day3/README.md +++ b/src/2019/day3/README.md @@ -39,4 +39,31 @@ R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51 U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135 What is the Manhattan distance from the central port to the closest intersection? +--- Part Two --- +It turns out that this circuit is very timing-sensitive; you actually need to minimize the signal delay. +To do this, calculate the number of steps each wire takes to reach each intersection; choose the intersection where the sum of both wires' steps is lowest. If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection. + +The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered. Again consider the example from above: + +........... +.+-----+... +.|.....|... +.|..+--X-+. +.|..|..|.|. +.|.-X--+.|. +.|..|....|. +.|.......|. +.o-------+. +........... +In the above example, the intersection closest to the central port is reached after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the second wire for a total of 20+20 = 40 steps. + +However, the top-right intersection is better: the first wire takes only 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30 steps. + +Here are the best steps for the extra examples from above: + +R75,D30,R83,U83,L12,D49,R71,U7,L72 +U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps +R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51 +U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps +What is the fewest combined steps the wires must take to reach an intersection? diff --git a/src/2019/day3/aoc.cpp b/src/2019/day3/aoc.cpp index a81a29f..967863a 100644 --- a/src/2019/day3/aoc.cpp +++ b/src/2019/day3/aoc.cpp @@ -27,7 +27,22 @@ void match(const std::vector<wire::line>& lhs, const std::vector<wire::line>& lv } } -int day3(line_view file) { +int match_closest(wire ws[]) { + day3point p1; + day3point p2; + ws[0].sort(); + ws[1].sort(); + match(ws[0].psh, ws[1].psv, &p1); + match(ws[1].psh, ws[0].psv, &p2); + day3point p = std::min(p1, p2); + return p.distance() ; +} + +int match_earliest(wire ws[]) { + return 0; +} + +std::pair<int, int> day3(line_view file) { wire ws[2]; day3point mp{0, 0}; int i{0}; @@ -36,14 +51,7 @@ int day3(line_view file) { ws[i++].parse(lv, &cp, &mp); return true; }); - ws[0].sort(); - ws[1].sort(); - day3point p1; - day3point p2; - match(ws[0].psh, ws[1].psv, &p1); - match(ws[1].psh, ws[0].psv, &p2); - day3point p = std::min(p1, p2); - return p.distance() ; + return {match_earliest(ws), match_closest(ws)}; } } // namespace aoc2019 diff --git a/src/2019/day3/aoc.h b/src/2019/day3/aoc.h index 48b6812..41be5ed 100644 --- a/src/2019/day3/aoc.h +++ b/src/2019/day3/aoc.h @@ -107,6 +107,6 @@ struct wire { } }; -int day3(line_view); +std::pair<int,int> day3(line_view); } // namespace aoc2019 |