diff options
author | dzwdz | 2022-12-17 12:53:27 +0100 |
---|---|---|
committer | dzwdz | 2022-12-17 12:53:27 +0100 |
commit | 9ce37081a57b5eaa4ef6a94296a8a6037721e8ee (patch) | |
tree | 9591a020261305c7fef79df8ab703ef75c210387 | |
parent | 1472c454ec38938c8ce1436de6d59c43ce4069ec (diff) |
day 17 part 1
-rw-r--r-- | 22.17/main.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/22.17/main.cpp b/22.17/main.cpp new file mode 100644 index 0000000..6a5c0cb --- /dev/null +++ b/22.17/main.cpp @@ -0,0 +1,125 @@ +#include <cassert> +#include <iostream> +#include <vector> +using namespace std; + +uint8_t shapes[5][4] = { + { + 0, + 0, + 0, + 0b00011110 + }, + { + 0, + 0b00001000, + 0b00011100, + 0b00001000, + }, + { + 0, + 0b00000100, + 0b00000100, + 0b00011100, + }, + { + 0b00010000, + 0b00010000, + 0b00010000, + 0b00010000, + }, + { + 0, + 0, + 0b00011000, + 0b00011000, + }, +}; + +uint8_t +signedShift(uint8_t a, int b) { + if (0 < b) return a >> b; + else return a << -b; +} + +bool +collide(vector<uint8_t> const& map, int shapeID, int x, int y) +{ + auto &shape = shapes[shapeID]; + // y increasing upwards + for (int i = 0; i < 4; i++) { + uint8_t line = shape[3 - i]; + if (signedShift(signedShift(line, x) & 0x7F, -x) != line) // wall coll + return true; + line = signedShift(line, x); + if (!(y + i < map.size())) continue; + if (line & map[y + i]) return true; + } + return false; +} + +void +place(vector<uint8_t>& map, int shapeID, int x, int y) +{ + auto &shape = shapes[shapeID]; + for (int i = 0; i < 4; i++) { + uint8_t line = signedShift(shape[3 - i], x); + if (line == 0) break; + if (!(y + i < map.size())) map.push_back(0); + map[y + i] |= line; + } +} + +void +vis(vector<uint8_t> const& map) +{ + char l[9] = {0}; + for (int i = map.size() - 1; 0 <= i; i--) { + for (int j = 0; j < 8; j++) { + l[7 - j] = (map[i] & (1 << j)) ? '#' : '.'; + } + cout << l << endl; + } + cout << endl; +} + +int +getWind(string const& s, int step) +{ + char c = s[step % s.size()]; + if (c == '<') return -1; + if (c == '>') return 1; + cout << s << endl; + throw "bad input"; +} + +int +main() +{ + string input; + getline(cin, input); + + int step = 0; + vector<uint8_t> map; + map.push_back(0x7F); + for (int i = 0; i < 2022; i++) { + int shape = i % 5; + int x = 0; + int y = map.size() + 3; + for (;;) { + int wind; + wind = getWind(input, step++); + if (!collide(map, shape, x + wind, y)) { + x += wind; + } + // cout << x << "," << y << endl; + if (collide(map, shape, x, y - 1)) { + break; + } + y--; + } + place(map, shape, x, y); + // vis(map); + } + cout << map.size() - 1 << endl; +} |