Day 3 | Advent of Code 2024

preview_player
Показать описание

0:00 Statement
1:19 Solution
10:13 Hard Version

I make educational videos on algorithms, coding interviews and competitive programming.
Рекомендации по теме
Комментарии
Автор

wow that adding hashes at end is a great idea!

siddharth-gandhi
Автор

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.

gauravshah
Автор

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.

Woofer
Автор

How are you doing Errichtho, Where had you been, long time no see, no videos, how is life going?

ChandraShekhar-bycd
Автор

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 :)

barterjke
Автор

I'm learning C++. For this problem i used regular expressions

xVelocity
Автор

Even I was thinking part 2 was going to be nested mul 😅

mrlectus
Автор

I get it in exactly 10 lines in python using regexes
I bet some crazy ppl get it in even less lines

hussein-alemam
Автор

why not use regex ? but its really very nice to do it with out regex though nice to see this .

praveenpp
Автор

regex pattern matching could maybe do it in 10 lines. i am not writing that lol

MemeConnoisseur
Автор

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(", ")]
)
)

animesh_chouhan