diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-03 17:41:43 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-03 17:41:43 +0800 |
commit | 86eb9400ed2793b42ced77e7ee68100a487ffd20 (patch) | |
tree | f7f6352e6fd24af9e7fb9cda4cd9f01fd32094cb /src | |
parent | e8daf4c16f0616868f6c719e52f054dc19692d3e (diff) | |
download | advent-of-code-86eb9400ed2793b42ced77e7ee68100a487ffd20.tar.gz advent-of-code-86eb9400ed2793b42ced77e7ee68100a487ffd20.zip |
2016 day1 move
Diffstat (limited to 'src')
-rw-r--r-- | src/2016/day1/aoc.h | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/src/2016/day1/aoc.h b/src/2016/day1/aoc.h index 95823fe..66e97d1 100644 --- a/src/2016/day1/aoc.h +++ b/src/2016/day1/aoc.h @@ -1,6 +1,64 @@ #pragma once #include "common.h" +#include <algorithm> +#include <math.h> +#include <vector> namespace aoc2016 { -} +struct instruction { + enum { + left = 0, + right, + } direction; + int distance; +}; + +struct position { + enum bearing { + north = 0, + east, + south, + west, + } b; + + int x; + int y; + + int blocks(position p) const noexcept { return abs(p.x - x) + abs(p.y - y); } + + position move(instruction i) const noexcept { + auto putx = [this](int d) { return x; }; + auto puty = [this](int d) { return y; }; + auto addx = [this](int d) { return x + d; }; + auto subx = [this](int d) { return x - d; }; + auto addy = [this](int d) { return y + d; }; + auto suby = [this](int d) { return y - d; }; + + struct { + bearing bs[2]; + std::function<int(int)> lr[4]; + } moves[] = { + {{west, east}, {subx, addx, puty, puty}}, + {{north, south}, {putx, putx, addy, suby}}, + {{east, west}, {addx, subx, puty, puty}}, + {{south, north}, {putx, putx, suby, addy}}, + }; + position next; + next.b = moves[int(b)].bs[int(i.direction)]; + next.x = moves[int(b)].lr[int(i.direction)](i.distance); + next.y = moves[int(b)].lr[int(i.direction) + 2](i.distance); + + return next; + } + + position move(const std::vector<instruction>& is) const noexcept { + position next; + std::for_each(is.begin(), is.end(), [&next, this](const instruction& i) { next = move(i); }); + return next; + } +}; + +std::vector<instruction> parse_day1(line_view); + +} // namespace aoc2016 |