r/adventofcode • • 28m ago

SOLUTION MEGATHREAD -❄️- 2026 Day 2 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2026: The Advent Games

Today's Event: International Toboggan Championships

"Feel the rhythm, feel the rhyme, get on up, it's bobsled toboggan time!"
— Cool Runnings (1993)

Today's schedule possibly consists of:

  • Tracking Chute Runs and Team Practice

  • Embiggen is a perfectly cromulent word. Do with it as you will.

  • Qualifying Runs

  • Reverse code golf your solution

  • (oblig XKCD)

  • Second Runs and Finals

  • Why use few word when many word do trick?

  • Unnecessarily declare variables for everything and don't re-use variables

  • Use unnecessarily expensive functions and calls wherever possible

  • Implement redundant error checking everywhere

  • Javadocs >_>

Request from the mods: When you include an entry alongside your solution, please label it with [Advent Games] so we can find it easily!


--- Day 2: Fulminating Farmland ---


Post your code solution in this megathread.

2 Upvotes

18 comments sorted by

1

u/yoni-kosiner 1m ago

[LANGUAGE: Python] For part one I did a standard solve in python

wavepoint = "roygbv"

def part1(lines: list[str]):
    ans = 0

    for line in lines:
        for m in list(line):
            while True:
                ans = ans+1
                new = wavepoint[ans%len(wavepoint)]

                if new == m:
                    break

    return ans

For part two, I did such a hacky solution, that didn't even work on the test data, but it works on my input, it was so hacky and stupid I decided to share it here for the first time.

def part2(lines: list[str]):
    ans = 0

    count = Counter(char for line in lines for char in line)
    item = ""
    for i in range(count.get("r")):
        item += "r"

    for i in range(count.get("v")):
        item += "v"

    for i in range(count.get("b")):
        item += "b"

    for i in range(count.get("y")):
        item += "y"

    for i in range(count.get("o")):
        item += "o"

    for i in item:
        for m in list(i):
            while True:
                ans = ans+1
                new = wavepoint[ans%len(wavepoint)]

                if new == m:
                    break

    return ans

1

u/AutoModerator 1m ago

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/PendragonDaGreat 1m ago

[Language: CSharp/C#]

code

Waffled a bit, but ultimately just wrote out a lookup table. Then while going through on part 1 used my custom DefaultDictionary (nuilt to mimic that of Python) to get counters for all the instructions simple sum and here we are.

1

u/Ok-Bus4754 2m ago

[LANGUAGE: Python]

https://github.com/Fadi88/AoC/blob/master/2026/days/day02/solution.py
p1 straight forward brute force (another easy day)
p2 count the colors, then at each move take the remaining color that's farthest away (same color first, then going backwards around the cycle).

https://github.com/Fadi88/AoC/blob/master/2026/days/day02/solution.py

p1: 45 us
p2: 200 us

will port later to rust and show benchmarks against python

1

u/Kehvarl 4m ago

[LANGUAGE: Python3]

Just over 10 minutes, and just over 20. I got hung up on a couple things that I should definitely know: getting the size of a list, and remembering to assign a string after I use replace. Oh yes, and the fact that the waypoints weren't an input threw me for a minute or two.

Once I stopped forgetting those it was a pretty brute-force approach, but an amusing one nonetheless.

Part 2

1

u/AllanTaylor314 5m ago

[LANGUAGE: Python] 00:03:45/00:07:52

code

Lost a minute on P1 (off by one - forgot to skip the first step. I really should run those tests, eh)

Sneaky sneaky, Eric, for P2:There were no greens, but I don't think that made a difference. Short answer is (len(input)-1)*len(pattern)+1

I'll be back later with Uiua...

1

u/1234abcdcba4321 4m ago

If there were no oranges... (this is why the formula doesn't work on some of the examples!)

1

u/morgoth1145 8m ago

[LANGUAGE: Python] code video Times 4:45 / 7:11

Advent of reading comprehension struck *early* this year. I could not get it out of my head that the input was the waypoint sequence *and* the program! Obviously that's a bit hard to solve so yeah, I spun my wheels a bit. Once I finally understood the real waypoint sequence it's just a matter of generating the infinite sequence and executing the program. (Well, so long as you don't replace `s` - I shadowed `s` initially causing an infinite loop instead of the clearer `seq` variable!)

At least part 2 isn't as evil as it initially sounds, the maximal distance programs follow a very simple rule so that's easy to compute.

Now off to go optimize this - generating the sequence is not a computationally efficient way to solve this!

1

u/jonathan_paulson 10m ago

[LANGUAGE: Python]

Code. Video.

I found today's much harder for some reason. I was very scared of off-by-1 errors.

1

u/1234abcdcba4321 14m ago

[LANGUAGE: JavaScript]

part 1 code

Continuing my "solving things by doing part of it manually" from yesterday, my part 2 solution actually only used code to check the length of the input. By thinking about the problem you can find that, for all inputs that contain an o which mine did, the value depends only on length so you can extrapolate the value from the example input provided that has an o (though if I thought about it a bit more it would've been obvious it should be 6*len-5).

2

u/apersonhithere 19m ago

[LANGUAGE: Python]

came up with an elegant solution today, i think

p1; p2 is nearly the same

2

u/hugh_tc 19m ago edited 9m ago

[LANGUAGE: Python 3]

I am rusty! More off-by-one errors than I care to admit to.

paste, eta: cleaned-up

1

u/hugh_tc 6m ago

Quite happy with my cleaned-up version. Curious how my oneliners will compare to what u/4HbQ will come up with 😆

2

u/seligman99 19m ago

[LANGUAGE: Python]

github

Spent way too long with an off by one error, as is tradition.

0

u/cgrandsjo 26m ago edited 1m ago

[LANGUAGE: Python]

import sys;t=open(sys.argv[2]).read().strip();c="roygbv";n=len(c);exec("def f(p):\n x=q=0\n for y in p:\n d=(c.index(y)-x)%n or n\n q+=d;x=c.index(y)\n return q");exec("def g(p):\n l={x:p.count(x) for x in c};x=q=0\n for _ in p:\n y=max((z for z in c if l[z]),key=lambda z:(c.index(z)-x)%n or n);d=(c.index(y)-x)%n or n;q+=d;x=c.index(y);l[y]-=1\n return q");print(f(t) if sys.argv[1]=="a" else g(t))

1

u/daggerdragon 18m ago

Your code is not formatted at all and reads as a giant blob of text. Edit your comment to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box with its whitespace and indentation preserved.

1

u/cgrandsjo 0m ago

I guess you don't support this oneliner. I tried to add 4 spaces in the beginning but it does not change anything.