Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
Perl: https://github.com/gustafe/aoc2023/blob/main/d01-Trebuchet.pl
thoughts
I found this really tough for a day 1 problem. Because I don’t really know regex, I did not know the secret to finding overlapping patterns. I quickly figured out that “twone” was a possibility, because it was in the example, but then I had to find other examples and hardcode them into my split pattern.
This isn’t general, my input doesn’t have ‘sevenine’ for example.
I don’t have a Github account and don’t want one. I’d like to share some comments and thoughts about the regexes in my solution - is it OK if I wrap them in spoiler tags?
Abso-fucking-lutly. I just find sharing a link easier than wrangling code blocks.
There are also hipster “forges” like Sourcehut, if that’s your jam.
absolutely — removing my dependency on GitHub has actually been a blocker on me releasing code lately, and it’s something I want to tackle when we launch that open source community. if it helps collaboration, I can provide some ultra-janky git hosting on awful.systems with the same service that hosts our infrastructure code, though this’d be just basic git and gitweb with ssh public key auth
I made one solution in Python, and another in Scheme. Here are the regex-related parts.
Python:
spoiler
Here is a regex:·
The leading
?=
is a lookahead assertion. If we can read any of the subexpressionsone
,two
,…, it produces the empty string as a match, with the actual matche#So here’s the core of the solution:
Where
numeralize
is a simple function that converts “eight” into “8”. Not to the number 8, since we want to do string concatenation on them rather than addition#I haven’t written any Perl in ages, so I have no idea if Perl has an analogue to
findall
.Scheme:
spoiler
…but I do know that Scheme, at least CHICKEN, doesn’t. Here is the best my currently badly fever-addled brain could come up with:
I’m using the
irregex
library. I start by building an irregex out of a SRE (Scheme Regular Expression):This matches from the beginning of the string (hence
bos
), and doesn’t use any fancy lookahead. It only matches one string.Here is a little recursive function that finds all matches using the above:
It simply tries making a match from the beginning, and then continues making one from a substring with the first character hacked off, until it ends up with the emp#
Using that, the analogue of the solution I did for the Python version is:
Again, not my proudest Scheme code! I am currently nursing a fever and trying not to sneeze and cough my lungs to pieces.
I like your condensed solve method, yay for concise code
fuck yes scheme. you might have just inspired me to write some Lisp Machine Lisp solutions, since I might need a Lisp Machine codebase to test one of my side projects
I have a toy Lisp implemenation I used to work on, but I’ve lost the motivation.
Not that I think one more little virtual machine project makes a material difference either way, but I really don’t feel like spending my free time writing nontrivial code for loathsome AI companies.
Re Perl findall, I used this regex in my “clean” solution which I didn’t post because I figure it’s more honest to submit what worked, not the smart stuff you found out later.
regex
# find all numerals and number in English from the string $line and put them into the array @spelled my @spelled = ( $line =~ (m/(?=(\d{1}|one|two|three|four|five|six|seven|eight|nine))/g );
Nice!