From 45bd0d906ecc1864c42402fd88288666ec4b46ab Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Thu, 12 Dec 2024 01:53:37 +0100 Subject: add day11 solution --- main.zig | 2 ++ src/day10.zig | 32 +++++++++++------------ src/day11.zig | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 src/day11.zig diff --git a/main.zig b/main.zig index 1a4b7d3..73ca11d 100644 --- a/main.zig +++ b/main.zig @@ -11,6 +11,7 @@ const day07 = @import("src/day07.zig"); const day08 = @import("src/day08.zig"); const day09 = @import("src/day09.zig"); const day10 = @import("src/day10.zig"); +const day11 = @import("src/day11.zig"); pub fn main() !void { if (std.os.argv.len != 3) { @@ -49,6 +50,7 @@ pub fn main() !void { 8 => day08.solve(args[2], buffer, allocator), 9 => day09.solve(args[2], buffer, allocator), 10 => day10.solve(args[2], buffer, allocator), + 11 => day11.solve(args[2], buffer, allocator), else => print("Day not yet implemented", .{}), }; } diff --git a/src/day10.zig b/src/day10.zig index 13bed52..a0eb88d 100644 --- a/src/day10.zig +++ b/src/day10.zig @@ -125,16 +125,19 @@ fn part2(buffer: []const u8, allocator: std.mem.Allocator) !void { @memset(grid[i], 0); } + var list = std.ArrayList(std.meta.Tuple(&.{ usize, usize })).init(allocator); + defer list.deinit(); + var row: usize = 0; while (lines.next()) |line| { if (line.len == 0) break; for (line, 0..) |item, col| { - if (item == '.') { - grid[row][col] = 15; - } else { - grid[row][col] = item - '0'; + grid[row][col] = item - '0'; + if (item == '0') { + try list.append(.{ row, col }); } } + row += 1; } @@ -143,19 +146,16 @@ fn part2(buffer: []const u8, allocator: std.mem.Allocator) !void { for (0..width) |i| { lookup[i] = try allocator.alloc(i32, width); } + defer allocator.free(lookup); - for (0..width) |r| { - for (0..width) |c| { - if (grid[r][c] == 0) { - out += try dfs2( - lookup, - grid, - @intCast(r), - @intCast(c), - -1, - ); - } - } + for (list.items) |zero| { + out += try dfs2( + lookup, + grid, + @intCast(zero[0]), + @intCast(zero[1]), + -1, + ); } for (0..width) |i| { diff --git a/src/day11.zig b/src/day11.zig new file mode 100644 index 0000000..af04ac9 --- /dev/null +++ b/src/day11.zig @@ -0,0 +1,84 @@ +const std = @import("std"); +const mecha = @import("mecha"); +const print = std.debug.print; + +pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void { + var lines = std.mem.splitScalar(u8, buffer, '\n'); + var split = std.mem.splitScalar(u8, lines.next().?, ' '); + + var numbers = std.ArrayList(usize).init(allocator); + defer numbers.deinit(); + while (split.next()) |n_string| { + try numbers.append(try std.fmt.parseInt(usize, n_string, 10)); + } + + if (std.mem.eql(u8, part, "1")) { + try part1(numbers, allocator); + } else { + try part2(numbers, allocator); + } +} + +fn digits(int: usize) usize { + var count: usize = 0; + var temp = int; + while (temp > 0) { + temp /= 10; + count += 1; + } + return count; +} + +fn recursive(number: usize, history: *std.AutoHashMap(std.meta.Tuple(&.{ usize, usize }), usize), depth: usize) !usize { + if (history.get(.{ number, depth })) |value| { + return value; + } + + if (depth == 0) { + return 1; + } + + var sum: usize = 0; + if (number == 0) { + sum = try recursive(1, history, depth - 1); + } else { + const n_digits = digits(number); + if (n_digits % 2 == 0) { + const p = std.math.pow(usize, 10, n_digits / 2); + const first = number / p; + const second = number % p; + + sum += try recursive(first, history, depth - 1); + sum += try recursive(second, history, depth - 1); + } else { + sum += try recursive(number * 2024, history, depth - 1); + } + } + + try history.put(.{ number, depth }, sum); + return sum; +} + +fn part1(numbers: std.ArrayList(usize), allocator: std.mem.Allocator) !void { + var history = std.AutoHashMap(std.meta.Tuple(&.{ usize, usize }), usize).init(allocator); + defer history.deinit(); + + var total: usize = 0; + for (numbers.items) |number| { + total += try recursive(number, &history, 25); + } + + print("{d}\n", .{total}); +} + +fn part2(numbers: std.ArrayList(usize), allocator: std.mem.Allocator) !void { + var history = std.AutoHashMap(std.meta.Tuple(&.{ usize, usize }), usize).init(allocator); + defer history.deinit(); + + var total: usize = 0; + for (numbers.items) |number| { + total += try recursive(number, &history, 75); + } + + print("{d}\n", .{total}); +} -- cgit v1.2.3