From 9ce37081a57b5eaa4ef6a94296a8a6037721e8ee Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 17 Dec 2022 12:53:27 +0100 Subject: day 17 part 1 --- 22.17/main.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 22.17/main.cpp (limited to '22.17') 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 +#include +#include +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 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& 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 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 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; +} -- cgit 1.4.1-2-gfad0