second challange

This commit is contained in:
2025-12-09 21:45:55 +01:00
parent 26c1dae4f6
commit cf51ef5a36
5 changed files with 4627 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
main

4570
first_day/2/input.txt Normal file

File diff suppressed because it is too large Load Diff

56
first_day/2/main.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <cstdlib>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int translate_movement(const string &input) {
string symbol = input.substr(0, 1);
int ammount = stoi(input.substr(1));
return (symbol == "R") ? ammount : -ammount;
}
int normalize_number(int number) {
number %= 100;
if (number < 0)
number += 100;
return number;
}
int main() {
std::ifstream file("input.txt");
vector<string> data;
string s;
while (file >> s) {
data.push_back(s);
}
int count_zero = 0;
int pos = 50;
for (const string &movement : data) {
int delta = translate_movement(movement);
if (abs(delta) >= 100) {
int cycles = abs(delta) / 100;
delta = delta % 100;
count_zero += cycles;
}
int old_pos = pos;
pos = normalize_number(pos + delta);
if (delta > 0 && pos < old_pos) {
count_zero++;
}
if (delta < 0) {
if ((pos > old_pos && old_pos != 0) || pos == 0) {
count_zero++;
}
}
}
printf("Dial pointed at zero %d times.", count_zero);
}