🎄 - 2023 DAY 1 SOLUTIONS -🎄
Welcome everyone to the 2023 advent of code! Thank you all for stopping by and participating in it in programming.dev whether youre new to the event or doing it again.
This is an unofficial community for the event as no official spot exists on lemmy but ill be running it as best I can with Sigmatics modding as well. Ill be running a solution megathread every day where you can share solutions with other participants to compare your answers and to see the things other people come up with
Day 1: Trebuchet?!
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots
🔓 Edit: Post has been unlocked after 6 minutes
Python 3
I had some trouble getting Part 2 to work, until I realized that there could be overlap ( blbleightwoqsqs -> 82). ::: spoiler spoiler
::: I still have a hard time understanding regex, but I think it's getting there.
Rust
Part 1 Part 2
Fun with iterators! For the second part I went to regex for help.
A new C solution: without lookahead or backtracking! I keep a running tally of how many letters of each digit word were matched so far: https://github.com/sjmulder/aoc/blob/master/2023/c/day01.c
And golfed down:
Java
My take on a modern Java solution (parts 1 & 2).
::: spoiler spoiler
:::
Solution in C: https://github.com/sjmulder/aoc/blob/master/2023/c/day01-orig.c
Usually day 1 solutions are super short numeric things, this was a little more verbose. For part 2 I just loop over an array of digit names and use
strncmp().Part 02 in Rust 🦀 :
Thanks, used this as input for reading the Day 2 file and looping the lines, just getting started with rust :)
There weren't any zeros in the training data I got - the text seems to suggest that "0" is allowed but "zero" isn't.
That's very close to how I solved part 2 as well. Using the greedy wildcard in the regex to find the last number is quite elegant.
Solved part one in about thirty seconds. But wow, either my brain is just tired at this hour or I'm lacking in skill, but part two is harder than any other year has been on the first day. Anyway, I managed to solve it, but I absolutely hate it, and will definitely be coming back to try to clean this one up.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day01.rs
there where only two rules about posting here and you managed to break one of them
edit: oh sry, only one rule
Wow, I sure did. I was tired last night. I'll edit my solution in and fix my error.
Dart solution
This has got to be one of the biggest jumps in trickiness in a Day 1 puzzle. In the end I rolled my part 1 answer into the part 2 logic. [Edit: I've golfed it a bit since first posting it]
I feel ok about part 1, and just terrible about part 2.
day01.factoron github (with comments and imports):I wanted to see if it was possible to do part 1 in a single line of Python:
print(sum([(([int(i) for i in line if i.isdigit()][0]) * 10 + [int(i) for i in line if i.isdigit()][-1]) for line in open("input.txt")]))@calvin @Ategon
Anything is possible in one line if your terminal is wide enough.
Trickier than expected! I ran into an issue with Lua patterns, so I had to revert to a more verbose solution, which I then used in Hare as well.
Lua:
::: spoiler lua
:::
Hare:
::: spoiler hare
:::
[Rust] 11157/6740
Link
Oh, doing this is Rust is really simple.
I tried doing the same thing in Rust, but ended up doing it in Python instead.
Started a bit late due to setting up the thread and monitoring the leaderboard to open it up but still got it decently quick for having barely touched rust
Probably able to get it down shorter so might revisit it
Ive been trying to learn rust for like a month now and I figured I’d try aoc with rust instead of typescript. Your solution is way better than mine and also pretty incomprehensible to me lol. I suck at rust -_-
he used one letter variable names, it's very incomprehensible.
Get yourself thru the book and you'll get everything here.
Yeah tried to golf it a bit so its not very readable
Seems like the site doesn't track characters though so won't do that for future days
It basically just loops through every character on a line, if it's a number it sets last to that and if its not a number it checks if theres a substring starting on that point that equals one of the 10 strings that are numbers
In case that this might help.
h.chars() returns an iterator of characters. Then he concatenate chars and see if it's a digit or a number string.
You can swap match u.is_numeric() with if u.is_numeric and covert _ => branch to else.
Uiua solution
I may add solutions in Uiua depending on how easy I find them, so here's today's (also available to run online):
or stripping away all the fluff:
My solutin in Elixir for both part 1 and part 2 is below. It does use regex and with that there are many different ways to accomplish the goal. I'm no regex master so I made it as simple as possible and relied on the language a bit more. I'm sure there are cooler solutions with no regex too, this is just what I settled on:
::: spoiler https://pastebin.com/u1SYJ4tY
:::
My solution in rust. I'm sure there's a lot more clever ways to do it but this is what I came up with. ::: spoiler Code
:::
Ruby
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day01/day01.rb
Part 1
Part 2
Then of course I also code golfed it, but didn't try very hard.
P1 Code Golf
P2 Code Golf (ignore the formatting, I just didn't want to reformat to remove all the spaces, and it's easier to read this way.)
Thank you for sharing this. I also wrote a regular expression with
\d|eno|owtand so on, and I was not so proud of myself :). Good to know I wasn't the only one :).haha it's such a dumb solution, but it works. i've seen several others that have done the same thing. The alternatives all sounded too hard for my tired brain.
I was trying so hard to avoid doing this and landed on a pretty nice solution (for me). It's funny sering everyone's approach especially when you have no problem running through that barrier that I didn't want to 😆
Part 1 felt fairly pretty simple in Haskell:
Part 2 was more of a struggle, though I'm pretty happy with how it turned out. I ended up using
concatMap inits . tailsto generate all substrings, in order of appearance soone3mbecomes["","o","on","one","one3","one3m","","n","ne","ne3","ne3m","","e","e3","e3m","","3","3m","","m",""]. I then wrote a functionstringToDigit :: String -> Maybe Charwhich simultaneously filtered out the digits and standardised them asChars.I went a bit excessively Haskell with it, but I had my fun!
This is my solution in Nim:
Oh hey, a fellow nim person. Have you joined the community?
Here's mine. Kbin doesn't even support code blocks, so using topaz:
I have now, thanks for the tip!
And that's some very compact code! I've only just started with nim, so seeing more nim solutions is a great way to learn 😁
I'm not doing anything too fancy here, just the first stuff that comes to mind and gets the job done. The filterIt template was pretty handy for part 1, though. I assume at some point in these puzzles I'll have to actually write some types and procedures instead of just using nested loops for everything.
I think it's a pretty cool language overall. I've only used it for one project so far, so there's a bunch that I still don't know. Haven't been able to wrap my head around how macros work, for example, though I've sort of figured out how to write really basic templates.
I looked into the iterators and they are really handy, they remind me of C#'s Linq syntax.
Hi there! Looks like you linked to a Lemmy community using a URL instead of its name, which doesn't work well for people on different instances. Try fixing it like this: ![email protected]
Have a snippet of Ruby, something I hacked together as part of a solution during the WFH morning meeting;
Crystal. Second one was a pain.
part 1
part 2
Damn, lemmy's tabs are massive
[Language: C#]
This isn't the most performant or elegant, it's the first one that worked. I have 3 kids and a full time job. If I get through any of these, it'll be first pass through and first try that gets the correct answer.
Part 1 was very easy, just iterated the string checking if the char was a digit. Ditto for the last, by reversing the string. Part 2 was also not super hard, I settled on re-using the iterative approach, checking each string lookup value first (on a substring of the current char), and if the current char isn't the start of a word, then checking if the char was a digit. Getting the last number required reversing the string and the lookup map.
Part 1:
Part 2:
Just getting my feet wet with coding after a decade of 0 programming. CS just didn't work out for me in school, so I swapped over to math. Trying to use Python on my desktop, with Notepad++ and Windows Shell.
::: spoiler Part 1
:::
::: spoiler Part 2
:::
I know my entire thing is kinda super hobbled together. Any recommendations on how I can make this all easier on myself?
Python 3
I'm trying to practice writing clear, commented, testable functions, so I added some things that are strictly unnecessary for the challenge (docstrings, error raising, type hints, tests...), but I think it's a necessary exercise for me. If anyone has comments or criticism about my attempt at "best practices," please let me know!
Also, I thought it was odd that the correct answer to part 2 requires that you allow for overlapping letters such as "threeight", but that doesn't occur in the sample input. I imagine that many people will hit a wall wondering why their answer is rejected.
::: spoiler day01.py
:::
::: spoiler test_day01.py
:::
I did this in C. First part was fairly trivial, iterate over the line, find first and last number, easy.
Second part had me a bit worried i would need a more string friendly library/language, until i worked out that i can just
strstrto find "one", and then in place switch that to "o1e", and so on. Then run part1 code over the modified buffer. I originally did "1ne", but overlaps such as "eightwo" meant that i got the 2, but missed the 8.Just realised how inefficient the sanitize function is, it iterates over the buffer way too many times. Should be restarting the strnstr from the location of the last hit instead of from the start.
Did this in Odin (very hashed together, especially finding the last number in part 2):
::: spoiler spoiler
:::
Had a ton of trouble with part 1 until I realized I misinterpreted it. Especially annoying because the example was working fine. So paradoxically part 2 was easier than 1.
Part-A in Python: https://github.com/pbui/advent-of-code-2023/blob/master/day01/day01-A.py
Was able to use a list comprehension to read the input.
Part-B in Python: https://github.com/pbui/advent-of-code-2023/blob/master/day01/day01-B.py
This was trickier...
::: spoiler Hint You have to account for overlapping words such as:
eightwo. This actually simplifies things as you just need to go letter by letter and check if it is a digit or one of the words. :::Update: Modified Part 2 to be more functional again by using a
mapbefore IfilterRe. your hint, that's funny because I skipped the obvious optimization of skipping over matched letters out of laziness, but it would've actually broken the solution.
Note you can put this on a separate post and should get some responses from that
(this is supposed to be for solutions only so people arent browsing it to solve help requests)
Oh sorry, just saw this thread and got excited, will delete to keep it clean
The problem is probably that letters can overlap:
eightwo -> 82 instead of 88
[Language: Lean4]
I'll only post the actual parsing and solution. I have written some helpers which are in other files, as is the main function. For the full code, please see my github repo.
Part 2 is a bit ugly, but I'm still new to Lean4, and writing it this way (structural recursion) just worked without a proof or termination.
::: spoiler Solution
:::
I decided to learn zig this year. Usually I solve both parts in the same source file, but it was annoying so here part 1 and part 2
My solution is not pretty at all, but it does the job: Golang