do day 1
This commit is contained in:
parent
3f24130557
commit
471297e87a
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
a.out
|
||||
|
||||
63
main.cpp
Normal file
63
main.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
struct Data{ char dir; int64_t cnt; };
|
||||
|
||||
using std::cout, std::endl, std::vector, std::ifstream;
|
||||
|
||||
int64_t part_1(const vector<Data>&);
|
||||
int64_t part_2(const vector<Data>&);
|
||||
|
||||
int main() {
|
||||
ifstream file{"input/2025-1.txt"};
|
||||
Data line;
|
||||
vector<Data> v;
|
||||
while (file >> line.dir >> line.cnt) {
|
||||
v.push_back(line);
|
||||
}
|
||||
cout << "Part 1: " << part_1(v) << endl;
|
||||
cout << "Part 1: " << part_2(v) << endl;
|
||||
}
|
||||
|
||||
int64_t part_1(const vector<Data>& v) {
|
||||
int64_t loc{50}, presses{0};
|
||||
for (auto data : v) {
|
||||
if (data.dir == 'L') {
|
||||
loc -= data.cnt % 100;
|
||||
} else {
|
||||
loc += data.cnt % 100;
|
||||
}
|
||||
loc %= 100;
|
||||
if (loc < 0) {
|
||||
loc += 100;
|
||||
}
|
||||
if (loc == 0) {
|
||||
++presses;
|
||||
}
|
||||
}
|
||||
|
||||
return presses;
|
||||
}
|
||||
|
||||
int64_t part_2(const vector<Data>& v) {
|
||||
int64_t loc{50}, presses{0};
|
||||
for (auto data : v) {
|
||||
auto last_loc = loc;
|
||||
if (data.dir == 'L') {
|
||||
loc -= data.cnt % 100;
|
||||
} else {
|
||||
loc += data.cnt % 100;
|
||||
}
|
||||
presses += std::abs(data.cnt / 100);
|
||||
if ((loc != last_loc && loc >= 100) || (loc <= 0 && last_loc != 0)) {
|
||||
presses += 1;
|
||||
}
|
||||
loc %= 100;
|
||||
if (loc < 0) {
|
||||
loc += 100;
|
||||
}
|
||||
}
|
||||
|
||||
return presses;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user