aboutsummaryrefslogtreecommitdiff
path: root/src/day15.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/day15.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/day15.cpp')
-rw-r--r--src/day15.cpp102
1 files changed, 0 insertions, 102 deletions
diff --git a/src/day15.cpp b/src/day15.cpp
deleted file mode 100644
index 6fcbfc2..0000000
--- a/src/day15.cpp
+++ /dev/null
@@ -1,102 +0,0 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <list>
5#include <numeric>
6#include <sstream>
7#include <string>
8#include <vector>
9using namespace std;
10
11string
12read_file(string_view filename)
13{
14 fstream input{ filename };
15 return { istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} };
16}
17
18void
19rtrim(string& str)
20{
21 str.erase(find_if(str.rbegin(), str.rend(), [](auto chr) { return !isspace(chr); }).base(), str.end());
22}
23
24vector<string>
25split(const string& line, char sep)
26{
27 vector<string> parts{};
28 stringstream input{ line };
29
30 for ( string part; getline(input, part, sep); ) {
31 rtrim(part);
32 parts.emplace_back(part);
33 }
34
35 return parts;
36}
37
38unsigned int
39calculate_hash(string_view str)
40{
41 unsigned int value = 0;
42 for ( auto chr: str ) {
43 value += static_cast<unsigned char>(chr);
44 value *= 17;
45 }
46 return value % 256;
47}
48
49void
50part1()
51{
52 const auto parts = split(read_file("data/day15.txt"), ',');
53 cout << accumulate(parts.begin(), parts.end(), 0UL, [](auto init, const auto& str) { return init + calculate_hash(str); }) << endl;
54}
55
56void
57part2()
58{
59 vector<list<tuple<string, long>>> boxes(256);
60
61 const auto line = read_file("data/day15.txt");
62 const auto parts = split(line, ',');
63 for ( const auto& part: parts ) {
64 const auto pos = part.find_first_of("=-");
65 const auto lens = part.substr(0, pos);
66 auto& box = boxes[calculate_hash(lens)];
67
68 if ( part[pos] == '=' ) {
69 const auto value = stol(part.substr(pos + 1));
70
71 auto iter = find_if(box.begin(), box.end(), [&lens](const auto& element) { return get<0>(element) == lens; });
72 if ( iter == box.end() ) {
73 box.emplace_back(lens, value);
74 }
75 else {
76 get<1>(*iter) = value;
77 }
78 }
79 else {
80 box.remove_if([&lens](const auto& element) { return get<0>(element) == lens; });
81 }
82 }
83
84 long value = 0;
85 for ( size_t idx = 0; idx != boxes.size(); ++idx ) {
86 const auto& box = boxes[idx];
87
88 long lens_number = 1;
89 for ( const auto& lens: box ) {
90 value += static_cast<long>(idx + 1) * lens_number * get<1>(lens);
91 ++lens_number;
92 }
93 }
94 cout << value << endl;
95}
96
97int
98main()
99{
100 part1();
101 part2();
102}