aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-18 19:12:58 +0100
committerThomas Schmucker <ts@its1.de>2023-12-18 19:12:58 +0100
commite67bd318726418f37d1541939ac3e97667995d8d (patch)
treed43c2a4198bbbacb1dbc6d25f7ebe4790df45545
parentd6a96063a7ff7910a88c52ae1e9a3ea444d1bbc4 (diff)
downloadadvent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.tar.gz
advent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.tar.bz2
advent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.zip
Lösungen für Tag 18
-rw-r--r--makefile3
-rw-r--r--src/day18.cpp115
2 files changed, 117 insertions, 1 deletions
diff --git a/makefile b/makefile
index 0ce0a6f..26c7f16 100644
--- a/makefile
+++ b/makefile
@@ -16,7 +16,8 @@ all: bin/day01 \
16 bin/day14 \ 16 bin/day14 \
17 bin/day15 \ 17 bin/day15 \
18 bin/day16 \ 18 bin/day16 \
19 bin/day17 19 bin/day17 \
20 bin/day18
20 21
21bin: 22bin:
22 mkdir $@ 23 mkdir $@
diff --git a/src/day18.cpp b/src/day18.cpp
new file mode 100644
index 0000000..3037739
--- /dev/null
+++ b/src/day18.cpp
@@ -0,0 +1,115 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <sstream>
5#include <string>
6#include <vector>
7using namespace std;
8
9using puzzle_t = vector<pair<long, long>>;
10
11puzzle_t
12read_file_part1(string_view filename, long& border_steps)
13{
14 static map<char, pair<int, int>> movement = {
15 { 'U', { 0, -1 } }, // up
16 { 'D', { 0, 1 } }, // down
17 { 'L', { -1, 0 } }, // left
18 { 'R', { 1, 0 } }, // right
19 };
20
21 border_steps = 0;
22
23 fstream input{ filename };
24 puzzle_t data;
25
26 data.emplace_back(0, 0);
27
28 for ( string line; getline(input, line); ) {
29 char dir{};
30 long steps{};
31 stringstream sstrm{ line };
32
33 if ( sstrm >> dir >> steps ) {
34 data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps,
35 get<1>(data.back()) + get<1>(movement[dir]) * steps);
36
37 border_steps += steps;
38 }
39 }
40
41 return data;
42}
43
44puzzle_t
45read_file_part2(string_view filename, long& border_steps)
46{
47 static map<char, pair<int, int>> movement = {
48 { '0', { 1, 0 } }, // right
49 { '1', { 0, 1 } }, // down
50 { '2', { -1, 0 } }, // left
51 { '3', { 0, -1 } }, // up
52 };
53
54 border_steps = 0;
55
56 fstream input{ filename };
57 puzzle_t data;
58
59 data.emplace_back(0, 0);
60
61 for ( string line; getline(input, line); ) {
62 char dir{};
63 long steps{};
64 string hexcode;
65 stringstream sstrm{ line };
66
67 if ( sstrm >> dir >> steps >> hexcode ) {
68 steps = stol(hexcode.substr(2, 5), nullptr, 16);
69 dir = hexcode[7];
70
71 data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps,
72 get<1>(data.back()) + get<1>(movement[dir]) * steps);
73
74 border_steps += steps;
75 }
76 }
77
78 return data;
79}
80
81long
82shoelace(const puzzle_t& puzzle, const long border_steps)
83{
84 long area = 0;
85 for ( size_t i = 1; i < puzzle.size(); ++i ) {
86 const auto [x1, y1] = puzzle[i - 1];
87 const auto [x2, y2] = puzzle[i];
88
89 area += (x1 * y2 - y1 * x2);
90 }
91 return (border_steps + area) / 2 + 1;
92}
93
94void
95part1()
96{
97 long border_steps = 0;
98 auto puzzle = read_file_part1("data/day18.txt", border_steps);
99 cout << shoelace(puzzle, border_steps) << endl;
100}
101
102void
103part2()
104{
105 long border_steps = 0;
106 auto puzzle = read_file_part2("data/day18.txt", border_steps);
107 cout << shoelace(puzzle, border_steps) << endl;
108}
109
110int
111main()
112{
113 part1();
114 part2();
115}