Day 3: Gear Ratios
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 11 minutes
36 Comments
cacheson@kbin.social · 8 pts · 2y
Nim
I hope y'all like nested loops:
Gobbel2000@feddit.de · 2 pts · 2y
I like it, it's simple and to the point. I've learned that one of the most helpful things to do when solving these puzzles is to not make it more complicated than it needs to be, and you certainly succeeded better at that today than I did.
cacheson@kbin.social · 1 pts · 2y
My solution for day 1 part 1 was simple and to the point. The other ones are getting increasingly less so. You're right that sometimes it's best not to get too fancy, but I think soon I may have to break out such advanced programming techniques as "functions" and maybe "objects", instead of writing increasingly convoluted piles of nested loops. xD
abbadon420@lemm.ee · 1 pts · 2y
This is the way
CommunityLinkFixer@lemmings.world · 1 pts · 2y
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: !nim@programming.dev
janAkali@lemmy.one · 5 pts · 2y
That was not fun to solve. Lots of ugly code. This is a less ugly second version. Language: Nim.
day_03.nim
Gobbel2000@feddit.de · 4 pts · 2y
Rust
I've been using Regexes for every day so far, this time it helped in finding numbers along with their start and end position in a line. For the second part I mostly went with the approach of part 1 which was to look at all numbers and then figure out if it has a part symbol around it. Only in part 2 I saved all numbers next to a gear
*in a hash table that maps each gear position to a list of adjacent numbers. Then in the end I can just look at all gears with exactly 2 numbers attached.Also it has to be said, multiplying two numbers is the exact opposite of getting their ratio!
pngwen@lemmy.sdf.org · 4 pts · 2y
I wrote today's program in Python. (I am going to do a different language each day.) The only thing that gave me a little trouble was I was counting "\n" as a part label. Once I realized that I was able to get both problems done very quickly.
My code for the two parts is on github:-
Nighed@sffa.community · 3 pts · 2y
Language: C#
I aimed at keeping it as simple and short as reasonably possible this time, no overbuilding here!
I even used a goto to let me break out of multiple loops at once 🤮 (I had to look up how they worked!) I would totally fail me in a code review!
::: spoiler One solution for both
:::
Strawberry@lemmy.blahaj.zone · 3 pts · 2y
Python solution
just more parsing problems
hades@lemm.ee · 3 pts · 2y
bugsmith@programming.dev · 3 pts · 2y
Edit: Updated now with part 2.
Managed to have a crack at this a bit earlier today, I've only done Part 01 so far. I'll update with Part 02 later.
I tackled this with the personal challenge of not loading the entire puzzle input into memory, which would have made this a bit easier.
Solution in Rust 🦀
View formatted on GitLab
zerot@kbin.social · 2 pts · 2y
F# solution
Had an off-by-one error that took me a while to find
capitalpb@programming.dev · 2 pts · 2y
Another day of the 2023 Advent of Code, and another day where I hate looking at my code. This year just seems like it is starting off a lot more complex than I remember in previous years. This one was a little tricky, but I got there without any major setbacks. Another one I am excited to come back to and clean up, but this first pass is all about getting a solution, and this one works.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day03.rs
Andy@programming.dev · 2 pts · 2y
Factor on github (with comments and imports):
morrowind@lemmy.ml · 2 pts · 2y
Crystal
My computer crashed right most of the way through and I lost everything, so this was even more frustrating than it should have been
Also damn, lemmy's tabs are massive
will post part 2 when I get to it
asyncrosaurus@programming.dev · 2 pts · 2y
[LANGUAGE: C#]
I kept trying to create clever solutions, but ended up falling back on regex when it was taking to long. THE TLDR is we scan the list of strings for a symbol, then parse the three lines above, below and inline with the symbol for digits. Then we try and match the indexes of the match and the area around the symbol. Part 2 was a small modification, and was mostly about getting the existing code to conform the data into a pattern for each of the three lines.
Part 1
Part 2:
Massahud@programming.dev · 2 pts · 2y
Python, used a number map to search positions near symbols.
https://github.com/massahud/advent-of-code-2023/blob/main/day03/day03.ipynb
StreetKid@reddthat.com · 2 pts · 2y
My Python solution for part 1 and part 2. I really practice my regex skills.
::: spoiler spoiler
:::
Adanisi@lemmy.zip · 2 pts · 2y
snowe@programming.dev · 2 pts · 2y
Ruby
My goodness I struggled today. https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day03/day03.rb
CommunityLinkFixer@lemmings.world · 1 pts · 2y
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: !ruby@programming.dev
Ategon@programming.dev · 2 pts · 2y
[Rust] Harder one today, for part 1 I ended up getting stuck for a bit since I wasnt taking numbers at the end of lines into account and in part 2 I defined my gears vector in the wrong spot and spent a bit debugging that
:::spoiler Code (lemmy removes some chars, all chars are in code link)
:::
Code Link
RowanCH@lemmy.blahaj.zone · 2 pts · 2y
Language: C++
Efficiency? Elegant Code? Nope but It works. Luckily I did part 1 by looking at the symbols first anyway, so extending to part two was trivial. Also originally had a bug where I treated all symbols as cogs, not only '*'. Interestingly it worked anyway as only '*'s had two adjacent numbers in my data. It is fixed in this version. Hacked together combined code (originally I did each part as separate programs but they shared so much that I ended up combining then so the post is shorter): https://pastebin.com/Dij2XSYe
Edit: anything in angle brackets is not displaying even with backslashes, idk why but i have moved the code to a pastebin.
vgnh@mastodon.social · 2 pts · 2y
pnutzh4x0r@lemmy.ndlug.org · 2 pts · 2y
Language: Python
Classic AoC grid problem... Tedious as usual, but very doable. Took my time and I'm pretty happy with the result. :]
::: spoiler Part 1
For the first part, I decided to break the problem into: 1. Reading the schematic, 2. Finding the numbers, 3. Finding the parts. This was useful for Part 2 as I could re-use my
read_schematicandfind_numbersfunctions.Two things I typically do for grid problems:
DIRECTIONSlist I loop through so I can check easily check the neighbors.:::
::: spoiler Part 2
For the second part, I just found the stars, and then I found the gears by checking if the stars are next to two numbers (which I had found previously).
:::
GitHub Repo
ace@lemmy.ananace.dev · 2 pts · 2y
I get the feeling that I should include some default types for handling 2D maps in my boilerplate, it's a very recurring problem in AoC after all.
My solution is reasonably simplistic - and therefore also a bit slow, but the design meant I could do part 2 with just a few extra lines of code on the already processed data, here's the functional part of it; (I push the previous days solution as part of my workflow for starting with the current day so the full code won't be up until tomorrow) :::spoiler Ruby The code has been compressed for brevity.
:::
sjmulder@lemmy.sdf.org · 2 pts · 2y
Language: C
Part 2 stumped me for a little bit, it wasn't an obvious extension of part 1. Part 1 was about numbers (with one or more ...) while part 2 worked from the symbols (with exactly two ...). Going the other way would require more bookkeeping to avoid double counting.
And for the implementation: if you loop over the grid and check surrounding cells for digits you'd have to account for a bunch of cases, e.g. NW/N or N/NE being part of the same number or NW and NE being part of separate numbers. And you'd have to parse the numbers again. But building a graph or reference list of some sort is both unergonomic with C and not necessarily any simpler.
I ended up just writing out the cases, and honestly it didn't turn out too bad.
GitHub link
:::spoiler Abridged code
:::
mykl@lemmy.world · 2 pts · 2y
Dart Solution
Holy moley, if this year is intended to be impervious to AI solution, it's also working pretty well as a filter to this paltry Human Intelligence.
Find interesting symbols, look around for digits, and expand these into numbers. A dirty hacky solution leaning on a re-used Grid class. Not recommended.
morrowind@lemmy.ml · 2 pts · 2y
what language is this? I don't recognize it
mykl@lemmy.world · 1 pts · 2y
Sorry, it’s Dart. I’ll update it.
Jummit@lemmy.one · 2 pts · 2y
Input parsing AGAIN?
::: spoiler Lua
:::
::: spoiler Hare (Part one only)
:::
eight_byte@feddit.de · 2 pts · 2y
Only had the time to solve part 1, but had a lot of fun with it. Here's my solution: d03/p1
soulsource@discuss.tchncs.de · 1 pts · 2y
[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.
Here I used HashMap and HashSet, but that's just an optimization. I'm not even sure if they are faster than just using lists here...
::: spoiler Solution
:::
kartoffelsaft@programming.dev · 1 pts · 2y
Did this in Odin
Here's a tip: if you are using a language / standard library that doesn't have a set, you can mimic it with a map from your key to a nullary (in this case an empty struct)
formatted code
sjmulder@lemmy.sdf.org · 3 pts · 2y
Probably meant to write 'a set'? Good trick though.
kartoffelsaft@programming.dev · 1 pts · 2y
Oh yeah, I misspoke, gonna edit.
kartoffelsaft@programming.dev · 1 pts · 2y
hmm, my code keeps getting truncated at
for y in .., anyone have any idea why? Maybe the "<" right after that confuses a parser somewhere?Ategon@programming.dev · 2 pts · 2y
Lemmy doesn't handle certain characters well currently such as left angle brackets and ampersands due to some sanitization in the back end to stop scripting attacks