18
votes
Day 2: Rock Paper Scissors
Today's problem description: https://adventofcode.com/2022/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>
My Python solution, taking advantage of modular arithmetic.
Part 1
Part 2
I think they've got a spreadsheeter on the team this year... that or its pity. Either way, this is the final form.
both parts
I combined the points from the get go and added onto the array for the second part. No use writing it out twice... or thrice, I guess.
No offence, but it's rare to see a beautiful spreadsheet answer, but this one is lovely.
ha -- thanks!
Got a bit hacky with my calculations, but this was pretty fun overall.
Part 1
Part 2
Python
That was interesting. At first I thought I could find a matrix that gives me the shape output for the desired outcome, but that proved cumbersome.
Then I decided I can just search for the desired outcome and then get the right shape.
I had to refactor my outcome matrix for part 2, since it was set up the other way round, and checking over multiple lists which result fits my desired outcome seems ugly.
Python
I immediately thought of a cheeky literal shortcut but decided to write a symbolic approach because it's fun and more in the spirit of a programming exercise like you might get at school.
Part 1
Part 2
The cheeky, but valid approach
attempt 1: ~70% elapsed time
attempt 2: ~35% elapsed time
Speedrun
Attempt 1:
Attempt 2 (aka throw memory at it):
Ruby
both parts
My questionable solutions in Java:
part 1
part 2
Part 2 tripped me up, had to move the original parser out of utilities and make a seperate parser for each one.
Really starting to appreciate rusts type system, allows me to reason really well about the data im passing around and how im transforming it.
Utilities
driver
part1
part2
I'm a big dumb dumb, so I just did it in the first way I thought of which is uh... not exactly elegant.
Nim (both parts)
I took a fairly straightforward and literal approach. I tried to use as few IF statements as possible for both parts. I'm timing my solutions and I'm happy to have both parts executing in around 0.1s.
Part 1 + 2
I'm confident that there's a tighter solution, but this is a simple puzzle and I'm in a lazy mood:
Parts 1 & 2, Clojure
Well today's python was even jankier! Part 2 took me absolutely forever, because I mixed up which letter corresponded to which outcome, and was getting very whacky results.
Part 1
Part 2
General musing and possible spoiler
I feel like if I knew how to make circular arrays, this would have been trivial to solve, since you could easily just make -1, 0, +1 correspond to win, draw, lose, respectively.Rust
Rust
I think I'm going to need to write a few extension modules for math/iterator stuff because the standard library functions for them seem to be pretty bare-bones compared to what I'm used to in Rust.
Another thing is that it feels like I'm having to write a ton of code compared to what I'd do for the same problem in Rust/Elixir.
Hopefully it's just because I'm not used to the language and I'm doing some weird stuff (like the unreachable default cases in
parseChoice
/parceOutcome
).Haxe
My Python solution is ugly and I knew it was ugly as I was writing it, but it works so I don't really care.
Parts 1 & 2
So I think I'll win today's "Ugly Janky Contest." I was already feeling lazy with Part 1, and Part 2 threw me for enough of a loop that its a mess.
Copying the map stuff I learned from yesterday's submissions here...real nice, I wish I had practiced more with it in my younger years. Also formally going into Typescript to avoid learning too many bad habits.
Code
I am actually happier with this than I thought I would be. I did most of the thinking and worked out the values for all the combinations, and just let Python crank on the calculating. I am also really happy with how I decided to do my testing, although I think if I tried harder I could probably make something that dynamically builds up the tests as I add days. Either way, it comes in handy. This code is also a little nicer because I actually ran and listened to
mypy
andblack
before posting my code! I am also going to leave out most of my fixture code, but if anyone is interested, just ask.Parts 1 and 2
And my new file loader that works in the way it probably should have from the beginning:
Here's my TypeScript solution
Part 1
Part 2
I'm using Rust this year, and trying to keep it
std
-only throughout the month.Part 1
Part 2