From 756f22d58bb198b8f34589c112e1003614ccdcd6 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 2 Nov 2025 21:41:58 +0100 Subject: aoc 2017, days 1-20 --- 2017/src/day03.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 2017/src/day03.cpp (limited to '2017/src/day03.cpp') diff --git a/2017/src/day03.cpp b/2017/src/day03.cpp new file mode 100644 index 0000000..417cef3 --- /dev/null +++ b/2017/src/day03.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +using namespace std; + +namespace { + +void +part1(int value) +{ + auto ring = static_cast(ceil((sqrt(value) - 1) / 2)); + auto side_len = (2 * ring) + 1; + auto max_value = side_len * side_len; + + array centers = { + max_value - ring - (2 * ring * 0), + max_value - ring - (2 * ring * 1), + max_value - ring - (2 * ring * 2), + max_value - ring - (2 * ring * 3) + }; + + auto* itr = ranges::min_element(centers, {}, [value](int rhs) { return abs(value - rhs); }); + + auto dist_to_center = abs(value - *itr); + + cout << "Part1: " << ring + dist_to_center << '\n'; +} + +void +part2(int value) +{ + static const array, 4> dirs{ + make_tuple(1, 0), // right + make_tuple(0, -1), // up + make_tuple(-1, 0), // left + make_tuple(0, 1), // down + }; + + static const array offsets = { -1, 0, 1 }; + + map, int> grid; // (x,y) => value + + int x = 0; + int y = 0; + int steps = 1; + + grid[{ x, y }] = 1; + + while ( true ) { + for ( const auto [dx, dy]: dirs ) { + for ( int _ = 0; _ != steps; ++_ ) { + x += dx; + y += dy; + + int sum = 0; + for ( const auto off_x: offsets ) { + for ( const auto off_y: offsets ) { + if ( off_x == 0 && off_y == 0 ) { + continue; + } + + const auto pos = make_tuple(x + off_x, y + off_y); + if ( grid.contains(pos) ) { + sum += grid.at(pos); + } + } + } + + if ( sum > value ) { + cout << "Part2: " << sum << '\n'; + return; + } + + grid[{ x, y }] = sum; + } + if ( dx == 0 ) { + ++steps; + } + } + } +} + +} // namespace + +int +main() +{ + static const auto puzzle = 265149; + part1(puzzle); + part2(puzzle); +} -- cgit v1.3