aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-12-02 15:09:27 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-12-02 15:09:27 +0000
commit5229a41ca76f0f4ac15872a031d0ad782824c4ee (patch)
treec27444cddd5605ff6bdddfe7f71fe2de43c6cec5
parent9a5201154c28923c402d93e3ddee4858c10d8017 (diff)
downloadaoc2023-5229a41ca76f0f4ac15872a031d0ad782824c4ee.tar.gz
aoc2023-5229a41ca76f0f4ac15872a031d0ad782824c4ee.tar.bz2
aoc2023-5229a41ca76f0f4ac15872a031d0ad782824c4ee.tar.xz
aoc2023-5229a41ca76f0f4ac15872a031d0ad782824c4ee.zip
day2
-rw-r--r--day2.in100
-rw-r--r--day2.zig67
-rw-r--r--day2_sample.in5
3 files changed, 171 insertions, 1 deletions
diff --git a/day2.in b/day2.in
new file mode 100644
index 0000000..e3b74cd
--- /dev/null
+++ b/day2.in
@@ -0,0 +1,100 @@
+Game 1: 2 blue, 3 red; 3 green, 3 blue, 6 red; 4 blue, 6 red; 2 green, 2 blue, 9 red; 2 red, 4 blue
+Game 2: 4 red, 1 green; 3 red; 13 green, 5 red, 3 blue; 3 green, 2 red; 3 blue, 5 red, 3 green; 2 red, 3 blue, 12 green
+Game 3: 4 red, 1 green, 1 blue; 1 red, 1 blue; 6 red, 1 green; 6 red, 3 blue, 1 green; 4 red
+Game 4: 4 blue, 12 red, 4 green; 6 green, 3 blue, 19 red; 3 blue, 2 red, 2 green
+Game 5: 1 red, 5 blue, 16 green; 1 red, 6 green, 3 blue; 2 red, 12 blue; 17 blue, 3 green; 7 green, 2 red, 6 blue
+Game 6: 3 green, 1 blue, 5 red; 5 green, 5 red; 2 green, 2 blue, 3 red; 5 green, 2 red; 3 green, 6 red, 3 blue; 5 green, 4 red
+Game 7: 15 blue, 1 red, 6 green; 4 blue, 7 green, 2 red; 14 blue, 5 green, 2 red
+Game 8: 6 blue, 3 green, 10 red; 2 blue, 1 green, 5 red; 6 blue, 3 green, 12 red; 11 red, 1 green, 1 blue; 5 blue, 14 red, 3 green; 3 red
+Game 9: 15 red, 3 blue; 1 blue, 16 red; 1 red, 3 blue; 1 blue, 1 green, 9 red
+Game 10: 1 red; 1 blue, 7 green; 1 green, 5 blue; 3 blue, 3 green; 1 green
+Game 11: 19 blue, 13 green; 19 blue, 2 green; 10 blue, 3 red, 12 green; 11 blue, 1 red, 6 green
+Game 12: 7 green, 5 blue; 6 green, 3 red, 6 blue; 2 red, 5 blue, 15 green; 2 red, 1 blue, 1 green; 4 red, 4 green, 2 blue; 3 blue, 6 green
+Game 13: 9 red, 2 blue, 2 green; 1 blue, 2 red, 15 green; 9 green, 2 blue, 9 red; 5 blue, 8 green, 5 red; 2 blue, 11 green, 5 red
+Game 14: 9 blue, 1 red; 10 blue, 4 green, 3 red; 2 red, 6 blue; 4 green, 2 blue, 1 red; 5 green, 2 red, 11 blue; 12 blue, 2 red, 1 green
+Game 15: 9 blue, 7 green, 12 red; 9 red, 17 green, 8 blue; 6 red, 4 blue, 4 green; 5 red, 17 green
+Game 16: 5 green, 4 red; 3 blue, 3 red, 14 green; 6 red, 5 blue, 12 green
+Game 17: 8 blue, 5 green, 2 red; 6 red, 6 blue; 9 red; 5 blue, 2 green, 8 red; 13 red, 4 blue, 4 green; 9 blue, 3 green, 5 red
+Game 18: 8 green, 1 red, 2 blue; 4 green, 4 red, 1 blue; 6 blue, 2 red
+Game 19: 3 green, 9 blue; 4 blue, 10 red; 6 red, 3 green, 3 blue; 6 red, 4 green, 9 blue
+Game 20: 11 green, 3 blue; 6 green; 3 green, 6 blue; 1 red, 5 green; 6 blue, 7 green
+Game 21: 1 green, 1 blue, 12 red; 6 red, 2 blue; 5 green, 4 red, 2 blue; 11 red, 8 green, 1 blue
+Game 22: 10 red; 1 red, 13 green, 9 blue; 6 blue, 12 red, 12 green; 10 red, 8 blue, 11 green; 2 green, 1 red, 3 blue; 7 red, 1 blue, 8 green
+Game 23: 11 red, 15 blue; 10 blue, 16 red, 1 green; 14 blue, 5 red; 1 green, 9 red, 9 blue; 1 red, 7 blue, 3 green; 6 red, 2 green, 3 blue
+Game 24: 6 blue, 11 red; 16 green, 2 red, 1 blue; 8 red, 7 blue; 14 blue, 9 green, 9 red; 13 green, 4 red, 8 blue; 2 red, 7 blue, 1 green
+Game 25: 2 green, 12 blue, 1 red; 10 blue, 5 red, 5 green; 2 blue, 9 red, 3 green; 5 blue, 4 red, 2 green
+Game 26: 7 blue, 6 red, 1 green; 2 blue, 3 green, 12 red; 2 blue, 6 red, 5 green
+Game 27: 2 green, 3 red; 4 green; 2 red, 1 blue, 1 green; 2 red, 1 green, 2 blue
+Game 28: 11 blue, 1 red, 5 green; 2 blue, 2 red, 4 green; 10 blue, 4 red, 1 green
+Game 29: 6 blue, 17 red, 1 green; 8 blue, 4 red; 14 blue, 1 red, 3 green
+Game 30: 2 blue, 4 green; 7 green, 1 blue, 1 red; 1 blue, 8 green
+Game 31: 15 blue, 9 green, 2 red; 5 green, 4 blue, 1 red; 1 green, 15 red, 7 blue; 5 red, 2 blue
+Game 32: 1 blue, 5 red, 3 green; 3 green, 8 red, 1 blue; 5 green, 1 red; 4 green, 3 blue, 15 red; 2 green, 1 blue; 4 blue, 15 red, 4 green
+Game 33: 3 red, 10 blue; 4 red, 9 blue; 1 green, 10 blue
+Game 34: 3 blue, 1 green, 9 red; 4 green, 2 red, 9 blue; 7 blue, 3 red; 6 blue, 13 red; 4 green, 13 blue, 9 red
+Game 35: 14 red, 1 green; 1 red, 2 green, 4 blue; 3 blue, 10 red, 6 green; 5 blue, 6 red, 7 green; 7 blue, 5 red
+Game 36: 2 blue, 8 red, 9 green; 9 green, 3 red, 10 blue; 6 red, 8 blue, 1 green; 6 green, 8 red, 4 blue
+Game 37: 10 green, 3 red, 6 blue; 2 blue, 9 red, 5 green; 13 green, 9 red, 10 blue; 2 blue, 4 green, 9 red
+Game 38: 4 red, 14 blue, 12 green; 6 red, 12 green, 18 blue; 6 green, 1 blue, 1 red
+Game 39: 5 red, 1 blue, 3 green; 1 blue, 3 green, 8 red; 15 red, 1 blue, 5 green; 3 green, 5 red; 1 blue, 14 red; 3 green, 1 blue, 12 red
+Game 40: 8 green, 4 blue; 5 blue, 7 red, 8 green; 5 blue, 8 green; 6 green, 3 red, 12 blue; 14 blue, 7 green, 2 red; 1 green, 7 red, 5 blue
+Game 41: 7 red, 10 green; 10 red, 6 green; 9 red, 7 green, 1 blue; 3 red, 1 blue
+Game 42: 3 green, 2 blue, 13 red; 1 blue, 3 red; 11 green, 16 red; 3 green, 1 blue, 16 red; 5 red, 8 green
+Game 43: 12 blue, 9 red; 16 blue, 2 red, 7 green; 4 red, 1 blue, 11 green; 15 blue, 4 red, 9 green
+Game 44: 17 green, 5 blue, 2 red; 9 green, 11 blue, 1 red; 20 green, 3 blue, 8 red; 2 red, 13 green, 9 blue; 15 green, 12 blue; 4 blue, 7 green, 9 red
+Game 45: 5 green; 5 green, 1 red; 3 green, 2 blue; 1 green, 1 blue, 1 red
+Game 46: 10 red, 11 green; 16 green, 8 blue, 12 red; 9 green, 9 blue
+Game 47: 20 green, 17 red, 1 blue; 16 red, 2 blue, 11 green; 3 blue, 19 red, 1 green; 3 blue, 17 red, 17 green; 12 green, 2 blue, 7 red
+Game 48: 1 red, 4 blue, 6 green; 19 green, 1 red, 1 blue; 16 green, 3 blue, 1 red; 3 blue, 17 green; 4 blue, 12 green
+Game 49: 13 green, 2 blue, 1 red; 1 green, 8 red, 2 blue; 11 red, 11 green, 3 blue; 7 red, 8 green, 4 blue
+Game 50: 11 blue, 1 red, 2 green; 1 green, 10 blue; 1 blue; 6 blue; 1 green, 2 blue
+Game 51: 3 red, 3 green, 1 blue; 3 green, 3 red; 10 green, 4 red; 3 red, 2 green
+Game 52: 1 red, 4 blue; 1 green, 11 blue; 1 green, 3 red, 6 blue; 4 red, 1 green, 4 blue; 9 blue, 1 green; 10 blue, 1 green
+Game 53: 2 blue, 4 green, 1 red; 8 blue, 4 red, 7 green; 9 red, 7 blue, 6 green; 3 red, 7 green, 1 blue; 2 red, 9 blue, 5 green; 1 green, 7 red, 10 blue
+Game 54: 1 red, 1 blue, 5 green; 2 red, 1 green, 2 blue; 3 green, 3 blue, 2 red; 4 red; 12 red, 5 green, 2 blue
+Game 55: 2 red, 11 blue; 16 green, 7 red, 16 blue; 4 blue, 11 green, 7 red; 8 green, 18 blue, 8 red
+Game 56: 2 blue, 2 green, 1 red; 1 red, 1 green; 1 red; 4 green; 1 blue; 1 blue, 7 green
+Game 57: 4 blue, 3 green; 16 green, 2 red, 5 blue; 1 red, 13 green, 2 blue; 3 blue, 12 green, 2 red; 2 red, 5 blue, 4 green; 10 green, 2 blue
+Game 58: 3 blue, 8 green; 4 green, 3 blue; 7 green, 5 blue, 5 red; 8 green; 3 red, 6 blue, 9 green; 2 red, 10 green, 4 blue
+Game 59: 7 blue, 6 green, 5 red; 7 red, 2 blue; 5 red, 11 green, 14 blue; 8 green, 17 red
+Game 60: 3 green, 8 blue, 2 red; 4 green, 7 blue, 6 red; 13 blue, 8 green, 2 red; 10 red, 6 blue, 5 green; 11 green, 3 blue, 4 red; 9 red, 5 green, 9 blue
+Game 61: 4 red, 18 blue, 13 green; 9 green, 5 red, 3 blue; 4 green, 3 blue, 4 red; 8 red, 4 green, 7 blue; 8 red, 4 blue, 6 green; 10 green, 5 red, 14 blue
+Game 62: 12 red, 14 blue, 9 green; 9 blue, 6 red, 4 green; 2 red, 5 blue; 1 red, 12 blue
+Game 63: 11 blue, 13 red, 11 green; 4 blue, 9 green; 8 blue, 9 red; 7 red, 11 green, 7 blue
+Game 64: 10 blue, 8 red, 12 green; 10 red, 12 blue, 9 green; 3 green, 17 red; 12 green, 15 blue, 16 red; 6 green, 15 blue, 1 red; 9 red, 6 blue, 10 green
+Game 65: 7 red, 7 blue; 3 blue, 1 red, 1 green; 3 red, 8 blue
+Game 66: 1 blue, 3 red; 10 green, 5 blue; 4 green; 3 red, 11 green; 3 blue, 15 green, 3 red
+Game 67: 1 red; 2 blue, 2 green, 1 red; 6 green, 1 blue
+Game 68: 7 red, 4 blue; 4 blue, 6 red, 7 green; 2 green, 19 red, 11 blue; 11 green, 9 red
+Game 69: 4 blue, 3 green, 1 red; 7 blue, 1 red, 3 green; 5 blue, 1 green; 2 blue, 10 green, 2 red; 2 red, 6 green, 5 blue; 1 red, 4 green, 2 blue
+Game 70: 9 blue, 7 red, 6 green; 19 blue, 4 red, 5 green; 6 blue, 7 red, 4 green; 3 blue, 4 red, 2 green
+Game 71: 6 green, 12 blue, 4 red; 11 red, 10 green, 11 blue; 3 red, 14 blue, 13 green; 4 blue, 3 green
+Game 72: 2 green, 1 blue, 9 red; 10 red, 3 green, 1 blue; 11 red, 2 green; 2 green, 1 blue, 5 red; 1 red, 1 blue, 3 green; 13 red, 4 blue, 1 green
+Game 73: 11 green, 6 blue; 7 green, 6 blue, 7 red; 12 green, 8 blue, 11 red; 4 red, 2 blue, 9 green; 4 green, 7 blue, 2 red
+Game 74: 3 blue, 7 red; 3 blue, 5 green, 2 red; 5 red, 1 green, 3 blue; 8 green, 2 blue, 11 red; 3 blue, 8 green, 10 red
+Game 75: 2 green; 5 blue; 1 blue, 1 red; 1 red, 9 blue, 2 green; 2 blue, 2 green
+Game 76: 12 blue, 13 green; 5 red, 11 blue, 9 green; 12 green, 6 red
+Game 77: 1 blue, 15 green, 12 red; 15 green, 5 blue; 14 green, 3 blue, 8 red
+Game 78: 11 green, 8 blue, 1 red; 9 green, 8 blue, 1 red; 13 green, 5 red, 6 blue; 5 red, 7 green, 20 blue; 10 blue, 5 red
+Game 79: 3 blue; 6 blue, 5 red; 4 red, 1 green, 4 blue; 7 blue, 6 red; 7 red, 1 blue; 1 red, 1 blue, 1 green
+Game 80: 11 green, 3 red, 8 blue; 2 red, 15 green, 2 blue; 5 green, 8 blue, 2 red; 8 blue, 14 green; 2 blue, 13 green
+Game 81: 9 red, 4 green; 7 green, 4 red; 2 red, 4 blue, 6 green; 6 red, 4 blue, 9 green; 1 green, 3 red; 6 green, 1 blue, 8 red
+Game 82: 5 blue, 3 red, 3 green; 5 red; 2 red, 3 green, 8 blue
+Game 83: 10 green, 1 red, 1 blue; 3 red, 1 green, 1 blue; 4 red, 10 green
+Game 84: 16 red, 2 green, 6 blue; 6 red, 3 green, 8 blue; 3 green, 10 red, 5 blue; 4 blue, 3 green; 15 red
+Game 85: 3 green, 2 red; 5 green, 4 blue; 5 green, 8 red, 3 blue
+Game 86: 7 green, 16 blue, 7 red; 1 green, 12 red, 2 blue; 15 green, 16 blue, 7 red
+Game 87: 1 red, 6 green, 5 blue; 2 green, 1 blue; 2 green, 1 red, 1 blue; 5 green, 4 blue
+Game 88: 3 green, 3 red, 4 blue; 1 red, 1 green; 6 blue, 9 red, 1 green; 1 green, 11 red, 3 blue; 7 red, 6 blue
+Game 89: 2 blue, 3 red, 4 green; 5 red, 7 blue, 14 green; 8 blue, 5 red, 16 green; 2 blue, 5 red, 7 green; 5 green, 9 blue, 1 red
+Game 90: 1 blue, 3 red, 7 green; 11 green, 4 red, 1 blue; 1 red, 1 blue, 6 green; 2 blue, 2 green; 8 green, 2 blue; 3 red, 2 blue, 4 green
+Game 91: 6 blue, 4 red, 1 green; 8 red, 3 blue, 3 green; 1 green, 2 blue, 5 red; 1 blue, 3 green
+Game 92: 8 green, 1 red, 5 blue; 2 green, 7 blue; 11 blue, 5 green, 8 red; 7 blue, 3 red, 4 green
+Game 93: 3 green, 1 red, 9 blue; 13 red, 5 blue, 8 green; 5 green, 2 red, 7 blue
+Game 94: 4 green, 10 blue, 8 red; 4 red, 10 blue, 2 green; 2 green, 10 blue, 5 red; 5 green, 2 red, 10 blue
+Game 95: 5 green, 1 blue; 3 blue, 11 green, 8 red; 8 blue, 2 red, 12 green; 4 green, 4 blue, 4 red
+Game 96: 1 blue, 13 green; 8 blue, 3 red, 4 green; 1 red, 3 blue, 10 green
+Game 97: 18 green, 4 red; 1 blue, 2 red, 9 green; 6 red, 3 blue, 10 green; 3 blue, 15 green, 4 red
+Game 98: 2 blue, 3 green, 6 red; 1 green, 1 blue, 8 red; 8 red, 3 green, 1 blue; 2 blue; 8 red, 2 green, 2 blue
+Game 99: 1 green, 2 red, 1 blue; 8 green, 4 blue, 1 red; 7 blue, 1 red, 11 green; 9 green, 3 blue; 1 red, 2 blue; 1 red, 6 blue
+Game 100: 7 blue, 9 green, 2 red; 5 red, 9 green; 1 blue, 8 red, 13 green \ No newline at end of file
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
diff --git a/day2_sample.in b/day2_sample.in
index e69de29..1cd7d33 100644
--- a/day2_sample.in
+++ b/day2_sample.in
@@ -0,0 +1,5 @@
+Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
+Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
+Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
+Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
+Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green \ No newline at end of file