day 03 - advent of code 2024 in rust

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


## Chapters

00:00 Day 03
01:06 mul parser
04:19 skipping junk
06:34 nom::Parser::map
08:10 running instructions
10:26 part 2
11:40 parsing instructions
13:26 Processing Instructions
16:49 recap part 1
18:09 recap part 2
21:04 fin
Рекомендации по теме
Комментарии
Автор

huh, I hadn't heard of Nom before, that's really cool! Definitely a tool to note down for later. I just pulled all the tokens out of a regex 😅

tacticalfluke
Автор

Very nice. Gonna have to start using nom for parsing because I was just manually parsing the string input using .split and .find haha

zkeltonETH
Автор

Nom looks so easy when you do it, but when I try I just get issues between `&str` and `&[u8]` or many other hurdles. :(

awdsqe
Автор

Nice solution!
Clever combination of many1 and many_till. Didn't know about those and wrapped everything in Options to discard what I don't need.
```
many0(alt((
map(instruction_parser, Some),
// Consume one char and return None if no instruction matches
map(anychar, |_| None),
)));
```
Your approach seems more idiomatic to nom imo.
At 3:11 you are resizing the terminal window and the vs-code window adjusts its size as well. What magic is that? Looks very useful.

flwi
Автор

I followed your part 1 just because I watched the video at work and was interested in the base nom parsing. For part 2, which I found logically pretty simple, but my own logic was failing me a bit, I diverged and instead of doing the whole `ShouldProcess` additional enum, i did similar logic with an impl function on Instruction


impl Instruction {
fn multiply(&self, process: Instruction) -> (Instruction, u32) {
match (self, process.clone()) {
(Instruction::Mul((x, y)), Instruction::Do) => (Instruction::Do, x * y),
(Instruction::Dont, _) => (Instruction::Dont, 0),
(Instruction::Do, _) => (Instruction::Do, 0),
(_, Instruction::Dont) => (Instruction::Dont, 0),
_ => (process, 0),
}
}
}

HirschyKiss
Автор

I feel like nom is nice but for most scenarios it's over-engineered. The code is more complicated for simple parsers. It's also longer. I really like your videos because that's how i have learned NOM - I was following your AOC in the past. I can now use it, but sometimes it's simpler to use just regex even if it generates heavier code. Regex can be simple as well, and code can be like at most 20 lines long :)

daniel
Автор

Using crates feels like cheating. I personally use std only for advent of code

ddg-norysq