30

Day 1: Dusty Shed

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] obdurant@lemmy.zip 4 points 1 day ago* (last edited 1 day ago)

#Zig

const std = @import("std");
const input = @embedFile("./input");

pub fn main() !void {
    var total: u64 = 1;
    const ally = std.heap.page_allocator;
    var id_iter = std.mem.splitScalar(u8, input, '\n');
    var id_hash = std.AutoHashMap(u16, u16).init(ally);
    defer id_hash.deinit();
    while (id_iter.next()) |line| {
        const trimmed_line = std.mem.trim(u8, line, " \r");
        if (trimmed_line.len == 0) continue;
        const id = try std.fmt.parseInt(u16, trimmed_line, 10);
        const entry = try id_hash.getOrPut(id);
        if (entry.found_existing) {
            entry.value_ptr.* += 1;
        } else {
            entry.value_ptr.* = 1;
        }
    }

    for (0..3) |_| {
        var id: u16 = undefined;
        var highest: u16 = 0;
        var entry_iterator = id_hash.iterator();
        while (entry_iterator.next()) |pair| {
            if (pair.value_ptr.* > highest) {
                highest = pair.value_ptr.*;
                id = pair.key_ptr.*;
            }
        }
        total *= id;
        const trimmed = id_hash.remove(id);
        if (trimmed == false) std.debug.panic("halp", .{});
    }
    std.debug.print("\n{d}\n", .{total});
}
[-] obdurant@lemmy.zip 3 points 1 day ago

i spent quite some time confusedly poking at my parsing code before i realized i was splitting on '\n' and there were still hidden '\r' chars in the lines since I was on my work laptop.

I haven't done much with hash maps or sorting in Zig yet. After I sleep I might find some time tomorrow see what a better approach than iterating over the entries three times might look like, but I just wanted to squeeze something out before bed.

this post was submitted on 01 Dec 2026
30 points (100.0% liked)

Advent Of Code

1262 readers
48 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS