aboutsummaryrefslogtreecommitdiff
path: root/src/day18.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
committerThomas Schmucker <ts@its1.de>2024-01-03 23:35:54 +0100
commit56e890cec0a28c0a485212ccebfaf774235a79a2 (patch)
treed9c6241a1aa247e06ab5ba2f6f11967b77d458ec /src/day18.cpp
parente9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff)
downloadadvent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2
advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip
prepare for more puzzles ... :)
Diffstat (limited to 'src/day18.cpp')
-rw-r--r--src/day18.cpp115
1 files changed, 0 insertions, 115 deletions
diff --git a/src/day18.cpp b/src/day18.cpp
deleted file mode 100644
index 3037739..0000000
--- a/src/day18.cpp
+++ /dev/null
@@ -1,115 +0,0 @@
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}