mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 15:41:34 +01:00
Solve 2024 day 2 part 1
This commit is contained in:
parent
87c6c27c6d
commit
174fb9a1d6
3 changed files with 1057 additions and 0 deletions
7
2024/day-02/day-02.pro
Normal file
7
2024/day-02/day-02.pro
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
TEMPLATE = app
|
||||||
|
CONFIG += console c++17
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
CONFIG -= qt
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
main.cpp
|
||||||
1000
2024/day-02/input
Normal file
1000
2024/day-02/input
Normal file
File diff suppressed because it is too large
Load diff
50
2024/day-02/main.cpp
Normal file
50
2024/day-02/main.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<vector<int>> list;
|
||||||
|
|
||||||
|
{
|
||||||
|
ifstream inputFile("input");
|
||||||
|
string line;
|
||||||
|
for (size_t i = 0; getline(inputFile, line); i++) {
|
||||||
|
list.push_back({});
|
||||||
|
stringstream stream(line);
|
||||||
|
int value;
|
||||||
|
while (stream >> value) {
|
||||||
|
list[i].push_back(value);
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part one
|
||||||
|
int numberOfSafeReports = 0;
|
||||||
|
for (vector<int> report : list) {
|
||||||
|
bool safe = true;
|
||||||
|
bool increasing = report[0] < report[1];
|
||||||
|
int previousLevel = report[0];
|
||||||
|
for (size_t i = 1; i < report.size(); i++) {
|
||||||
|
int currentLevel = report[i];
|
||||||
|
if (increasing) {
|
||||||
|
if (previousLevel >= currentLevel || currentLevel - previousLevel > 3) {
|
||||||
|
safe = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (previousLevel <= currentLevel || previousLevel - currentLevel > 3) {
|
||||||
|
safe = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
previousLevel = currentLevel;
|
||||||
|
}
|
||||||
|
numberOfSafeReports += safe;
|
||||||
|
}
|
||||||
|
cout << "Number of safe reports: " << numberOfSafeReports << endl;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue