Unable to load image

rDrama Advent of Code Day 16: Energized Edition

Summary for those just joining us:

Advent of Code is an annual Christmas themed coding challenge that runs from December 1st until christmas. Each day the coding problems get progressively harder. We have a leaderboard and pretty good turnout, so feel free to hop in at any time and show your stuff!

Whether you have a single line monstrosity or a beautiful phone book sized stack of OOP code, you can export it in a nice little image for sharing at https://carbon.vercel.app

What did you think about today's problem?

https://adventofcode.com/2023

Our Code is 2416137-393b284c (No need to share your profile, you have the option to join anonymously if you don't want us to see your github)

15
Jump in the discussion.

No email address required.

https://i.rdrama.net/images/17027074193190532.webp

One thing I learned (from comparing my code to Snakes'), it's actually good to not abstract away directions to an enum or something, but to decide that they are integers that go around like so, so that you can turn them 90 degrees etc with simple math.

Jump in the discussion.

No email address required.

https://i.rdrama.net/images/17027079618324792.webp

:marseyitsover:

Jump in the discussion.

No email address required.

That's what I'm talking about, yeah. Bite the bullet and the code is way shorter and more understandable and debuggable. Like, look at your -, | nonsense, it's asymmetrical! Also memorizing what your 4 directions mean builds character.

Jump in the discussion.

No email address required.

Agree to disagree, it's not intuitive to represent the direction as anything other than an Enum or a tuple of deltas. The -, | nonsense is easy, you just start the second beam with the opposite direction as the incident one.

Jump in the discussion.

No email address required.

it's not intuitive to represent the direction as anything other than an Enum or a tuple of deltas.

0 points right, then you go clockwise (or counterclockwise is more traditional actually). Then you can do math modulo 4, +/- 1 turns it ninety degrees, +2 does a 180.

There is some time required to familiarize yourself with the encoding until you intuitively understand that 2 points left etc, I agree, but the ability to do the math above is well worth unabstracting it like that.

Jump in the discussion.

No email address required.

Also visited should be global for all beams. You should be able to type

       front = deque([start])
       visited = set(front)
       while front:
           row, col, dir = front.popleft()
           ...

in your sleep.

Jump in the discussion.

No email address required.

No, visited checks that beams haven't entered a loop, I have an energized variable for the tiles.

I can type that in my sleep, that's why I'm doing it in Nim.

Jump in the discussion.

No email address required.

No, visited checks that beams haven't entered a loop, I have an energized variable for the tiles.

The trick is to have (x, y, dir) as a key for your visited set, then compress it to an (x, y)-set for the answer. Also it looks like you didn't do the former for your beams and got lucky that it didn't blow up in your face.

Jump in the discussion.

No email address required.

Why would I be lucky? It's functionally equivalent.

Jump in the discussion.

No email address required.

I can't see the rest of your code, but from your data structure it seems that a beam that goes like this

-> > > V
   ^   V
   ^ < <

will stop instead of continuing upwards.

Jump in the discussion.

No email address required.

lmao you're right! I didn't run this code, it's my post-star refactoring. In the initial one I had a table of beams and directions rather than a per beam sequence.

Jump in the discussion.

No email address required.

a table of beams and directions

dis is da way.

Also btw, I had a useful runner for my code that downloads the input but also, importantly, maintains an answer database (a dict dumped into a json file). I did that specifically so that I can rewrite the code, especially after getting the first star and realizing that it's too slow for the second star, and still know that it gives the correct answer for part 1. Also when for when I change some utility function, then rerun all problems to see that they all still work. Highly recommend.

Jump in the discussion.

No email address required.

More comments
Link copied to clipboard
Action successful!
Error, please refresh the page and try again.