Watch
1
0
Fork
You've already forked advent-of-code
0

2024 Day 10 Part 2

This commit is contained in:
Jeremy Kaplan 2024-12-09 22:14:17 -08:00
commit cc4ad29230

View file

@ -22,11 +22,16 @@ pub fn main() !void {
var bw = std.io.bufferedWriter(stdout_file); var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer(); const stdout = bw.writer();
try stdout.print("{d}\n", .{try part1(allocator, grid)}); const part1, const part2 = try findTrails(allocator, grid);
try stdout.print("{d}\n", .{part1});
try bw.flush();
try stdout.print("{d}\n", .{part2});
try bw.flush(); try bw.flush();
} }
fn part1(allocator: Allocator, grid: Grid) !u64 { fn findTrails(allocator: Allocator, grid: Grid) !struct { usize, usize } {
var stack = ArrayList(ArrayList(Pos)).init(allocator); var stack = ArrayList(ArrayList(Pos)).init(allocator);
defer stack.deinit(); defer stack.deinit();
defer for (stack.items) |*trail| trail.deinit(); defer for (stack.items) |*trail| trail.deinit();
@ -45,6 +50,8 @@ fn part1(allocator: Allocator, grid: Grid) !u64 {
} }
} }
var total: usize = 0;
var trailheads = std.AutoHashMap(Pos, aoc.AutoHashSet(Pos)).init(allocator); var trailheads = std.AutoHashMap(Pos, aoc.AutoHashSet(Pos)).init(allocator);
defer trailheads.deinit(); defer trailheads.deinit();
defer { defer {
@ -58,6 +65,8 @@ fn part1(allocator: Allocator, grid: Grid) !u64 {
const pos = trail.getLast(); const pos = trail.getLast();
const height = grid.get(pos).?; const height = grid.get(pos).?;
if (height == '9') { if (height == '9') {
total += 1;
const start = trail.items[0]; const start = trail.items[0];
const entry = try trailheads.getOrPut(start); const entry = try trailheads.getOrPut(start);
if (!entry.found_existing) { if (!entry.found_existing) {
@ -79,12 +88,12 @@ fn part1(allocator: Allocator, grid: Grid) !u64 {
} }
} }
var sum: u64 = 0; var uniq: u64 = 0;
var it = trailheads.valueIterator(); var it = trailheads.valueIterator();
while (it.next()) |set| { while (it.next()) |set| {
sum += set.count(); uniq += set.count();
} }
return sum; return .{ uniq, total };
} }
const Pos = struct { const Pos = struct {