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.

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

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.

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

For part2 is just part 1, but for every possible starting value. It runs in ~600ms. I then tried to add some logic that says if I already passed through a starting point with the same movement vector, I can just skip checking that starting point. That increased my time needed by 50% to 900ms.

using boost::unordered_flat_set instead of std::set to prevent looping, improves this to 160ms.

So anyone has a non brute force solution?

Jump in the discussion.

No email address required.

A recursive/DP solution where you cache the set of cells which get energized when you hit a deflector with certain direction should be fast. If you use 1 bit per cell there might be a clever way to bitwise OR the 2 sets of energized cells together.

Jump in the discussion.

No email address required.

I'm surprised he didn't force people to do that given it's day 16.

Jump in the discussion.

No email address required.

yall on some other shit i can't keep up

to learn list:

enums ?wtf are thos?

hash .. map???

memoization :interrobang#:

u guys are frickin nerds


:marseythonk: https://i.rdrama.net/images/1702755858979316.webp

I got the light beam going around but because of how I worded it it's now tricky figuring out how to handle closed loops

Jump in the discussion.

No email address required.

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

Another quality-of-life feature of Nim: arrays can be indexed by enum values. This is because arrays can be indexed by any ordinal type, and enums are one of them.

@SleighOut

Jump in the discussion.

No email address required.

That's wild I've never seen that syntax before. Doesn't seem to be in the docs and I'm getting Error: invalid order in array constructor when I try to mess with it. Where did you read about it?

Jump in the discussion.

No email address required.

I found it through a quick search, but it's also in the Nim manual. You're getting that error because the indices have to be in the same order as the enum.

Also, apparently the syntax gets even better: [Up: Down, Up, Right, Left][dir] will also work. "If an index is left out, succ(lastIndex) is used as the index value."

Jump in the discussion.

No email address required.

This felt straight-forward but I did manage to anticipate and work around a few issues that would have bogged me down 2 weeks ago. So I guess I am learning a bit from this. If I ever need to implement complicated algorithms in my next React CRUD I should handle it no problem :marseygiggle:

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

Jump in the discussion.

No email address required.

ngl bros today was hacky as shit

r←↑⊃⎕NGET'16.in'1 ⋄ D←≢r←'#'(,∘⌽∘⍉⍣4)r
ca←{(⊂⍵)⊃r}
n←{
   c←ca ⍺
   '#'≡c:⊂0 0
   '.'≡c:⊂⍵
   (0≡2⊃⍵)∧'|'≡c:⊂⍵
   (0≡1⊃⍵)∧'-'≡c:⊂⍵
   '/'≡c:⊂¯1×⌽⍵
   '\'≡c:⊂⌽⍵
   '|'≡c:(1 0)(¯1 0)
   '-'≡c:(0 1)(0 ¯1)
}
_r←(⊣,⊣-∘(⍳∘|××)-)
i←{
   np nd←⍺{d←⍺ n ⍵ ⋄ (⍺∘{p←⍺ ⋄ d←⍵ ⋄ {d+⍵}⍣{'.'≢ca ⍺}⊢p}¨d)d}⍵
   v⊢←∪v,t/⍨'#'≠ca¨t←⊃,/⍺∘{y1 x1←⍺
   y2 x2←⍵ ⋄ (y1 _r y2),¨(x1 _r x2)}¨np
   np nd←('#'≠ca¨np)∘/¨np nd
   o←np,¨nd ⋄ k←~o∊s ⋄ s⊢←∪s,o ⋄ k/o
}
m←{
   S←≢s ⋄ w←∊⍺ i¨⍵
   S≡≢s:≢v
   p←{((≢⍵)⍴1 1 0 0)⊆⍵}w ⋄ d←{((≢⍵)⍴0 0 1 1)⊆⍵}w
   p m d
}
v←⍬ ⋄ s←⍬ ⋄ p1←⊂2 2 ⋄ d1←⊂0 1
⎕←p1 m d1

l←1+⍳¯2+D ⋄ g←⍬
h←{v⊢←⍬ ⋄ s⊢←⍬ ⋄ g⊢←g,(⊂⍵)m ⍺}
(⊂1 0)∘h¨2∘,¨l
(⊂0 ¯1)∘h¨,∘(¯1+D)¨l
(⊂¯1 0)∘h¨(¯1+D)∘,¨l
(⊂0 1)∘h¨,∘2¨l
⎕←⌈/g
Jump in the discussion.

No email address required.

This is really gross but it works :tayshrug:

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

Copied the advice to not use an enum and got a much simpler result. Confused why everyone is using a deque

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

Jump in the discussion.

No email address required.

I couldn't be assed checking all four sides, so I just did the top row (25% chance) and got lucky first time lmao

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

Jump in the discussion.

No email address required.

>test case works

>actual input doesn't work

many such cases

Jump in the discussion.

No email address required.

you have the option to join anonymously if you don't want us to see your github

I thought it would give me that option when I joined the leaderboard but nope it slapped me into the rdrama leaderboard with my real name. I can't be bothered to make a second AoC login and submit all the answers twice so I guess I'm staying anonymous.

I thought today's problem was pretty bad. Eric's been talking about reflections and mirrors and pipes and lenses a lot but they're not tying together. Really lazy pt 2. This felt like a day 6 problem.

Jump in the discussion.

No email address required.

I also handled this one without much difficulty, but I can see how it'd be a nightmare for someone not comfortable with arrays and vectors. Difficulty is subjective depending on a codecel's skillset.

Jump in the discussion.

No email address required.

Sure but it's day 16. Anyone who got this far can already do arrays. The only difficulty here is that you need to account for direction as well as coordinate, but that's been done 100x before in past years. I'm not particularly good at DFS/BFS because I only do them once a year for AoC but this was as vanilla as it gets.

Jump in the discussion.

No email address required.

I'd say there was a fair bit more complexity in figuring out how to represent the state due to the forking feature. Add the possibility of loops and there's some extra handling to do.

I think the bottom line is that people have got good at solving these kinds of tasks precisely due to AoC. It gets easier each year as people get better.

Jump in the discussion.

No email address required.

Jump in the discussion.

No email address required.

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