diff options
author | Leah Neukirchen <leah@vuxu.org> | 2017-12-23 18:15:29 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2017-12-23 18:15:29 +0100 |
commit | 11269ed8c1be4af3b0bb8c355257481215b7dc9b (patch) | |
tree | 5e5d790a4bc4e0ab4241b3f284f47b9ec8db4041 | |
parent | b366ef386c4e8884225c7eec3f28a7fc1eee8167 (diff) | |
download | adventofcode2017-11269ed8c1be4af3b0bb8c355257481215b7dc9b.tar.gz adventofcode2017-11269ed8c1be4af3b0bb8c355257481215b7dc9b.tar.xz adventofcode2017-11269ed8c1be4af3b0bb8c355257481215b7dc9b.zip |
day22
-rw-r--r-- | day22 | 25 | ||||
-rw-r--r-- | day22.cc | 85 |
2 files changed, 110 insertions, 0 deletions
diff --git a/day22 b/day22 new file mode 100644 index 0000000..0ffb865 --- /dev/null +++ b/day22 @@ -0,0 +1,25 @@ +#.###...#..#..#...##.#### +##.##.#..##.#..#.#..##### +.####..###.#.#####.#.##.# +##..#.##.#.#.#...#..##..# +..#...####.#.###.###...#. +#..###.##.###.....#....#. +.#..#.##.##....##...####. +###.##....#...#.##....##. +..#.###..######.#.####... +.#.###..#.##.#..##.###### +###.####.#####.####....#. +#...####.#.##...##..#.#.. +##.######.#....##.#.####. +.#.#..#...##....#....#... +.####.##.#..##...#..####. +.#.####.##..###..###..##. +...#...####...#.#.#.###.# +#.##.####.#..##.###.####. +.#.#...####....##..####.# +##.###.##..####..#.###### +#.#...#.#.##.####........ +.......#..##..#.#..###... +.#..###.###........##.#.. +.######.......#.#.##.#.#. +.##..#.###.....##.#.#...# diff --git a/day22.cc b/day22.cc new file mode 100644 index 0000000..cfe8e59 --- /dev/null +++ b/day22.cc @@ -0,0 +1,85 @@ +#include <iostream> +#include <vector> +#include <string> + +using namespace std; + +void +rotate(int& dy, int &dx) +{ + if (dy) { + dx = (dy == -1) ? 1 : -1; + dy = 0; + } else { + dy = (dx == -1) ? -1 : 1; + dx = 0; + } +} + +int +main() { + const int pad = 400; + vector<vector<char>> d1; + + string p(pad, '.'); + for (string line; getline(cin, line); ) { + line = p + line + p; + d1.emplace_back(begin(line), end(line)); + } + vector<char> padding(2 * pad + size(d1), '.'); + d1.insert(begin(d1), pad, padding); + d1.insert(end(d1), pad, padding); + + vector d2 = d1; + + int p1 = 0, p2 = 0; + + for (int i=0, y=size(d1)/2, x=y, dy=-1, dx=0; + i < 10'000; + i++, y += dy, x += dx) { + if (d1[y][x] == '#') { // turn right; + rotate(dy, dx); + } else { // turn left; + rotate(dy, dx); + dy = -dy; + dx = -dx; + } + + if (d1[y][x] == '#') { + d1[y][x] = '.'; + } else { + p1++; + d1[y][x] = '#'; + } + } + + cout << p1 << endl; // 5462 + + for (int i=0, y=size(d2)/2, x=y, dy=-1, dx=0; + i < 10'000'000; + i++, y += dy, x += dx) { + if (d2[y][x] == '#') { // turn right; + rotate(dy, dx); + } else if (d2[y][x] == '.') { // turn left; + rotate(dy, dx); + dy = -dy; + dx = -dx; + } else if (d2[y][x] == 'F') { + dy = -dy; + dx = -dx; + } + + if (d2[y][x] == '#') { + d2[y][x] = 'F'; + } else if (d2[y][x] == '.') { + d2[y][x] = 'W'; + } else if (d2[y][x] == 'W') { + p2++; + d2[y][x] = '#'; + } else if (d2[y][x] == 'F') { + d2[y][x] = '.'; + } + } + + cout << p2 << endl; // 2512135 +} |