From 5229a41ca76f0f4ac15872a031d0ad782824c4ee Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 2 Dec 2023 15:09:27 +0000 Subject: day2 --- day2.zig | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'day2.zig') diff --git a/day2.zig b/day2.zig index d4f8f85..9ce71d9 100644 --- a/day2.zig +++ b/day2.zig @@ -1,8 +1,73 @@ const std = @import("std"); const Setup = @import("common.zig").Setup; +const Colour = enum { + red, green, blue , + fn parse(str: []const u8) !Colour { + const ei = @typeInfo(Colour).Enum; + inline for (ei.fields) |field| { + if (std.mem.eql(u8, field.name, str)) { + return @enumFromInt(field.value); + } + } else { + return error.InvalidColour; + } + } +}; + pub fn main() !void { var setup = try Setup.get(); defer setup.deinit(); - + var sum: u32 = 0; + var lines = std.mem.splitScalar(u8, setup.input, '\n'); + const maxred: u32 = 12; + const maxgreen: u32 = 13; + const maxblue: u32 = 14; + while (lines.next()) |line| { + var spl = std.mem.split(u8, line, ": "); + const game_no_str = spl.first(); + const game_no = try std.fmt.parseInt(u32, game_no_str[5..game_no_str.len], 10); // "Game " prefix skipped + const game_str = spl.next() orelse return error.NoGame; + var turns = std.mem.split(u8, game_str, "; "); + var gamemaxred: u32 = 0; + var gamemaxgreen: u32 = 0; + var gamemaxblue: u32 = 0; + + while (turns.next()) |turn| { + var cols = std.mem.split(u8, turn, ", "); + while (cols.next()) |col| { + var spl2 = std.mem.split(u8, col, " "); + const num = try std.fmt.parseInt(u32, spl2.first(), 10); + const col_str = spl2.next() orelse return error.NoColour; + const c = try Colour.parse(col_str); + switch(c) { + .blue => { + gamemaxblue = @max(num, gamemaxblue); + }, + .green => { + gamemaxgreen = @max(num, gamemaxgreen); + }, + .red => { + gamemaxred = @max(num, gamemaxred); + }, + } + } + } + switch (setup.part) { + .pt1 => { + if (gamemaxblue <= maxblue and gamemaxgreen <= maxgreen and gamemaxred <= maxred) { + sum += game_no; + } else { + //std.log.warn("game was impossible {s}", .{line}); + } + }, + .pt2 => { + const power = gamemaxblue * gamemaxgreen * gamemaxred; + sum += power; + }, + } + + //turns.next() + } + try std.fmt.format(std.io.getStdOut().writer(), "answer: {}\n", .{sum}); } \ No newline at end of file -- cgit v1.2.3-ZIG