aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day06.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-11-13 22:20:30 +0100
committerThomas Schmucker <ts@its1.de>2024-11-13 22:20:30 +0100
commit4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc (patch)
treee9e22ef6696b5d5657af3fef94c69b0e6f9fb9bd /2015/src/day06.cpp
parent06a24f45a2cb811aaf6b522187d25042ca685d45 (diff)
downloadadvent-of-code-4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc.tar.gz
advent-of-code-4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc.tar.bz2
advent-of-code-4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc.zip
aoc 2015, days 5, 6 and 7
Diffstat (limited to '2015/src/day06.cpp')
-rw-r--r--2015/src/day06.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/2015/src/day06.cpp b/2015/src/day06.cpp
new file mode 100644
index 0000000..b7f15b5
--- /dev/null
+++ b/2015/src/day06.cpp
@@ -0,0 +1,114 @@
1#include <algorithm>
2#include <cstdio>
3#include <fstream>
4#include <iostream>
5#include <map>
6#include <numeric>
7#include <string>
8#include <tuple>
9#include <vector>
10
11using namespace std;
12
13enum Operation {
14 On,
15 Off,
16 Toggle
17};
18
19using instruction = tuple<Operation, int, int, int, int>;
20
21auto
22read_file(string_view filename)
23{
24 fstream input{ filename };
25 vector<instruction> instructions;
26
27 for ( string line; getline(input, line); ) {
28 int x0, y0, x1, y1; // NOLINT
29
30 if ( sscanf(line.c_str(), "turn on %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT
31 instructions.emplace_back(Operation::On, x0, y0, x1, y1);
32 }
33 else if ( sscanf(line.c_str(), "toggle %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT
34 instructions.emplace_back(Operation::Toggle, x0, y0, x1, y1);
35 }
36 else if ( sscanf(line.c_str(), "turn off %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT
37 instructions.emplace_back(Operation::Off, x0, y0, x1, y1);
38 }
39 }
40
41 return instructions;
42}
43
44// clang-format off
45static void turn_on(int& value) { value = 1; }
46static void turn_off(int& value) { value = 0; }
47static void toggle(int& value) { value ^= 1; }
48// clang-format on
49
50void
51part1(const vector<instruction>& instructions)
52{
53 map<Operation, function<void(int&)>> ops = {
54 { Operation::On, turn_on },
55 { Operation::Toggle, toggle },
56 { Operation::Off, turn_off }
57 };
58
59 map<tuple<int, int>, int> lights;
60
61 for ( const auto& [operation, x0, y0, x1, y1]: instructions ) {
62 for ( int x = x0; x <= x1; ++x ) {
63 for ( int y = y0; y <= y1; ++y ) {
64 ops[operation](lights[{ x, y }]);
65 }
66 }
67 }
68
69 auto sum = count_if(begin(lights), end(lights), [](const auto& light) {
70 return light.second == 1;
71 });
72
73 cout << sum << endl;
74}
75
76// clang-format off
77static void increase(int& value) { ++value; }
78static void decrease(int& value) { if (value > 0) { --value; } }
79static void increase_by_2(int& value) { value += 2; }
80// clang-format on
81
82void
83part2(const vector<instruction>& instructions)
84{
85 map<Operation, function<void(int&)>> ops = {
86 { Operation::On, increase },
87 { Operation::Toggle, increase_by_2 },
88 { Operation::Off, decrease }
89 };
90
91 map<tuple<int, int>, int> lights;
92
93 for ( const auto& [operation, x0, y0, x1, y1]: instructions ) {
94 for ( int x = x0; x <= x1; ++x ) {
95 for ( int y = y0; y <= y1; ++y ) {
96 ops[operation](lights[{ x, y }]);
97 }
98 }
99 }
100
101 auto sum = accumulate(begin(lights), end(lights), 0, [](int value, const auto& light) {
102 return value + light.second;
103 });
104
105 cout << sum << endl;
106}
107
108int
109main()
110{
111 auto instructions = read_file("data/day06.txt");
112 part1(instructions);
113 part2(instructions);
114}