From 528c58bf205864e2797e2027fa9691c5f0ba1249 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 12 Jan 2025 10:29:51 +0100 Subject: aoc 2016, day 2 --- 2016/src/day02.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 2016/src/day02.cpp (limited to '2016/src/day02.cpp') diff --git a/2016/src/day02.cpp b/2016/src/day02.cpp new file mode 100644 index 0000000..ad625d5 --- /dev/null +++ b/2016/src/day02.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +vector +read_file(string_view filename) +{ + fstream input{ filename }; + vector lines; + + vector> result; + for ( string line; getline(input, line); ) { + lines.emplace_back(line); + } + return lines; +} + +void +part1(const vector& lines) +{ + int pos_x = 1; + int pos_y = 1; + + for ( const auto& line: lines ) { + for ( const auto chr: line ) { + switch ( chr ) { + case 'U': + --pos_y; + break; + case 'R': + ++pos_x; + break; + case 'L': + --pos_x; + break; + case 'D': + ++pos_y; + break; + default: + cerr << "unknown char: " << chr << endl; + return; + } + + pos_x = clamp(pos_x, 0, 2); + pos_y = clamp(pos_y, 0, 2); + } + cout << pos_y * 3 + pos_x + 1; + } + cout << endl; +} + +void +part2(const vector& lines) +{ + const map, char> keypad = { + { { 2, 0 }, '1' }, + { { 1, 1 }, '2' }, + { { 2, 1 }, '3' }, + { { 3, 1 }, '4' }, + { { 0, 2 }, '5' }, + { { 1, 2 }, '6' }, + { { 2, 2 }, '7' }, + { { 3, 2 }, '8' }, + { { 4, 2 }, '9' }, + { { 1, 3 }, 'A' }, + { { 2, 3 }, 'B' }, + { { 3, 3 }, 'C' }, + { { 2, 4 }, 'D' } + }; + + tuple pos = { 0, 2 }; + + for ( const auto& line: lines ) { + for ( const auto chr: line ) { + auto pos2 = pos; + switch ( chr ) { + case 'U': + --get<1>(pos2); + break; + case 'R': + ++get<0>(pos2); + break; + case 'L': + --get<0>(pos2); + break; + case 'D': + ++get<1>(pos2); + break; + default: + cerr << "unknown char: " << chr << endl; + return; + } + + if ( keypad.contains(pos2) ) { + pos = pos2; + } + } + cout << keypad.at(pos); + } + cout << endl; +} + +int +main() +{ + auto lines = read_file("data/day02.txt"); + part1(lines); + part2(lines); +} -- cgit v1.3