aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day08.cpp
blob: 87e318de873d8320e2665c2c7d5d21352bfa3e93 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
using namespace std;

using pos_type = tuple<size_t, size_t>;

map<pos_type, char>
read_file(string_view filename)
{
	fstream             input{ filename };
	map<pos_type, char> data;

	size_t yPos = 0;
	for ( string line; getline(input, line); ) {
		for ( size_t xPos = 0; xPos != line.size(); ++xPos ) {
			data[{ xPos, yPos }] = line[xPos];
		}
		++yPos;
	}

	return data;
}

void
part1(const map<pos_type, char>& data)
{
	map<char, vector<pos_type>> unique_frequencies;
	for ( const auto& [position, frequency]: data ) {
		if ( frequency == '.' ) {
			continue;
		}
		unique_frequencies[frequency].emplace_back(position);
	}

	set<pos_type> antinodes;
	for ( const auto& [frequency, positions]: unique_frequencies ) {
		for ( size_t i = 0; i != positions.size(); ++i ) {
			for ( size_t j = i + 1; j != positions.size(); ++j ) {
				const auto [lhs_x, lhs_y] = positions[i];
				const auto [rhs_x, rhs_y] = positions[j];

				const auto delta_x = rhs_x - lhs_x;
				const auto delta_y = rhs_y - lhs_y;

				pos_type new_position1{ lhs_x - delta_x, lhs_y - delta_y };
				if ( data.contains(new_position1) ) {
					antinodes.emplace(new_position1);
				}

				pos_type new_position2{ rhs_x + delta_x, rhs_y + delta_y };
				if ( data.contains(new_position2) ) {
					antinodes.emplace(new_position2);
				}
			}
		}
	}
	cout << antinodes.size() << endl;
}

void
part2(const map<pos_type, char>& data)
{
	map<char, vector<pos_type>> unique_frequencies;
	for ( const auto& [position, frequency]: data ) {
		if ( frequency == '.' ) {
			continue;
		}
		unique_frequencies[frequency].emplace_back(position);
	}

	set<pos_type> antinodes;
	for ( const auto& [frequency, positions]: unique_frequencies ) {
		for ( size_t i = 0; i != positions.size(); ++i ) {
			for ( size_t j = i + 1; j != positions.size(); ++j ) {
				const auto [lhs_x, lhs_y] = positions[i];
				const auto [rhs_x, rhs_y] = positions[j];

				const auto delta_x = rhs_x - lhs_x;
				const auto delta_y = rhs_y - lhs_y;

				for ( size_t count = 0;; ++count ) {
					pos_type new_position{ lhs_x - count * delta_x, lhs_y - count * delta_y };
					if ( !data.contains(new_position) ) {
						break;
					}
					antinodes.emplace(new_position);
				}

				for ( size_t count = 0;; ++count ) {
					pos_type new_position{ rhs_x + count * delta_x, rhs_y + count * delta_y };
					if ( !data.contains(new_position) ) {
						break;
					}
					antinodes.emplace(new_position);
				}
			}
		}
	}
	cout << antinodes.size() << endl;
}

int
main()
{
	auto data = read_file("data/day08.txt");
	part1(data);
	part2(data);
}