Day one part 2 was relatively rough. Things I learned from it: rust regex crate doesn't support look-ahead, rust onig crate is currently broken in many ways and shouldn't be used (the version in crates.io doesn't compile and the version on GitHub is failing tests and look-ahead isn't working). It was a very frustrating time for me. After 2 hours of troubleshooting the above I used the same approach in python and it t…
Advent of Code 2023 is nigh
261–270 of 319 posts
Re: Advent of Code 2023 is nigh
#262Earlier quoted context omitted.
I first brute-forced the solve (on each line, find and rfind every digit, keep the smallest find and the largest rfind), that worked fine out of the box as that's not sensible to overlap. I then figured I'd use aho-corasick because that was an opportunity to, and there's no kill like overkill. I then proceeded to waste half an hour because I didn't read the documentation, so I didn't see that `find_iter` finds non-ov…
Yeah, the overlapping case is the less common case in my experience. And even in the non-overlapping case, the "standard" approach is often not what you might expect. For example, if one were to use the Aho-Corasick algorithm to implement a regex like `samwise|sam`, the standard algorithm would yield incorrect results if you expect it to behave like, say, Perl or Javascript regexes. That's what led me to develop `Mat…
Re: Advent of Code 2023 is nigh
#263I challenge myself to do it in bash one liners. I came up with a clever and shockingly simple solution to part2 using expansion and substitution. cat 1.txt | sed -E 's/(one)/\11\1/g; s/(two)/\12\1/g; s/(three)/\13\1/g; s/(four)/\14\1/g; s/(five)/\15\1/g; s/(six)/\16\1/g; s/(seven)/\17\1/g; s/(eight)/\18\1/g; s/(nine)/\19\1/g;' | sed -e 's/[^0-9]//g' | awk '{print substr($0,1,1) substr($0,length,1)}' | tr '\n' '+' | s…
Re: Advent of Code 2023 is nigh
#264Earlier quoted context omitted.
You might try BurntSushi's aho-corasick crate. It led to a fairly nice solution in Rust for this one. (It will report overlapping matches)
I would assume you can also use RegexSet from the regex crate, as it > match(es) multiple, possibly overlapping, regexes in a single search.
You can drop down to regex-automata, which does let you do multi-regex search and it will tell you which patterns match[1]. The docs have an example of a simple lexer[2]. But... that will only give you non-overlapping matches.
You can drop down to an even lower level of abstraction and get multi-pattern overlapping matches[3], but it's awkward. The comment there explains that I had initially tried to provide a higher level API for it, but was unsure of what the semantics should be. Getting the starting position in particular is a bit of a wrinkle.
[1]: https://docs.rs/regex-automata/latest/regex_automata/meta/in...
[2]: https://docs.rs/regex-automata/latest/regex_automata/meta/st...
[3]: https://github.com/rust-lang/regex/blob/837fd85e79fac2a4ea64...
Re: Advent of Code 2023 is nigh
#265https://github.com/emiruz/adventofcode2023/blob/main/day01/p...
Re: Advent of Code 2023 is nigh
#266I challenge myself to do it in bash one liners. I came up with a clever and shockingly simple solution to part2 using expansion and substitution. cat 1.txt | sed -E 's/(one)/\11\1/g; s/(two)/\12\1/g; s/(three)/\13\1/g; s/(four)/\14\1/g; s/(five)/\15\1/g; s/(six)/\16\1/g; s/(seven)/\17\1/g; s/(eight)/\18\1/g; s/(nine)/\19\1/g;' | sed -e 's/[^0-9]//g' | awk '{print substr($0,1,1) substr($0,length,1)}' | tr '\n' '+' | s…
does it deal with cases like "twone", "nineight" and so on? it doesn't appear so to me because of the leading sed statements would commit the interpretation regardless of what happens next, but perhaps there is something subtle im not seeing.
Re: Advent of Code 2023 is nigh
#267Earlier quoted context omitted.
Because it is a mechanism for ReDOS, and the standard library should not be introducing vulnerabilities into users. Other libraries can implement it for folks who decide they really need it.
You need to put a time limit on your regex execution no matter what, if you're parsing untrusted input.
One of the key advantages of a regex engine based on finite automata is that it lets you make guarantees about the runtime performance of a search.
Re: Advent of Code 2023 is nigh
#268Day one part 2 was relatively rough. Things I learned from it: rust regex crate doesn't support look-ahead, rust onig crate is currently broken in many ways and shouldn't be used (the version in crates.io doesn't compile and the version on GitHub is failing tests and look-ahead isn't working). It was a very frustrating time for me. After 2 hours of troubleshooting the above I used the same approach in python and it t…
Was regex even necessary?
"necessary" is a strong word. :-)
Re: Advent of Code 2023 is nigh
#269I think the edge cases were entirely unclear in day 1, part 2. I had to redo it in a "dumb"/brute-force way to avoid using fancy regex tricks I don't know. It's quite clear the small sample data was chosen intentionally to not cover them.
I wonder if it is because of ChatGPT and friends.
Re: Advent of Code 2023 is nigh
#270Advent of Code means a lot to me. The problems are fun, sure, but something about it really boosts my winter. I've noticed that I get a lot more productive with my hobbies in the weeks following AoC. I think maybe it's as simple as living on the west coast and getting used to getting amped and doing some big, adrenaline-filled, social, fun activity at 9 PM every night. It gets me used to being PRODUCTIVE in the eveni…