advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit db5039226bb1f1bcb85ca2732f1d028b08face0a
parent bf5f5aa91628f956c79865b9d83c2e33a5491739
Author: bsandro <email@bsandro.tech>
Date:   Tue,  3 Dec 2024 08:47:22 +0200

Day 03 p2

Diffstat:
Mday03/main.cpp | 12+++++++-----
Aday03/test2.txt | 1+
2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/day03/main.cpp b/day03/main.cpp @@ -22,21 +22,23 @@ std::string read_file(const std::string &path) { } int part1(std::string input) { - std::regex expr_regex("mul\\((\\d+),(\\d+)\\)"); - auto r_begin = std::sregex_iterator(input.begin(), input.end(), expr_regex); + std::regex expr("mul\\((\\d+),(\\d+)\\)"); + auto r_begin = std::sregex_iterator(input.begin(), input.end(), expr); auto r_end = std::sregex_iterator(); int sum = 0; for (auto i=r_begin; i!=r_end; ++i) { int a = std::atoi((*i)[1].str().c_str()); int b = std::atoi((*i)[2].str().c_str()); - //std::cout << (*i).str() << " = " << a << "+" << b << std::endl; sum += a*b; } return sum; } + int part2(std::string input) { - (void)input; - return 0; + std::regex expr("don't\\(\\).*?(do\\(\\)|$)"); + std::string noeol = std::regex_replace(input, std::regex("\n"), ""); + std::string cleaned = std::regex_replace(noeol, expr, ""); + return part1(cleaned); } int main(int argc, char **argv) { diff --git a/day03/test2.txt b/day03/test2.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))