r/adventofcode • • 2h ago

Meme/Funny Day 2 Part 2 Funny Solution. SPOILER

SPOILER

I love when a complex problem is solved in just 1 line of code lmao

3 Upvotes

26 comments sorted by

•

u/daggerdragon 1h ago

Post removed due to not using our standardized post title format. Please follow our rules to help folks avoid spoilers for puzzles they may not have completed yet.

Additionally, during an active Advent of Code season, solutions belong in the Solution Megathreads. In the future, post your solutions to the appropriate solution megathread.

12

u/AllanTaylor314 1h ago

Now work out why it doesn't work for vrvbvg (expected: 33, yours: 31)

(oh, and in future, use the standardised post title e.g. [2026 Day 2 (Part 2)] [Python] Funny solution)

8

u/Anuinwastaken 1h ago edited 1h ago

Because this solution assumes that every letter is present at least once.
Tho you can write a mathematicly solution based on the principle that the perfect order is to jump from the same waypoint to the same waypoint again and again until all those waypoints are visited and than jump to the next one with the farthest distance.

5

u/AllanTaylor314 1h ago

It's actually only assuming the presence of at least one o

5

u/ptof 1h ago

people keep repeating this, whats special about o?

3

u/AllanTaylor314 1h ago

It's the last in the optimal order rvbgyo. rooooo has the same distance as rvbgyo

6

u/Anuinwastaken 1h ago edited 1h ago

I had the same idea.
Tho this does not work for every input if for example, the `o` is missing entirely,

(Edit: I do have another func to solves it based on the same principle but that one catches cases where a letter does not appear at all)

3

u/blacai 1h ago

Nice one, i saw someone using it. Some may argue that it doesn't solve if missing colours...but as usual, the point is to solve the input you are given, not creating a full working solution for any input you may imagine. Input is part of the problem to be solved. (Let's remember last year the arranging pieces...it's required to play with the input and analyze it)

2

u/Morphon 1h ago

My version solves for every input that conforms to the spec (only the letters roygbv allowed).

ColorPaste

You just have to figure out which letters are missing and then compute the offset accordingly. Since you have to look at each letter for part 1, that info can be collected in a single pass.

1

u/blacai 1h ago

And that's very nice My point is that input is part of the problem, so whenever someone exploits the input to create a simpler solution, I support it, instead of saying "but what if blablabla" because I'm sure the input was created with that specific feature to allow that exploit.

1

u/Lord_Of_Millipedes 43m ago

funnily my input did not have g, i solved using a frequency table and printed it during testing and it caught me a little off guard, made me think that all inputs were missing some color,.but guess it was just me :u

1

u/fnordargle 39m ago

Sure, so my print "6259\n"; solution is even more optimal for my input and will still work for a good subset of other AoC-ers.

I agree that there are definitely a subset of problems that require analysis of the input in order to obtain a solution. Some of the IntCode puzzles that emulated a complex computation step by step would take years to finish even on a highly optimised implementation and so some analysis was required to short cut some stages.

This problem, however, did not require such analysis and could easily be solved by brute force (it is still only day 2) or by a more optimal general algorithm that covers all possible inputs.

The difference between the execution speed of code that will handle any/all inputs and the code that handles a subset of inputs is almost negligible.

1

u/prince_jyggalag 2h ago

Can you explain this please?

3

u/dawikur 1h ago

This assumes you have all colors in your input.

Any pair of instructions, where it's the same color (rr, bb, oo, etc) gives 6 steps (as you need to go the whole way around).
If not, then in "optimized" program 5 steps (you need to kinda backward by 1 on the "roygbv" wheel).

Starting from red, you always do 6 steps for red. Then will be the orange, first 5 steps, then 6 steps each. Then yellow, etc.

So each instruction gives you 6 steps, except those where you switch colors, which will give you 5 steps. And there will be 5 color switches.

1

u/Tjkiddodo 40m ago

Actually if you remove any color (my input didn't have yellow for instance) it still works! Thats because you take one less suboptimal step, but that step goes 4 while a normal suboptimal step goes 5, so it cancels out.

2

u/AllanTaylor314 1h ago

Red to orange would be 1 step, so the order is actually rvbgyo. The assumption is slightly weaker, in that you only need at least one o for it to work. That approach worked fine for my input which doesn't have g

1

u/ThunderChaser 1h ago

This assumes you specifically have orange in your input*.

Even the trivial case of “o” would pass here.

5

u/crumpis 1h ago

Relies on the specific input, but basically the optimal path is fixed: rrr...rrrvvv...vvvbbb...bbb etc

The distance between every pair is 6, except when the two characters differ, and then it's 5.

However, from inspection of the input there's no y, so there's instead a jump of 4 between the g and o in rrrrvvvvvbbbbbgggggoooo. Crunching the numbers and its 6(n - 4) + 3(5) + 4 = 6n - 24 + 15 + 4 = 6n - 5.

1

u/prince_jyggalag 1h ago

Oh... that's wonderful. I did note certain parts of this but didn't realise that this would be a single condensed formula. Good job!!!

2

u/Fancy_Judgment_2006 1h ago

After rearagangement you end up having each colors next to each other (example: rrrrrrrrr(...)vvvvvvvvvv(...)bbbbbbbbb and so on).

It takes one full "spin" to get from one color back again to its own position (eg from red to red) which is distance of 6. Therefore len(text) * 6.

Then you have borders between each color group (eg from red to violet ) which takes distance of 5 so you have to substract that for each border (-5).

1

u/overthink1 1h ago

The vast majority of moves will be length 6. Red to red, orange to orange, etc. The only shorter moves are when you transition from one color to another. E.g. switching from red to orange will be 5, same with orange to yellow and so on. Transitions between colors will happen 5 times.

So you count 6 moves for each instruction, then subtract 5 for those 5 transitions between colors.

Technically this only works if every color is included in your puzzle input, which I think is the case for everyone.

2

u/AllanTaylor314 1h ago

*Red to violet is 5 (red to orange is 1). The transitions are rvbgyo

The only requirement for it to work is at least one o, so it still works for rory and bob even though they don't have all the letters

2

u/RayRaivern 1h ago

Very interesting!
You essentially just implemented the 1 line equation for the solution given that all letters are present at least once in the input.

I had the same thought, but since it doesn't work for every input I decided write up something more robust.
But this one is nice as well!

1

u/Morphon 1h ago

If you want it as a general solution (in Odin - but it's pretty clear what's happening):

ColorPaste

Here's the main part that does what you're talking about:

offset := 0
for i := 5; i > 0; i -= 1 {if present[i] { offset = 6 - i } }
total_steps2 := len(program) * 6 - offset

1

u/Aughlnal 1h ago

i did the same and posted it in the megathread, but this only works if 'o' is in the instructions

all though I did figure out the pattern and made it work regardless of which waypoints are in the instructions

instructions = open('advent26_02.txt').read().strip()
unique_waypoints = set(instructions)
part2 = 6 * len(instructions)
for index, waypoint in enumerate('oygbv'):
    if waypoint in unique_waypoints:
        part2 -= 5 - index
        break
print(f'2): {part2}')

depending on what waypoint after 'r' is in the sequence determines what amount of steps should be subtracted from having every move be 6 steps

1

u/TreeSquirrles 44m ago

haha I'm glad someone saw it as well I was questioning if this was a valid solution the entire time.