Doing Advent of Code like every year. Last year I wrote blog posts every day but idk if I have the energy for that this time around; my goal this year is to produce at least one or two interesting gifs of not necessarily solutions, but something relevant to the solution. But since those are exponentially more effort than just solving it, I’m not actually committing lmao…
I was briefly considering writing my detection in a way that’s robust against removing any one element, but it’s honestly just much easier (and way fast enough) to simply try every possible one-element-removed list.
Excited to participate for the first time this year! My friend said AoC really helped him learn a lot, so hopefully it does the same for me c: I’ll be doing it in Python
Yep, I learned multiple languages using it over the years, as well as (Neo)vim last year! It just gives you something relatively low stakes but concrete to do, which is great practice.
I originally had a neat iterator-based solution for part 1, because if you really don’t care about anything but the muls, you can liberally discard stuff left and right. But after I wrote out the full version for part 2, I fused the solutions because that’s neater. Old solution here though, for posterity (thanks, Neovims insane undo feature!):
Previous part 1 code
pub fn one(input: &str) -> crate::Result<i32> {
Ok(input
.split("mul(")
.filter_map(|t| {
let (t, _) = t.split_once(')')?;
let (a, b) = t.split_once(',')?;
let (a, b) = (a.parse::<i32>().ok()?, b.parse::<i32>().ok()?);
Some(a * b)
})
.sum())
}
And day 4. This finally pushed me to write my own implementation of a carthesian product (ie. all combinations of elements from two lists). Also even though it’s technically redundant, I wrote a carthesian product for three lists, too–just for ergonomics.
I use this product all the time in code I write, and it’s available in a popular extension library, but I figured in the spirit of the challenge I should handroll it.
That’s day 5 done, too. I started out not particularly liking my solution, it felt a bit too verbose for what I’m doing, But it’s fine actually, and I actually really enjoy the main trick I did in part 2.
6 kind of had teeth for a day 6; and day 7 wasn’t so bad except for the part where I did it really badly at first because I was in a rush to go play PoE2 lol. Now that I got that out of my system a little bit (that’s a lie, I’m going right back in a sec), I realized that this is exactly the sort of thing that you can easily do recursvely…