From d152641c23fb548065d1f535c440507f286d1377 Mon Sep 17 00:00:00 2001 From: Stedd Date: Mon, 2 Dec 2024 19:17:25 +0100 Subject: [PATCH] added safe report checks --- days/day02.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++-- input/day02short.txt | 5 ++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/days/day02.cpp b/days/day02.cpp index 0d2b692..129a28d 100644 --- a/days/day02.cpp +++ b/days/day02.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include @@ -58,7 +60,7 @@ Analyze the unusual data from the engineers. How many reports are safe? void day02::Calculate() { - + //std::ifstream input("input/day02short.txt"); std::ifstream input("input/day02.txt"); if (!input) @@ -69,4 +71,71 @@ void day02::Calculate() return; } -} \ No newline at end of file + //int num1, num2, num3, num4, num5, num6, num7, num8; + + int safeReports = 0; + bool increasing, decreasing, safe; + + //std::string line; + for (std::string line; std::getline(input, line, '\n');) + { + std::istringstream iss(line); + std::vector numbers(std::istream_iterator{iss}, std::istream_iterator()); + int previousNumber = numbers.at(0); + increasing = false; + decreasing = false; + safe = true; + for (int i = 1; i < numbers.size(); i++) + { + int currentNumber = numbers.at(i); + int diff = abs(currentNumber - previousNumber); + + if (i == 1) + { + if (currentNumber > previousNumber) + { + increasing = true; + } + if (currentNumber < previousNumber) + { + decreasing = true; + } + if (currentNumber == previousNumber) + { + safe=false; + break; + } + } else + { + if (currentNumber < previousNumber && increasing) + { + safe=false; + break; + } + if (currentNumber > previousNumber && decreasing) + { + safe=false; + break; + } + } + + + if (diff < 1 || diff > 3) + { + safe=false; + break; + } + + previousNumber = currentNumber; + } + if (safe) + { + safeReports++; + } + } + std::cout << safeReports << '\n'; + // while (std::getline(input, line, '\n') ) + // { + // // printf("%d\n", num2); + // } +} diff --git a/input/day02short.txt b/input/day02short.txt index 267ec41..5a89d0c 100644 --- a/input/day02short.txt +++ b/input/day02short.txt @@ -9,4 +9,7 @@ 64 67 68 71 74 71 74 81 29 32 32 33 36 39 40 72 74 74 75 74 -11 14 14 17 17 \ No newline at end of file +11 14 14 17 17 +7 6 4 2 1 +1 3 6 7 9 +8 6 4 4 1 \ No newline at end of file