aboutsummaryrefslogtreecommitdiff
path: root/2015/src
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
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')
-rw-r--r--2015/src/day05.cpp56
-rw-r--r--2015/src/day06.cpp114
-rw-r--r--2015/src/day07.cpp97
3 files changed, 267 insertions, 0 deletions
diff --git a/2015/src/day05.cpp b/2015/src/day05.cpp
new file mode 100644
index 0000000..b88ecf5
--- /dev/null
+++ b/2015/src/day05.cpp
@@ -0,0 +1,56 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <regex>
5#include <string>
6#include <vector>
7
8using namespace std;
9
10auto
11read_file(string_view filename)
12{
13 fstream input{ filename };
14 vector<string> lines;
15
16 for ( string line; getline(input, line); ) {
17 lines.emplace_back(line);
18 }
19
20 return lines;
21}
22
23void
24part1(const vector<string>& lines)
25{
26 const regex re1("(.*[aeiou]){3}");
27 const regex re2("(.)\\1");
28 const regex re3("(ab|cd|pq|xy)");
29
30 auto count = count_if(begin(lines), end(lines), [&](auto line) {
31 return regex_search(line, re1) && regex_search(line, re2) && !regex_search(line, re3);
32 });
33
34 cout << count << endl;
35}
36
37void
38part2(const vector<string>& lines)
39{
40 const regex re1("(..).*\\1");
41 const regex re2("(.).\\1");
42
43 auto count = count_if(begin(lines), end(lines), [&](auto line) {
44 return regex_search(line, re1) && regex_search(line, re2);
45 });
46
47 cout << count << endl;
48}
49
50int
51main()
52{
53 auto puzzle = read_file("data/day05.txt");
54 part1(puzzle);
55 part2(puzzle);
56}
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}
diff --git a/2015/src/day07.cpp b/2015/src/day07.cpp
new file mode 100644
index 0000000..f24d5af
--- /dev/null
+++ b/2015/src/day07.cpp
@@ -0,0 +1,97 @@
1#include <cinttypes>
2#include <cstdio>
3#include <fstream>
4#include <iostream>
5#include <map>
6#include <string>
7#include <vector>
8
9using namespace std;
10
11auto
12read_file(string_view filename)
13{
14 fstream input{ filename };
15 map<string, string> deps;
16
17 for ( string line; getline(input, line); ) {
18 const auto pos = line.find(" -> ");
19 if ( pos == string::npos ) {
20 continue;
21 }
22
23 deps[line.substr(pos + 4)] = line.substr(0, pos);
24 }
25
26 return deps;
27}
28
29// NOLINTBEGIN
30uint16_t
31solve(map<string, string>& deps, const string& dep)
32{
33 map<string, uint16_t> cache;
34
35 function<uint16_t(const string&)> solve = [&](const string& dep) -> uint16_t {
36 if (cache.contains(dep)) {
37 return cache[dep];
38 }
39
40 uint16_t value = 0;
41 if (sscanf(dep.c_str(), "%" SCNu16, &value) == 1) {
42 return cache[dep] = value;
43 }
44
45 const auto& line = deps[dep];
46
47 static const size_t MAX = 11;
48 char lhs[MAX], rhs[MAX];
49
50 if ( sscanf(line.c_str(), "%10s AND %10s", lhs, rhs) == 2 ) {
51 value = solve(lhs) & solve(rhs);
52 }
53 else if ( sscanf(line.c_str(), "%10s OR %10s", lhs, rhs) == 2 ) {
54 value = solve(lhs) | solve(rhs);
55 }
56 else if ( sscanf(line.c_str(), "%10s LSHIFT %10s", lhs, rhs) == 2 ) {
57 value = static_cast<uint16_t>(solve(lhs) << solve(rhs));
58 }
59 else if ( sscanf(line.c_str(), "%10s RSHIFT %10s", lhs, rhs) == 2 ) {
60 value = static_cast<uint16_t>(solve(lhs) >> solve(rhs));
61 }
62 else if ( sscanf(line.c_str(), "NOT %10s", lhs) == 1 ) {
63 value = ~solve(lhs);
64 }
65 else {
66 value = solve(line);
67 }
68
69 return cache[dep] = value;
70 };
71
72 return solve(dep);
73}
74// NOLINTEND
75
76auto
77part1(map<string, string>& deps)
78{
79 auto result = solve(deps, "a");
80 cout << result << endl;
81 return result;
82}
83
84void
85part2(map<string, string>& deps, uint16_t result)
86{
87 deps["b"] = to_string(result);
88 cout << solve(deps, "a") << endl;
89}
90
91int
92main()
93{
94 auto deps = read_file("data/day07.txt");
95 auto result = part1(deps);
96 part2(deps, result);
97}