diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-18 16:34:57 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-18 16:34:57 +0800 |
commit | 6266e308faf85d4087637b396f164883a5ec6c34 (patch) | |
tree | 55b0346ddddbf06387ab25da48d07ad5555e322a /src | |
parent | 056c57d1808e9f11eda435729deda8b6d279ce91 (diff) | |
download | advent-of-code-6266e308faf85d4087637b396f164883a5ec6c34.tar.gz advent-of-code-6266e308faf85d4087637b396f164883a5ec6c34.zip |
day9 parse
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day9/README.md | 25 | ||||
-rw-r--r-- | src/2015/day9/aoc.cpp | 14 | ||||
-rw-r--r-- | src/2015/day9/aoc.h | 56 | ||||
-rw-r--r-- | src/2015/day9/input | 28 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 |
5 files changed, 124 insertions, 0 deletions
diff --git a/src/2015/day9/README.md b/src/2015/day9/README.md new file mode 100644 index 0000000..184ff48 --- /dev/null +++ b/src/2015/day9/README.md @@ -0,0 +1,25 @@ +--- Day 9: All in a Single Night --- + +Every year, Santa manages to deliver all of his presents in a single night. + +This year, however, he has some new locations to visit; his elves have provided him the distances between every pair of locations. He can start and end at any two (different) locations he wants, but he must visit each location exactly once. What is the shortest distance he can travel to achieve this? + +For example, given the following distances: + +London to Dublin = 464 +London to Belfast = 518 +Dublin to Belfast = 141 + +The possible routes are therefore: + +Dublin -> London -> Belfast = 982 +London -> Dublin -> Belfast = 605 +London -> Belfast -> Dublin = 659 +Dublin -> Belfast -> London = 659 +Belfast -> Dublin -> London = 605 +Belfast -> London -> Dublin = 982 + +The shortest of these is London -> Dublin -> Belfast = 605, and so the answer is 605 in this example. + +What is the distance of the shortest route? + diff --git a/src/2015/day9/aoc.cpp b/src/2015/day9/aoc.cpp new file mode 100644 index 0000000..8a5f8fd --- /dev/null +++ b/src/2015/day9/aoc.cpp @@ -0,0 +1,14 @@ +#include "aoc.h" + +namespace aoc2015 { +int day9(line_view file) { + world_day9 world; + per_line(file, [&world](line_view lv) { + world.parse(lv); + return true; + }); + world.check(); + return 0; +} + +} // namespace aoc2015 diff --git a/src/2015/day9/aoc.h b/src/2015/day9/aoc.h new file mode 100644 index 0000000..2a46bc7 --- /dev/null +++ b/src/2015/day9/aoc.h @@ -0,0 +1,56 @@ +#pragma once +#include "common.h" +#include <map> +#include <vector> + +namespace aoc2015 { + +struct location { + line_view name; + std::map<location*, int> routes; +}; + +struct world_day9 { + std::vector<location*> locations; + + location* find(line_view l) { + for (auto city : locations) { + if (city->name == l) { + return city; + } + } + locations.emplace_back(new location{l, {}}); + return locations.back(); + } + + int distance(line_view l) const noexcept { + int d{0}; + const char* p = l.line; + while (p < l.line + l.length) { + d = d * 10 + *p++ - '0'; + } + return d; + } + + void parse(line_view r) { + const char* p1 = r.contains("to"); + const char* p2 = r.contains("="); + location* l1 = find({r.line, p1 - 1}); + location* l2 = find({p1 + 3, p2 - 1}); + int d = distance({p2 + 2, r.line + r.length - 1}); + l1->routes.insert({l2, d}); + l2->routes.insert({l1, d}); + } + + void check() const noexcept { + for (auto city : locations) { + for (auto& kv : city->routes) { + std::cout << city->name << " -> " << kv.first->name << ": " << kv.second << std::endl; + } + } + } +}; + +int day9(line_view); + +} // namespace aoc2015 diff --git a/src/2015/day9/input b/src/2015/day9/input new file mode 100644 index 0000000..97a6b63 --- /dev/null +++ b/src/2015/day9/input @@ -0,0 +1,28 @@ +Faerun to Tristram = 65 +Faerun to Tambi = 129 +Faerun to Norrath = 144 +Faerun to Snowdin = 71 +Faerun to Straylight = 137 +Faerun to AlphaCentauri = 3 +Faerun to Arbre = 149 +Tristram to Tambi = 63 +Tristram to Norrath = 4 +Tristram to Snowdin = 105 +Tristram to Straylight = 125 +Tristram to AlphaCentauri = 55 +Tristram to Arbre = 14 +Tambi to Norrath = 68 +Tambi to Snowdin = 52 +Tambi to Straylight = 65 +Tambi to AlphaCentauri = 22 +Tambi to Arbre = 143 +Norrath to Snowdin = 8 +Norrath to Straylight = 23 +Norrath to AlphaCentauri = 136 +Norrath to Arbre = 115 +Snowdin to Straylight = 101 +Snowdin to AlphaCentauri = 84 +Snowdin to Arbre = 96 +Straylight to AlphaCentauri = 107 +Straylight to Arbre = 14 +AlphaCentauri to Arbre = 46 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7cc0a82..6fbe717 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOLUTION_FILES "2015/day6/aoc.cpp" "2015/day7/aoc.cpp" "2015/day8/aoc.cpp" + "2015/day9/aoc.cpp" ) add_library(solution SHARED ${SOLUTION_FILES}) |