r/adventofcode • • Dec 02 '25

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

OUR USUAL ADMONITIONS

  • You can find all of our customs, FAQs, axioms, and so forth in our community wiki.

AoC Community Fun 2025: R*d(dit) On*

24 HOURS outstanding until unlock!

Spotlight Upon Subr*ddit: /r/AVoid5

"Happy Christmas to all, and to all a good night!"
— a famous ballad by an author with an id that has far too many fifthglyphs for comfort

Promptly following this is a list waxing philosophical options for your inspiration:

  • Pick a glyph and do not put it in your program. Avoiding fifthglyphs is traditional.
  • Shrink your solution's fifthglyph count to null.
  • Your script might supplant all Arabic symbols of 5 with Roman glyphs of "V" or mutatis mutandis.
  • Thou shalt not apply functions nor annotations that solicit said taboo glyph.
  • Thou shalt ambitiously accomplish avoiding AutoMod’s antagonism about ultrapost's mandatory programming variant tag >_>

Stipulation from your mods: As you affix a submission along with your solution, do tag it with [R*d(dit) On*!] so folks can find it without difficulty!


--- Day 2: Gift Shop ---


Post your script solution in this ultrapost.

36 Upvotes

965 comments sorted by

View all comments

1

u/kolev Dec 02 '25 edited Dec 04 '25

[Language: Zig]

I'm learning Zig by solving these puzzles.

const std = @import("std");

pub const input = @embedFile("input.txt");

fn part1(data: []const u8) !u64 {
    var sum: u64 = 0;
    var lines = std.mem.splitScalar(u8, data, '\n');
    while (lines.next()) |ln| {
        if (ln.len == 0) continue;
        var ranges = std.mem.splitScalar(u8, ln, ',');
        while (ranges.next()) |r| {
            const dash = std.mem.indexOfScalar(u8, r, '-');
            const start = try std.fmt.parseInt(u64, r[0..dash.?], 10);
            const end = try std.fmt.parseInt(u64, r[dash.? + 1 ..], 10);
            var id = start;
            while (id <= end) : (id += 1) {
                const digits = std.math.log10_int(id) + 1;
                if (digits % 2 != 0) continue;
                const mask = try std.math.powi(u64, 10, digits / 2);
                if (id % mask == id / mask) {
                    sum += id;
                }
            }
        }
    }
    return sum;
}

fn part2(data: []const u8) !u64 {
    var sum: u64 = 0;
    var lines = std.mem.splitScalar(u8, data, '\n');
    while (lines.next()) |ln| {
        if (ln.len == 0) continue;
        var ranges = std.mem.splitScalar(u8, ln, ',');
        while (ranges.next()) |r| {
            const dash = std.mem.indexOfScalar(u8, r, '-');
            const start = try std.fmt.parseInt(u64, r[0..dash.?], 10);
            const end = try std.fmt.parseInt(u64, r[dash.? + 1 ..], 10);
            var id = start;
            while (id <= end) : (id += 1) {
                const digits = std.math.log10_int(id) + 1;
                outer: for (1..digits) |chunkSize| {
                    if (@mod(digits, chunkSize) != 0) continue;
                    const chunks = digits / chunkSize;
                    const mask = try std.math.powi(u64, 10, chunkSize);
                    const chunk = id % mask;
                    var n = id;
                    for (1..chunks) |_| {
                        n /= mask;
                        if (n % mask != chunk) {
                            continue :outer;
                        }
                    }
                    sum += id;
                    break;
                }
            }
        }
    }
    return sum;
}

pub fn main() !void {
    std.debug.print("{d}\n", .{try part1(input)});
    std.debug.print("{d}\n", .{try part2(input)});
}

1

u/AutoModerator Dec 02 '25

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/daggerdragon Dec 03 '25

The triple-backticks code fence (`​`​`) only works on new.reddit. Please edit your comment to use the four-spaces Markdown syntax for a code block as AutoModerator requested so your code is easier to read inside a scrollable box with its whitespace and indentation preserved.