12
votes
Day 2: Cube Conundrum
Today's problem description: https://adventofcode.com/2023/day/2
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
This was mostly straightforward... basically just parsing input. Here are my condensed solutions in Python
Part 1
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
More Java. I didn't need to make a class or anything, but I figured why not. I'm still pretty noob at Java (< 12 months), so I'm not sure what counts as "idiomatic java" or not. I'm happy with it, though.
Solution
Making classes for everything even when you don't need to is as idiomatic as Java gets
I was happy to see I was fast enough this time to have the site tell me I got a leaderboard spot (though it's only top 1000, haha). This was a neat problem, though I found part 2 to be rather easy the way I had my code set up.
Solution
Today's problem was very straightforward without any gotchas that I ran into.
I'm going for solve speed rather than code golfing for now so there's nothing fancy in my solution.
Rust
Edit: I'll almost certainly need to use something more powerful than just regex at some point so I decided to make another version using logos to refresh my memory of it.
I'll probably also make a nom version of today's or tomorrow's puzzle.
Rust - Logos
Run Times
today was the day i decided to learn nom, it was really confusing at first, but I think im warming up to it.
Nice! I've been really digging
just
as a program. Is powerful, but stays out of my way.Julia
This task was pretty simple, so I was able to solve it directly from the Julia REPL.
I'm actually somewhat surprised seeing how long other people's solutions are.
Part 1
Part 2
I wrote the Kubernetes Jsonnet library at work (it unfortunately came out 3 months before Grafana Tanka did). So I suppose this would be a good exercise in Jsonnet, and perhaps functional programming.
Turns out Jsonnet is pretty slow. Solving both problems in the same invocation takes almost 4 seconds on my i9. Parsing of the input is also a bit annoying as
std.split
only take a single separator, so the leading space must be stripped later.Note that the test doesn't get run if it's not in the output (the
test::
must be changed totest:
)Maybe I'll try Nix tomorrow...
Rust
Day 2
Google Sheets
I don't like the way I did the first part
part 1
part 2
Step-by-step Python explanation: https://advent-of-code.xavd.id/writeups/2023/day/2/
Pretty straightforward today - Python's
re.findall
and a regex got me all the pairs of count + color and adefaultdict
made it easy to calculate the min number required. Oddly, this felt more like a day 1 than yesterday did 😅Nice writeup! One neat alternative to using
reduce
andmul
on an iterable is to usemath.prod
.Ah, that's awesome! I always use
sum
but never knew there was a multiplication version!Elixir
Pretty straightforward.
Map.merge/2
andMap.merge/3
were helpful.Parts 1 and 2
Not much to say, pretty straightforward overall. AWK solutions:
Part 1
Part 2
My solution in Common Lisp.
FSet made this fairly straightforward. I represented each game round as a bag (aka a multiset). Allowed games for part 1 were games where every round was a subset of a conjectured bag. Minimum cube quantities were just the union of all of the rounds for a game.
The one thing that tripped me up for a bit was that when I calculated the “power” of a set of cubes. I didn't see a way to map across just a bag's multiplicities, so I converted the bag to a set, used
fset:image
to map across that set, callingfset:multiplicity
for each cube color. Well, that puts the results into a set, too, so it was collapsing duplicate multiplicities (e.g. “1 red, 2 blue, 2 green” turned into a set of just 1 and 2). I dealt with that by turning the set back into a bag and everything came out right.Guile Scheme again, and again my solution feels... Complicated.
My
utils.scm
got an addition:utils.scm
It makes sure everything in the `lst` maps to `true` when `proc` is applied to itPart 1
For part 2 I ran through every game again to calculate the number of cubes.
Part 2
Didn't start this one until the afternoon.
Day 2 - Ruby
Rust, decided to take the oppurtunity to try out nom for parsing. Suprisingly intuitive once you get used to it.
https://git.venberg.xyz/Gabe/advent_of_code_2023/src/branch/main/days/day02
Rust again this year, helped by working with it a lot in the past year. Like last year, I'm going to stick to the standard library for as long as I can. Hopefully I won't fall completely off of doing this like I did last year!
Parts 1 and 2 at once because, as other have mentioned, the complexity today was entirely in the parsing.
Here are my Python solutions:
Part 1
Part 2
Btw, today's post isn't tagged
advent_of_code.2023
so I missed it on the tag's RSS feed. Could we please tag these?Yesterday I hadn't written a line of Kotlin before. Today I learned more of its conveniences.
Kotlin
A not-very-exciting Golang solution. I tend to think in object-oriented terms which is slow but (IMO) clear.
I am trying to guess at what primitives / helpers will be useful, like a helper that parses "Game 1" and "4 red" as we're likely to hit inputs with similar structure later. My experience is that the AoC folks are pretty good at mixing things up, so I don't spend a lot of time implementing more than what is immediately needed.
Github
Both parts
Plus the parsing helper function