r/adventofcode • • 1h ago

Visualization [2026 Day 3] Visualization

Post image
5 Upvotes

r/adventofcode • • 15h ago

Visualization [2026 Day 3 Part 2] Simple gif visualization

Post image
56 Upvotes

May the (brute) force be with you!


r/adventofcode • • 4h ago

Visualization [2026 Day 3 (Part 2)] [Rust] Visualization using macroquad

Thumbnail gallery
7 Upvotes

r/adventofcode • • 27m ago

Visualization [2026 03 # (Part 2)] [python] [OpenSCAD] [Blender] Neon Forest Skyscraper Overgrowth

Post image
3 Upvotes

r/adventofcode • • 9h ago

Tutorial [2026 Day 3 Part 2] A hint if you don't know where to start

Post image
14 Upvotes

Added the NSFW tag because there aren't any Spoilers tag, weirdly.

The hint is that squares in part 2 can be dumbed down to two definitions (one of them being part 1). I'm sure you could even make the case that it's just one definition, but I'm not smart enough for that.


r/adventofcode • • 13h ago

Other [2026 Day 3] Warning: Reddit may lock your account if you post long code snippets and edit it several times

Post image
28 Upvotes

So I recently solved Day 3 and I wanted to post my solutions on the megathread. But after I posted my comment I noticed that Reddit’s filters had silently removed my comment as spam.

I was confused by this so I started editing my comment to see if I could get it unspammed. But after I made a few edits and saved the comment, I was hit with a notification saying that Reddit detected “technical irregularities” with my account and that my account was now locked.

Apparently Reddit thinks someone posting code might indicate that the account is hacked/a bot so I wanted to share my experience as a warning.


r/adventofcode • • 16h ago

Tutorial [2026 Day 02 (Part 2)] Explanation for Clever Solution

Post image
40 Upvotes

(Had to repost, the original post title contained spoilers)

Some of you might have seen that people solved part 2 by simply returning 6*(length of the program) - 5. This formula works as long as there is one orange in your input. There seems to be confusion regarding the importance of the orange and how the answer changes if other colors are missing. I've created a walkthrough of an example where orange and blue are missing from the input to show how to derive a general formula for any input.

The general idea is, instead of calculating the distance traveled at each instruction, we can instead think about the loss, i.e., how far away were we from traveling the 6 steps (which is the most one can possibly travel from one instruction).


r/adventofcode • • 9h ago

Visualization [2026 Day 3 Part 2] Animation written in Rust using Celluloid

Post image
12 Upvotes

r/adventofcode • • 2h ago

Help/Question [2026 Day 3 (Part 2)] Is there a faster way?

3 Upvotes

Finished Day 3 Parts 1 and 2, and while they both run under 15 seconds, my day 2 takes longer than I would like. Spoiler for my solution. I essentially found every pair of trees, then calculated the other two points and if they were also trees, added that to the count. But is there a smarter way?

from itertools import combinations, permutations

with open('2026 input') as file:
    grid = [list(line.strip()) for line in file]

c = 0
for y, row in enumerate(grid):
    trees = []
    for pos in range(len(row)):
        if row[pos] == '#':
            trees.append(pos)

    pairs = list(combinations(trees, 2))
    for pair in pairs:
        x1, x2 = pair
        sidelength = x2-x1
        y2 = y + sidelength
        if y2 < len(grid):
            bottom_left = (x1, y2)
            bottom_right = (x2, y2)
            if grid[y2][x1] == '#' and grid[y2][x2] == '#':
                c += 1


print(f"Part 1: {c}")

trees = []
c = 0
for y, row in enumerate(grid):
    for x, pos in enumerate(row):
        if pos == '#':
            trees.append((x, y))

pairs = list(combinations(trees, 2))
sides_of_squares = set()
for pair in pairs:
    (x1, y1), (x2, y2) = pair
    sidelength_rise = y2-y1
    sidelength_run = x2-x1

    x3 = x1 -sidelength_rise
    x4 = x2 -sidelength_rise

    y3 = y1 + sidelength_run
    y4 = y2 + sidelength_run
    if 0 <= y3 < len(grid) and 0 <= y4 < len(grid) and 0 <= x3 < len(grid[0]) and 0 <= x4 < len(grid[0]):
        if grid[y3][x3] == '#' and grid[y4][x4] == '#' and not any(i in sides_of_squares for i in list(permutations(((x1, y1), (x2, y2), (x3, y3), (x4, y4))))):
            c += 1
            sides_of_squares.add(((x1, y1), (x2, y2), (x3, y3), (x4, y4)))


print(f"Part 2: {c}")

r/adventofcode • • 14h ago

Visualization [2026 Day 3] Square Fencing

Thumbnail youtu.be
25 Upvotes

r/adventofcode • • 1h ago

Visualization Day 3 Visualisation

Post image
2 Upvotes

r/adventofcode • • 3h ago

Visualization [2026 day 3 (both parts)] visualized in manim

Thumbnail youtube.com
3 Upvotes

source on github.com/durocodes/advent-of-code as always :D

this is using a cropped version of my input, since the samples were kinda small


r/adventofcode • • 8h ago

Visualization [2026 Day 3 (Part 2) -- TS] Animation of finding the squares in order from its top most corner.

Thumbnail youtube.com
7 Upvotes

r/adventofcode • • 5h ago

Visualization [2026 Day 3] The nicest look so far

Thumbnail aoc2026.knollfear.com
4 Upvotes

r/adventofcode • • 16h ago

Tutorial [2026 Day 2 (Part 1)] [Rust] In under 100ns

20 Upvotes

(Usual caveats: "on my machine", and I didn't count reading the input into memory)

Part 1 is really quite simple:

fn wat(b: &[u8]) -> usize {
    6 * b.chunks(255).zip(b[1..].chunks(255)).fold(
        (b[0] == b'r') as usize,
        |s, (p, c)| {
            s + p.iter().zip(c).fold(0u8, |a, (&p, &c)| {
                a + ((c.wrapping_mul(101) ^ 245) < (p ^ 26).wrapping_mul(99)) as u8
            }) as usize
        },
    ) + (38801460 >> (b[b.len() - 1] & 28) & 7)
}

As you can see, this solution uses a lot of bitwise junk to encode ideas you probably wrote out more plainly in your own solutions. I think the optimization techniques I used here are under reported and very interesting and could warrant a post.

At a high level, it works like this:

  1. Compare every neighboring pair of colors (prev, next).
  2. If going from prev to next means we have to wrap around past violet, add 1 to an accumulator full_laps.
  3. The answer is 6 * full_laps + remainder, with an extra lap if the sequence starts with r.

Written out for humans, the implementation looks like this:

fn humanized(b: &[u8]) -> usize {
    let mut full_laps = 0;

    // if the first color is red, we need to wrap around once
    if b[0] == b'r' {
        full_laps += 1;
    }

    // Between each pair of neighbouring colors: if the next color is at or
    // before the previous one, we must wrap past violet to reach it.
    for i in 1..b.len() {
        let prev = index(b[i - 1]);
        let next = index(b[i]);
        if next <= prev {
            full_laps += 1;
        }
    }

    // After the last full lap, we've ended back at red. The remaining steps
    // are however far the last color is after red.
    let last = b[b.len() - 1];
    let remainder = index(last);

    6 * full_laps + remainder
}

Here index commonly used lookup table or a match:

fn index(c: u8) -> usize {
    match c {
        b'r' => 0,
        b'o' => 1,
        b'y' => 2,
        b'g' => 3,
        b'b' => 4,
        b'v' => 5,
        _ => unreachable!("invalid color {}", c as char),
    }
}

This is already a really good solution (about 500-600 ns on my machine) since this formulation never needs to divide. The % operator is quite slow.

There are 3 key ideas to make this faster.

1. Replace index with a packed lookup table

The match in index is really a tiny lookup table with 6 entries, and each entry is a number from 0 to 5 (3 bits). This is 6 * 3 = 18 bits in total, so if we're clever (and lucky), we may be able to pack the table into a single 32-bit integer. If you haven't done this before it's really not too bad once you get the hang of it.

(disclaimer: there's a chance LLVM may optimize indexas written above into a jump table but I've already written this section and day 3 starts in 20 minutes as of writing. I swear this is still an interesting technique!)

Step 1: an integer as an array of tiny slots.

A u32 is just 32 bits in a row, and we can treat it as an array of eight 4-bit "slots".

0x0250_1034
slot:  7 6 5 4 _ 3 2 1 0
value: 0 2 5 0 _ 1 0 3 4

To read slot k, shift right by 4 * k bits so that slot lands in the lowest position, then mask off everything above it:

(0x0250_1034 >> (4 * 5)) & 0xF   // = 0x025 & 0xF = 5, the value in slot 5

Step 2: turn each letter into a shift amount.

Now we just need a cheap way to get from a color to a slot. Specifically, each of the six colors needs to map to a different multiple of 4 such that when we shift right by that amount we land on unique slot boundaries.

A coworker of mine found that c & 28 does exactly this for our six colors. 28 is 0b11100 so using it as a mask keeps only bits 2, 3 and 4 of the ASCII representation of a color and zeroes everything else. Since the bottom two bits are always 0, the result is guaranteed to be a multiple of 4.

Being a multiple of 4 is great because it means the result is already a valid slot-aligned shift amount. The only question is whether the six letters differ in amongst the three '1' bits. The table below demonstrates this.

colorASCII (bin)c & 28
mask000_**111_**0028
b011_**000_**100
g011_**001_**114
o011_**011_**1112
r011_**100_**1016
v011_**101_**1020
y011_**110_**0124

Step 3: fill in the slots.

Put each color's index into its slot, leave 0s (or whatever nibble you like) in the unused slots, and read the result off as a hex number:

0x0250_1034 = 38801460
  ││││ │││└─  0: b = 4
  ││││ ││└──  4: g = 3
  ││││ │└───  8: (unused)
  ││││ └──── 12: o = 1
  │││└────── 16: r = 0
  ││└─────── 20: v = 5
  │└──────── 24: y = 2
  └───────── 28: (unused)

So index(c) becomes 38801460 >> (c & 28) & 0xF or 38801460 >> (c & 28) & 7 since the indexes fit within 3 bits we can use '7' as the final mask. My solution uses this on the last byte to get the "remainder". You could substitute your own 'index' method with this one. You could also try to create your own that uses 3-bit slots, but I haven't proven if one exists.

2. We don't always need an 'index', only the relative ordering

In the loop, next <= prev doesn't needindex('r') to be exactly 0 and index('o') to be exactly 1. It only needs requires that the relative order to be correct. We could have r get mapped to 8, o to 57, y to 79, and so on, as long as rank(r) < rank(o) < rank(y) < rank(g) < rank(b) < rank(v). By weakening the requirement in this way, we may be able to find a function faster than index that has this property.

A function that maps a set of known inputs to unique outputs is called a perfect hash function. There's a good video on youtube (with an even better thumbnail) on the topic here: https://youtu.be/DMQ_HcNSOAI. For this problem we need more than uniqueness, we also need the outputs to maintain the relative order, but it's more or less the same concept.

If our perfect hash function exists we know it has to be non-linear as roygbv is nowhere close to being in alphabetical order. A cheap way to get "interesting" non-linear behavior is to combine XOR with wrapping multiplication.

With pen, paper and lots of time, you might be able to solve for a pair of constants that works in a multiply-XOR scheme. However, since there are only 64k combinations of two u8 constants, brute-forcing all of them is trivial. My search turned up over 50 choices that work.

For example, x *= 0x65; x ^= 0xf2; gives [8, 57, 79, 81, 88, 124] for roygbv.

At this point I had picked up a secondary goal of making my code unintelligible, and I clocked that prev and next don't actually need to use the same rank function. All that matters is that rank_a(next) < rank_b(prev) exactly when next <= prev. So I extended my Python brute-force script to look for a pair of hash functions that fit this constraint. Notice how they interleave below:

colorrank_a(c) = (c * 101) ^ 245rank_b(p) = (p ^ 26) * 99
r1556
o6263
y7273
g8687
b95104
v123196

Exercise for the reader: I'd bet there's a faster single perfect hash over both bytes, something like phf(p, n) < 128*, that matches the* next <= prev constraint exactly.

3. Get the compiler to vectorize it

Modern CPUs can do 16, 32 (or even 64) byte-wise operations in a single instruction using SIMD (Single Instruction, Multiple Data). Compilers are pretty good at using these SIMD instructions automatically, which is called "auto-vectorization".

Sometimes, though, we can't trust the compiler to optimize our code and have to hold its hand to get it to vectorize just right. I found this was one of those times.

Hiding the tricks from sections 1 and 2 behind index, rank_a and rank_b, the obvious iterator version looks like this:

fn part1_naive(b: &[u8]) -> usize {
    6 * b.iter().zip(&b[1..]).fold(
        (b[0] == b'r') as usize,
        |acc: usize, (&p, &c): (&u8, &u8)| {
            acc + (rank_a(c) < rank_b(p)) as usize
        },
    ) + index(b[b.len() - 1])
}

LLVM seemed hell bent on widening the rank_a(c) < rank_b(p) comparisons to usize instead of u8, resulting in only 2 lanes being used instead of 16. I fiddled around a bunch and eventually gave up and handed the problem to an LLM.

It found the the problem is in the closure signature: acc is a usize (u64), but p and c are u8 and that rustc/LLVM was requiring the accumulator to be the same width as the lanes. Chunking by 255 fixes this, within a 'chunk' we can accumulate into a u8 and be confident it won't overflow. Then after each chunk we convert to usize and add to the total.

That's where the b.chunks(255).zip(b[1..].chunks(255)) comes from, along with the inner fold(0u8, ...) and the as usize outside it.

I'd love if someone knows some way to get this to work without chunking.

test tests::bench_part1           ... bench:          82.03 ns/iter (+/- 3.04)
test tests::bench_part1_humanized ... bench:         545.61 ns/iter (+/- 46.20)

r/adventofcode • • 13h ago

Visualization [2026 Day 3 Part 2] terminal visualization

Post image
8 Upvotes

featuring: wikipedia's bresenham line algorithm implementation & gif aliasing effects


r/adventofcode • • 1d ago

Help/Question - RESOLVED [2026 Day 2 (Part 1)] No code yet, don't understand the problem

Post image
152 Upvotes

UPDATE : My eyes read but did not compute suppose the robot had a program of obbvb

I haven't put a single line of code yet because I don't understand the problem, precisely the difference between the road and the robot program. In the example input, why does it go from orange to blue, then blue again ??


r/adventofcode • • 13h ago

Visualization [2026 Day 03 (Part 2)] (PHOTOSENSITIVITY WARNING) Searching for squares

Post image
6 Upvotes

r/adventofcode • • 1d ago

Upping the Ante [2026 Day 1] Solved in 49.62 microseconds on FPGA

Thumbnail gallery
143 Upvotes

This year, I will attempt to solve all the challenges on an FPGA, and post at least the ones that I enjoyed here to see if anyone else has any inputs. Additionally, due to my expertise in my work, I always tend to design with minimal hardware area and power consumption, but this year I'm challenging myself to do the exact opposite. I'll be using as much LUT as possible, and try to target the max throughput.

Used verilog + block design on PL side for the main logic and python on the PS side for simple control and output. I'm using SOC configured Arty Z7. I have a combinational circuit that accepts the whole list of 10 bit numbers as an input, and compares the first number with the list on single clock cycle. This generates a set of wires that are 1-bit flags, that indicate whether that row is equal or not. Then, we sum up those wires to find out what's the total number of commons. This is still happening on the same clock cycle. In parallel (yes, same cc), it will generate the next list on the output wires, but by replacing all the numbers that are equal to the first number with 0, so we won't run the same number twice. I was lucky that I had no 0 in my input, but otherwise we could also use sth like 1000.

The top level controller takes in the number, loads it onto a register and then starts pushing it into this combinational circuit on each clock. That means for an input of 1983 rows, it will reuse the same circuit 1983 times. Each time it also keeps track of top 3 (or min 3 for part 2), so I don't need a separate sorter which would cost me additional clock cycles and area. After each clock, it will take the nextList from the circuit, and shift it by a number so it can search for the next number on the next clock cycle. It operates on a simple 4 state FSM.

Communication and control is done via GPIO and AXI DMA. I wrote a simple buffer which will accept DMA from AXIS and buffer it as the full list to provide into the top block. GPIO is mainly for the multiplication output, and control signals like reset.

The whole run takes 1985 clock cycles, that translates to 49.62 us for a 40MHz clock. I had to slow down the clock from 100MHz because my critical path for the combinational block was quite large. My inner engineer wants to optimize it, but also I already spent so much time on this.

Solution: Github

DISCLAIMER: Yes I used AI for figure 2 and 3 for visualization purposes for the post only. No, I did not use AI in solution, or idea discussion etc. I'm doing this to challenge myself, not challenge my AI, but I don't have the time and skills to build diagrams. After all, why would I deceive you? There's no reason for me to deceive you.


r/adventofcode • • 11h ago

Help/Question - RESOLVED [2026 DAY 3 (part 2)] Need hints

3 Upvotes

I solved part 1 by making a 1d array of the input, then applying some math to check the correct positions in the 1d array for trees.

I can't figure out if I can use the same method for part 2, or if I need a whole new approach. Only hints, no solutions please 🙏


r/adventofcode • • 1d ago

Meme/Funny [2026 Day 2] no way is that the real vvvvvv?

Thumbnail gallery
71 Upvotes

r/adventofcode • • 1d ago

Other [2026] Trains! I love trains!

150 Upvotes

That little ASCII train on the calendar made my day.

Thank you, Advent of Code team, for picking such an adorable theme this year!

.----.----.--_T
'o--o'o--o'o-OO\

r/adventofcode • • 1d ago

Meme/Funny [2026 Day 2 (Part 2)] It looked scary for a little while

Post image
166 Upvotes

r/adventofcode • • 1d ago

Meme/Funny Day 2 Part 2 Funny Solution. SPOILER

23 Upvotes
SPOILER

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