Thanks for the video! Also : Everyone, please add this case in your test case if possible : ...mul(mul(45,67)... and ...mul(63,mul(12,56).... In both this cases, we should consider 45*67 and 12*56.
Love being able to see other peoples thought processes with these puzzles, personally I used a regex to solve it which ended up being a fairly clean solution.
I understand that regex is not everyone's cup of tea, but why not to use the full power of c++ ? std::cin >> s or getline(std::cin, s)? reallocate string is really inefficient. and you could have used strtol for parsing number I like how you added hashes to avoid checking for boundaries. It was clever :)
We can actually solve both parts in one line in Python using regex: Part 1: print(sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d+),(\d+)\)", open('input.txt').read()))) Part2: A bit complicated due to flag. print(sum(int(x) * int(y) for match in re.findall(r"mul\(\d+,\d+\)|do\(\)|don't\(\)", open("input.txt").read()) if (flag := (match == "do()") or (match != "don't()" and globals().get("flag", True))) and match.startswith("mul(") for x, y in [match[4:-1].split(",")])) # One-liner un-rolled: print( sum( int(x) * int(y) for match in re.findall( r"mul\(\d+,\d+\)|do\(\)|don't\(\)", open("input.txt").read() ) if ( flag := (match == "do()") or (match != "don't()" and globals().get("flag", True)) ) and match.startswith("mul(") for x, y in [match[4:-1].split(",")] ) )
Thank you as I don't code in python, great to see! I got very lazy and joined all lines of input within vim :%s/ //g and stripped the "not to evaluated" part with :s/don't().\{-}do()\|don't().\{-}$//g That way part 1 solution doesn't need to be touched. the vim regex translated to reg ex website equivalent for explanation: /don't\(\).*?do\(\)|don.t\(\).*?$/g
wow that adding hashes at end is a great idea!
Thanks for the video! Also :
Everyone, please add this case in your test case if possible : ...mul(mul(45,67)... and ...mul(63,mul(12,56)....
In both this cases, we should consider 45*67 and 12*56.
How are you doing Errichtho, Where had you been, long time no see, no videos, how is life going?
Love being able to see other peoples thought processes with these puzzles, personally I used a regex to solve it which ended up being a fairly clean solution.
Loving these!
I'm learning C++. For this problem i used regular expressions
regex pattern matching could maybe do it in 10 lines. i am not writing that lol
I get it in exactly 10 lines in python using regexes
I bet some crazy ppl get it in even less lines
Even I was thinking part 2 was going to be nested mul 😅
why not use regex ? but its really very nice to do it with out regex though nice to see this .
I understand that regex is not everyone's cup of tea, but why not to use the full power of c++ ?
std::cin >> s or getline(std::cin, s)? reallocate string is really inefficient. and you could have used strtol for parsing number
I like how you added hashes to avoid checking for boundaries. It was clever :)
Long time no see
Where's day 4!!!!
First
We can actually solve both parts in one line in Python using regex:
Part 1:
print(sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d+),(\d+)\)", open('input.txt').read())))
Part2: A bit complicated due to flag.
print(sum(int(x) * int(y) for match in re.findall(r"mul\(\d+,\d+\)|do\(\)|don't\(\)", open("input.txt").read()) if (flag := (match == "do()") or (match != "don't()" and globals().get("flag", True))) and match.startswith("mul(") for x, y in [match[4:-1].split(",")]))
# One-liner un-rolled:
print(
sum(
int(x) * int(y)
for match in re.findall(
r"mul\(\d+,\d+\)|do\(\)|don't\(\)", open("input.txt").read()
)
if (
flag := (match == "do()")
or (match != "don't()" and globals().get("flag", True))
)
and match.startswith("mul(")
for x, y in [match[4:-1].split(",")]
)
)
just no.
i am never going to understand how to use regex lol
Thank you as I don't code in python, great to see! I got very lazy and joined all lines of input within vim :%s/
//g and stripped the "not to evaluated" part with :s/don't().\{-}do()\|don't().\{-}$//g
That way part 1 solution doesn't need to be touched. the vim regex translated to reg ex website equivalent for explanation: /don't\(\).*?do\(\)|don.t\(\).*?$/g
just... don't use regex for parsing pls
@@MrAdhit regex is literally meant for parsing lol, as long as your input is a type 3 language in the chomsky hierarchy