diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-15 14:43:26 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-15 14:43:26 +0800 |
commit | ac57557d101f8b7c75e9350712daf16e9b956613 (patch) | |
tree | 78a1d4e8673237aca1b99f810c45952d72635c0c /src/2015/day3/aoc.cpp | |
parent | 1a768c1b8d69986c1cafcf5ead8a1f0424cefcbe (diff) | |
download | advent-of-code-ac57557d101f8b7c75e9350712daf16e9b956613.tar.gz advent-of-code-ac57557d101f8b7c75e9350712daf16e9b956613.zip |
day3
Diffstat (limited to 'src/2015/day3/aoc.cpp')
-rw-r--r-- | src/2015/day3/aoc.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/2015/day3/aoc.cpp b/src/2015/day3/aoc.cpp new file mode 100644 index 0000000..8dcf3c3 --- /dev/null +++ b/src/2015/day3/aoc.cpp @@ -0,0 +1,61 @@ +#include "aoc.h" +#include <map> + +namespace aoc2015 { + +house move_day3(house h, dir d) { + house n = h; + switch (d) { + case dir::up: + n.y += 1; + break; + case dir::down: + n.y -= 1; + break; + case dir::left: + n.x -= 1; + break; + case dir::right: + n.x += 1; + break; + } + return n; +} + +std::pair<size_t, size_t> day3(line_view lv) { + house h{0, 0}; + house h1{0, 0}; + house h2{0, 0}; + std::map<house, size_t> m1{{h, 1}}; + std::map<house, size_t> m2{{h, 1}}; + house* hs[] = {&h1, &h2}; + + for (size_t i = 0; i < lv.length; i++) { + int x = i % 2; + switch (lv.line[i]) { + case '^': + h = move_day3(h, dir::up); + *hs[x] = move_day3(*hs[x], dir::up); + break; + case 'v': + h = move_day3(h, dir::down); + *hs[x] = move_day3(*hs[x], dir::down); + break; + case '>': + h = move_day3(h, dir::right); + *hs[x] = move_day3(*hs[x], dir::right); + break; + case '<': + h = move_day3(h, dir::left); + *hs[x] = move_day3(*hs[x], dir::left); + break; + default: + break; + } + m1.insert({h, 1}); + m2.insert({*hs[x], 1}); + } + return {m1.size(), m2.size()}; +} + +} // namespace aoc2015 |