aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day12.cpp
blob: d6f92772eef6fee848521897ff81f3f306281326 (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
#include <array>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <string>
#include <vector>

using namespace std;

namespace {

using Region = tuple<long, long, array<long, 6>>;

tuple<map<long, long>, vector<Region>>
read_file(const filesystem::path& filename)
{
	ifstream       file{ filename };
	vector<string> lines;

	for ( string line; getline(file, line); ) {
		lines.emplace_back(line);
	}

	map<long, long> areas;

	size_t idx = 0;
	for ( ;; idx += 5 ) {
		if ( lines.at(idx).at(1) != ':' ) {
			break;
		}

		const long num = lines.at(idx).at(0) - '0';

		long count = 0;
		for ( size_t j = idx + 1; j != idx + 4; ++j ) {
			count += ranges::count(lines.at(j), '#');
		}

		areas[num] = count;
	}

	vector<Region> regions;
	for ( ; idx != lines.size(); ++idx ) {
		static const regex rgx(R"(^(\d+)x(\d+):\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$)");

		smatch match;
		if ( !regex_match(lines.at(idx), match, rgx) ) {
			continue;
		}

		regions.emplace_back(
		    stol(match[1]), // width
		    stol(match[2]), // height
		    array<long, 6>{
		        stol(match[3]),
		        stol(match[4]),
		        stol(match[5]),
		        stol(match[6]),
		        stol(match[7]),
		        stol(match[8]) } // counts
		);
	}

	return { areas, regions };
}

void
part1(const map<long, long>& areas, const vector<Region>& regions)
{
	const auto result = ranges::count_if(regions, [&](const auto& region) {
		const auto& [width, height, counts] = region;

		long required = 0;
		for ( size_t i = 0; i != counts.size(); ++i ) {
			required += counts.at(i) * areas.at(static_cast<long>(i));
		}
		return required < width * height;
	});

	cout << "Part 1: " << result << '\n';
}

} // namespace

int
main()
{
	const auto [areas, regions] = read_file("data/day12.txt");
	part1(areas, regions);
}