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(",")] ) )
For the second part you can reduce complexity somewhat (imo) by instead preprocessing to remove don’t\(\).*(do\(\)|$) then you can just run the same logic from part one.
wow that adding hashes at end is a great idea!
Yup, not worth it for short substrings as comparing 5 characters one by one might be faster than computing a hash in O(1).
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.
How are you doing Errichtho, Where had you been, long time no see, no videos, how is life going?
Loving these!
Where's day 4!!!!
I'm learning C++. For this problem i used regular expressions
Even I was thinking part 2 was going to be nested mul 😅
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 :)
I get it in exactly 10 lines in python using regexes
I bet some crazy ppl get it in even less lines
Long time no see
why not use regex ? but its really very nice to do it with out regex though nice to see this .
You answered your own question :D
I agree that it's nice to see various approaches.
First
regex pattern matching could maybe do it in 10 lines. i am not writing that lol
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
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
For the second part you can reduce complexity somewhat (imo) by instead preprocessing to remove don’t\(\).*(do\(\)|$) then you can just run the same logic from part one.