blob: 5f965bdc0fc0d46802eda42d21201321828ea449 (
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
|
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
namespace {
vector<vector<int>>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
vector<vector<int>> grid;
for ( string line; getline(file, line); ) {
stringstream strm{ line };
vector<int> row;
for ( int value{}; strm >> value; ) {
row.emplace_back(value);
}
grid.emplace_back(row);
}
return grid;
}
void
part1(const vector<vector<int>>& grid)
{
int checksum = 0;
for ( const auto& row: grid ) {
auto [min, max] = ranges::minmax_element(row);
checksum += *max - *min;
}
cout << "Part1: " << checksum << '\n';
}
void
part2(const vector<vector<int>>& grid)
{
int checksum = 0;
for ( auto row: grid ) {
ranges::sort(row, ranges::greater());
for ( auto it = row.begin(); it != row.end(); ++it ) {
for ( auto it2 = next(it); it2 != row.end(); ++it2 ) {
if ( *it % *it2 == 0 ) {
checksum += *it / *it2;
}
}
}
}
cout << "Part2: " << checksum << '\n';
}
} // namespace
int
main()
{
auto grid = read_file("data/day02.txt");
part1(grid);
part2(grid);
}
|