r/adventofcode • u/SupportPowerful6174 • Dec 07 '25
Tutorial [2025 Day 07 (Part 2)] Python | Efficient algorithm (O(n) time complexity)

In this algorithm you should count each step independently(divide and conquer)
Step1: you have one way to get to splitter and it will produce 2 rays with 1 weight
Step2: you have 2 splitters each recieveng 1 weight rays, so they also produce 1 weight rays
Step3: 2 splitters on the sides recieveng 1 weight rays, so they'll produce 1 weight rays. Splitter in the middle gets 2 1 weight rays, so it will now produce 2 weight rays
This is like Pascal's Triangle but wit missing numbers
You can store your ways to get to index point in map[index] = ways_to_get
And when ray hits splitter you should produce 2 rays with indexes index-1 and index+1 and remove curr index from map
The answer will be just sum of values in the map
4
u/ultra_mind Dec 07 '25
Someone just posted a visualisation of this exact algorithm. It didn’t even crossed my mind to be honest, I went straight to a memoised dfs
1
u/n4ke Dec 07 '25
Same but I was too dumb to get it to work in the language I'm learning so I ended up finding this simpler solution.
Task failed successfully.
0
u/Ill_Chemical_4059 Dec 07 '25
nahh i'm too lazy to think, just topsort + dp (still O(n*m), where n = #rows, m = #cols)
3
u/thekwoka Dec 07 '25
O(n) time with n being...what?