aoc2023

Advent of Code 2023
Log | Files | Refs | README

day2.zig (2631B)


      1 const std = @import("std");
      2 const Setup = @import("common.zig").Setup;
      3 
      4 const Colour = enum { 
      5     red, green, blue ,
      6     fn parse(str: []const u8) !Colour {
      7         const ei = @typeInfo(Colour).Enum;
      8         inline for (ei.fields) |field| {
      9             if (std.mem.eql(u8, field.name, str)) {
     10                 return @enumFromInt(field.value);
     11             }
     12         } else {
     13             return error.InvalidColour;
     14         }
     15     }
     16 };
     17 
     18 pub fn main() !void {
     19     var gpa = std.heap.GeneralPurposeAllocator(.{}){};
     20     defer _ = gpa.deinit();
     21     var setup = try Setup.get(gpa.allocator());
     22     defer setup.deinit();
     23     var sum: u32 = 0;
     24     var lines = std.mem.splitScalar(u8, setup.input, '\n');
     25     const maxred: u32 = 12;
     26     const maxgreen: u32 = 13;
     27     const maxblue: u32 = 14;
     28     while (lines.next()) |line| {
     29         var spl = std.mem.split(u8, line, ": ");
     30         const game_no_str = spl.first();
     31         const game_no = try std.fmt.parseInt(u32, game_no_str[5..game_no_str.len], 10); // "Game " prefix skipped
     32         const game_str = spl.next() orelse return error.NoGame;
     33         var turns = std.mem.split(u8, game_str, "; ");
     34         var gamemaxred: u32 = 0;
     35         var gamemaxgreen: u32 = 0;
     36         var gamemaxblue: u32 = 0;
     37 
     38         while (turns.next()) |turn| {
     39             var cols = std.mem.split(u8, turn, ", ");
     40             while (cols.next()) |col| {
     41                 var spl2 = std.mem.split(u8, col, " ");
     42                 const num = try std.fmt.parseInt(u32, spl2.first(), 10);
     43                 const col_str = spl2.next() orelse return error.NoColour;
     44                 const c = try Colour.parse(col_str);
     45                 switch(c) {
     46                     .blue => {
     47                         gamemaxblue = @max(num, gamemaxblue);
     48                     },
     49                     .green => {
     50                         gamemaxgreen = @max(num, gamemaxgreen);
     51                     },
     52                     .red => {
     53                         gamemaxred = @max(num, gamemaxred);
     54                     },
     55                 }
     56             }
     57         }
     58         switch (setup.part) {
     59             .pt1 => {
     60                 if (gamemaxblue <= maxblue and gamemaxgreen <= maxgreen and gamemaxred <= maxred) {
     61                     sum += game_no;
     62                 } else {
     63                     //std.log.warn("game was impossible {s}", .{line});
     64                 }
     65             },
     66             .pt2 => {
     67                 const power = gamemaxblue * gamemaxgreen * gamemaxred;
     68                 sum += power;
     69             },
     70         }
     71         
     72         //turns.next()
     73     }
     74     try std.fmt.format(std.io.getStdOut().writer(), "answer: {}\n", .{sum});
     75 }