🦌 - 2023 DAY 2 SOLUTIONS -🦌
Day 2: Cube Conundrum
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
Found a C per-char solution, that is, no lines, splitting, lookahead, etc. It wasn't even necessary to keep match lengths for the color names because they all have unique characters, e.g. 'b' only occurs in "blue" so then you can attribute the count to that color.
Golfed:
Not too tricky today. Part 2 wasn't as big of a curveball as yesterday thankfully. I don't think it's the cleanest code I've ever written, but hey - the whole point of this is to get better at Rust, so I'll definitely be learning as I go, and coming back at the end to clean a lot of these up. I think for this one I'd like to look into a parsing crate like nom to clean up all the spliting and unwrapping in the two from() methods.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day02.rs
Rust
Pretty straightforward this time, the bulk of the work was clearly in parsing the input.
I had some time, so here's a terrible solution in Uiua (Run it here) :
Factor on github (with comments and imports):
A solution in Nim language. Pretty straightforward code. Most logic is just parsing input + a bit of functional utils: allIt checks if all items in a list within limits to check if game is possible and
mapItcollects red, green, blue cubes from each set of game.https://codeberg.org/Archargelod/aoc23-nim/src/branch/master/day_02/solution.nim
Another nim person! Have you joined the community? There are dozens of us!
Here's mine (no code blocks because kbin):
Yep, but it is a bit quiet in there.
Good solution. I like your parsing with scanf. The only reason I didn't use it myself - is that I found out about std/strscans literally yesterday.
I actually just learned about scanf while writing this. Only ended up using it in the one spot, since split worked well enough for the other bits. I really wanted to be able to use python-style unpacking, but in nim it only works for tuples. At least without writing macros, which I still haven't been able to wrap my head around.
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]
Ruby
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day02/day02.rb
Second part was soooo much easier today than yesterday. Helps I solved it exactly how he wanted you to solve it I think.
I'm going to work on some code golf now.
Golfed P2 down to 133 characters:
That's a nice golf! Clever use of the hash and nice compact reduce. I got my C both-parts solution down to 210 but it's not half as nice.
Thanks! Your C solution includes main, whereas I do some stuff to parse the lines before hand. I think it would only be 1 extra character if I wrote it to parse the input manually, but I just care for ease of use with these AoC problems so I don't like counting that, makes it harder to read for me lol. Your solution is really inventive. I was looking for something like that, but didn't ever get to your conclusion. I wonder if that would be longer in my solution or shorter 🤔
Rust (Rank 7421/6311) (Time after start 00:32:27/00:35:35)
Extremely easy part 2 today, I would say easier than part 1 but they share the same sort of framework
:::spoiler Code Block (Note lemmy removed some characters, code link shows them all)
:::
Code Link
String parsing! Always fun in C!
https://github.com/sjmulder/aoc/blob/master/2023/c/day02.c
Dart solution
Quite straightforward, though there's a sneaky trap in the test data for those of us who don't read the rules carefully enough.
Read, run and edit this solution in your browser: https://dartpad.dev/?id=203b3f0a9a1ad7a51daf14a1aeb6cf67
What's that? I didn't notice anything, perhaps I was lucky.
Oh, I misread the rules as each game having rounds of draws without replacement and the test data gave the same result for that reading, so when I confidently submitted my answer I got a bit of a surprise.
Same here, yesterday felt like a trap, but didn't run into anything today?
My (awful) Python solves. Much easier than day 1's, although I did run into an issue with trimming whitespace characters with my approach (Game 96 wouldn't flag properly).
::: spoiler Part 1
:::
::: spoiler Part 2
:::
Done in C# Input parsing done with a mixture of splits and Regex (no idea why everyone hates it?) capture groups.
I have overbuilt for both days, but not tripped on any of the 'traps' in the input data - generally expecting the input to be worse than it is... too used to actual data from users
::: spoiler Input Parsing (common)
public class Day2RoundInput { private Regex gameNumRegex = new Regex("[a-z]* ([0-9]*)", RegexOptions.IgnoreCase);
:::
::: spoiler Task1 internal class Day2Task1:IRunnable { public void Run() { var inputs = GetInputs();
:::
::: spoiler Task2 internal class Day2Task2:IRunnable { public void Run() { var inputs = GetInputs();
:::
Was pretty simple in Python with a regex to get the game number, and then the count of color. for part 2 instead of returning true/false whether the game is valid, you just max the count per color. No traps like in the first one, that I've seen, so it was surprisingly easy
Mostly an input parsing problem this time, but it was fun to use Hares tokenizer functions:
:::spoiler lua
:::
:::spoiler hare
:::
This was mostly straightforward... basically just parsing input. Here are my condensed solutions in Python
::: spoiler Part 1
:::
::: spoiler Part 2
For the second part, the main parsing remainded the same. I just had to change what I did with the games I read.
:::
GitHub Repo
Did mine in Odin. Found this day's to be super easy, most of the challenge was just parsing.
Getting my head around parsing tricks for python, maybe abusing dicts as a replacement for a types, but appears to be working: https://gist.github.com/purplemonkeymad/983eec7ff0629e8834163b17ec673958
Today in Zig
Spent most of the time running down annoying typos in the tokenizer.
Late as always, as I'm on UK time and can't work on these until late evening.
Part 01 and Part 02 in Rust 🦀 :
Python, interesting that the readMax function that I created on part 1 was also what I needed for part 2. https://github.com/massahud/advent-of-code-2023/blob/main/day02/day02.ipynb
My solution in python
[LANGUAGE: C#]
Part 1:
Part 2:
crystal
(commented parts are for part 1)
Man I really need to learn Crystal. I love the ruby syntax and it's supposed to be really fast right?
Yeah, it's still a very small language, not much community or tooling, but a pleasure to use. In practice it'll hit half the speeds of low level languages like rust and C, but with way less effort.
Haskell
A rather opaque parser, but much shorter than I could manage with Parsec.
[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.
::: spoiler Solution
:::
Is anyone else like me, but it took me longer to understand what I was supposed to do on day 2 than on day 1? However, here is my solution written in Golang: https://github.com/alexruf/adventofcode2023/tree/main/d02
It was confusing. It took me 3 tries to write the parser. The first time I didn’t realize there were multiple observations per game and the second time for some reason I was convinced the color came first.
Just getting started with Rust, part 1 took a long time. Really amazed when I saw part 2, just needed to add 2 lines and was done due to the approach I had taken. Feedback more than welcome!