-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.cpp
More file actions
150 lines (123 loc) · 3.57 KB
/
day15.cpp
File metadata and controls
150 lines (123 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "day15.h"
#include "helpers.h"
#include <cassert>
#include <fstream>
#include <list>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
namespace day15
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 15 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day15_example.txt" : "inputs/day15_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
long long hashString(std::string_view string)
{
long long result{};
for (const char c : string)
{
result += c;
result *= 17;
result %= 256;
}
return result;
}
struct Lens
{
int focalLength{};
std::string label{};
bool operator==(const Lens& other) const
{
return label == other.label;
}
};
struct Box
{
int index{};
std::list<Lens> lenses{};
long long score()
{
long long sum{};
int lensNr{};
for (auto& [focalLength, label] : lenses)
{
lensNr++;
sum += (index + 1LL) * lensNr * focalLength;
}
return sum;
}
void addLens(const Lens& l)
{
for (auto& lens : lenses)
{
if (lens.label == l.label)
{
lens.focalLength = l.focalLength;
return;
}
}
lenses.push_back(l);
}
void removeLensByLabel(std::string_view label)
{
lenses.remove_if([label](const Lens l) {return l.label == label; });
}
};
long long solvePart1(std::ifstream& file)
{
std::string input;
std::getline(file, input);
const auto instructions = splitStringBySeperator(input, ',');
long long sum{};
for (const auto& instruction : instructions)
{
sum += hashString(instruction);
}
return sum;
}
long long solvePart2(std::ifstream& file)
{
std::string input;
std::getline(file, input);
const auto instructions = splitStringBySeperator(input, ',');
std::vector<Box> boxes{};
for(int i = 0; i<256;i++)
{
boxes.push_back(Box{ i });
}
for (const auto& instruction : instructions)
{
std::string::size_type index;
if ((index = instruction.find('-')) != std::string::npos)
{
const std::string label{ instruction.substr(0, index) };
const auto hash{ hashString(label) };
boxes[static_cast<size_t>(hash)].removeLensByLabel(label);
}
else
{
index = instruction.find('=');
const std::string label{ instruction.substr(0, index) };
const auto hash{ hashString(label) };
const int focalLength = instruction[instruction.size() - 1] - '0';
boxes[static_cast<size_t>(hash)].addLens(Lens{ focalLength, label });
}
}
long long sum{};
for (auto box : boxes)
{
sum += box.score();
}
return sum;
}
}