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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
namespace {
struct Record {
int guard_id;
long start_sleep;
long stop_sleep;
};
long
to_timestamp(const string& line)
{
long year = stol(line.substr(1, 4));
long mon = stol(line.substr(6, 2));
long day = stol(line.substr(9, 2));
long hour = stol(line.substr(12, 2));
long min = stol(line.substr(15, 2));
auto timestamp = (year * 100'000'000) + (mon * 1'000'000) + (day * 10'000) + (hour * 100) + min; // NOLINT
return timestamp;
}
vector<Record>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
vector<string> lines;
for ( string line; getline(file, line); ) {
lines.emplace_back(line);
}
ranges::sort(lines);
vector<Record> records;
int guard = -1;
long start_sleep = -1;
long stop_sleep = -1;
for ( const auto& line: lines ) {
if ( line.find(" Guard #") != string::npos ) {
guard = stoi(line.substr(26));
}
else if ( line.find(" falls asleep") != string::npos ) {
start_sleep = to_timestamp(line);
}
else if ( line.find(" wakes up") != string::npos ) {
stop_sleep = to_timestamp(line);
records.emplace_back(guard, start_sleep, stop_sleep);
}
}
return records;
}
void
part1(const vector<Record>& records)
{
map<int, long> sleep_amounts;
for ( const auto& record: records ) {
sleep_amounts[record.guard_id] += record.stop_sleep - record.start_sleep;
}
auto max_amount = ranges::max_element(sleep_amounts,
[](const auto& lhs, const auto& rhs) {
return lhs.second < rhs.second;
});
map<long, unsigned> sleep_counts;
for ( const auto& record: records ) {
if ( record.guard_id != max_amount->first ) {
continue;
}
for ( auto timestamp = record.start_sleep; timestamp != record.stop_sleep; ++timestamp ) {
sleep_counts[timestamp % 10'000]++;
}
}
auto max_count = ranges::max_element(sleep_counts,
[](const auto& lhs, const auto& rhs) {
return lhs.second < rhs.second;
});
cout << "Part 1: " << max_amount->first * (max_count->first % 100) << '\n';
}
void
part2(const vector<Record>& records)
{
map<int, map<long, unsigned>> sleep_counts;
for ( const auto& record: records ) {
for ( auto timestamp = record.start_sleep; timestamp != record.stop_sleep; ++timestamp ) {
sleep_counts[record.guard_id][timestamp % 10'000]++;
}
}
int found_guard = -1;
long found_min = -1;
unsigned max_count = 0;
for ( const auto& [id, times]: sleep_counts ) {
for ( const auto& [min, count]: times ) {
if ( count > max_count ) {
max_count = count;
found_guard = id;
found_min = min % 100;
}
}
}
cout << "Part 2: " << found_guard * found_min << '\n';
}
} // namespace
int
main()
{
auto records = read_file("data/day04.txt");
part1(records);
part2(records);
}
|