aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day03.cpp
blob: 417cef300ce6001d3bb31c2715540f8bd7664584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>

using namespace std;

namespace {

void
part1(int value)
{
	auto ring      = static_cast<int>(ceil((sqrt(value) - 1) / 2));
	auto side_len  = (2 * ring) + 1;
	auto max_value = side_len * side_len;

	array<int, 4> 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<tuple<int, int>, 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<int, 3> offsets = { -1, 0, 1 };

	map<tuple<int, int>, 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);
}