aboutsummaryrefslogtreecommitdiff
path: root/2024/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-06 22:05:54 +0100
committerThomas Schmucker <ts@its1.de>2024-12-06 22:05:54 +0100
commit9af62d7c591b7893f47f111784224eb0d5c5e4ff (patch)
tree866db017c4a7fc393dd1aa035d5b0bfd53a0f59d /2024/src
parentb5e867fa1e9e542ed3a6d1699c6680a78ba3f781 (diff)
downloadadvent-of-code-9af62d7c591b7893f47f111784224eb0d5c5e4ff.tar.gz
advent-of-code-9af62d7c591b7893f47f111784224eb0d5c5e4ff.tar.bz2
advent-of-code-9af62d7c591b7893f47f111784224eb0d5c5e4ff.zip
aoc 2024, day6
Diffstat (limited to '2024/src')
-rw-r--r--2024/src/day06.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/2024/src/day06.cpp b/2024/src/day06.cpp
new file mode 100644
index 0000000..1198d54
--- /dev/null
+++ b/2024/src/day06.cpp
@@ -0,0 +1,139 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <set>
5#include <string>
6#include <tuple>
7using namespace std;
8
9using pos_type = tuple<size_t, size_t>;
10
11map<pos_type, char>
12read_file(string_view filename)
13{
14 fstream input{ filename };
15 map<pos_type, char> data;
16
17 size_t yPos = 0;
18 for ( string line; getline(input, line); ) {
19 for ( size_t xPos = 0; xPos != line.size(); ++xPos ) {
20 data[{ xPos, yPos }] = line[xPos];
21 }
22 ++yPos;
23 }
24
25 return data;
26}
27
28set<pos_type>
29distinct_positions(const map<pos_type, char>& data)
30{
31 static const array<pos_type, 4> deltas = {
32 make_tuple(0, -1),
33 make_tuple(1, 0),
34 make_tuple(0, 1),
35 make_tuple(-1, 0)
36 };
37
38 size_t direction = 0;
39
40 auto position = get<0>(*ranges::find_if(data, [](auto position) {
41 return get<1>(position) == '^';
42 }));
43
44 set<pos_type> positions;
45 while ( true ) {
46 positions.emplace(position);
47
48 auto next_position = make_tuple(get<0>(position) + get<0>(deltas.at(direction)),
49 get<1>(position) + get<1>(deltas.at(direction)));
50
51 if ( !data.contains(next_position) ) {
52 break;
53 }
54
55 if ( data.at(next_position) == '#' ) {
56 direction = (direction + 1) % deltas.size();
57 }
58 else {
59 position = next_position;
60 }
61 }
62
63 return positions;
64}
65
66void
67part1(const map<pos_type, char>& data)
68{
69 auto positions = distinct_positions(data);
70
71 cout << positions.size() << endl;
72}
73
74bool
75would_loop(const map<pos_type, char>& data)
76{
77 static const array<pos_type, 4> deltas = {
78 make_tuple(0, -1),
79 make_tuple(1, 0),
80 make_tuple(0, 1),
81 make_tuple(-1, 0)
82 };
83
84 size_t direction = 0;
85
86 auto position = get<0>(*ranges::find_if(data, [](auto position) {
87 return get<1>(position) == '^';
88 }));
89
90 set<tuple<pos_type, size_t>> positions;
91
92 while ( true ) {
93 positions.emplace(position, direction);
94
95 auto next_position = make_tuple(get<0>(position) + get<0>(deltas.at(direction)),
96 get<1>(position) + get<1>(deltas.at(direction)));
97
98 if ( !data.contains(next_position) ) {
99 return false;
100 }
101
102 if ( data.at(next_position) == '#' ) {
103 direction = (direction + 1) % deltas.size();
104 }
105 else {
106 position = next_position;
107 }
108
109 if ( positions.contains({ position, direction }) ) {
110 return true;
111 }
112 }
113}
114
115void
116part2(map<pos_type, char> data)
117{
118 const auto positions = distinct_positions(data);
119
120 long sum = 0;
121 for ( const auto& position: positions ) {
122 if ( data.at(position) == '.' ) {
123 data.at(position) = '#';
124 if ( would_loop(data) ) {
125 ++sum;
126 }
127 data.at(position) = '.';
128 }
129 }
130 cout << sum << endl;
131}
132
133int
134main()
135{
136 auto data = read_file("data/day06.txt");
137 part1(data);
138 part2(data);
139}