commit: d8e07cb4b76db643e38caa0bcab20e56a9e1b529
parent 1ce0691d9be2aa63f7f00e6d208a2c8f89a84df8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 2 Dec 2023 15:13:44 +0100
day2.ha: new
Diffstat:
A | day2.ha | 94 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 94 insertions(+), 0 deletions(-)
diff --git a/day2.ha b/day2.ha
@@ -0,0 +1,94 @@
+// SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+aoc2023@hacktivis.me>
+// SPDX-License-Identifier: MIT
+
+use bufio;
+use strings;
+use strconv;
+use fmt;
+use os;
+use io;
+
+type game = struct {
+ red: uint,
+ green: uint,
+ blue: uint,
+};
+
+export fn main() void = {
+ let total = 0u32;
+
+ for(let id = 1u32; true; id += 1) {
+ const line = match(bufio::read_line(os::stdin)) {
+ case let l: []u8 => yield l;
+ case io::EOF => break;
+ case => abort();
+ };
+
+ const line = strings::fromutf8(line)!;
+
+ let val = game {
+ red = 0,
+ green = 0,
+ blue = 0,
+ };
+
+ const (id_str, reveal) = strings::cut(line, ": ");
+
+ fmt::printfln("{}={} | {}", id, id_str, reveal)!;
+
+ let hands = strings::tokenize(reveal, "; ");
+ for(true) {
+ const hand = match(strings::next_token(&hands)) {
+ case let h: str => yield h;
+ case void => break;
+ };
+ //fmt::printfln(" hand: {}", hand)!;
+
+
+ let colors = strings::tokenize(hand, ", ");
+ for(true) {
+ const color = match(strings::next_token(&colors)) {
+ case let h: str => yield h;
+ case void => break;
+ };
+ //fmt::printfln(" color: {}", color)!;
+ const (n, color) = strings::cut(color, " ");
+
+ const n = match(strconv::stou(n)) {
+ case let u: uint =>
+ yield u;
+ case =>
+ fmt::fatalf("Not a number: {}", n);
+ };
+
+ switch(color) {
+ case "red" =>
+ if(n > val.red) {
+ val.red = n;
+ };
+ case "green" =>
+ if(n > val.green) {
+ val.green = n;
+ };
+ case "blue" =>
+ if(n > val.blue) {
+ val.blue = n;
+ };
+ case =>
+ fmt::fatalf("Unknown color: {}", color);
+ };
+ };
+ };
+
+ fmt::printf("{} = R{}, G{}, B{}", id, val.red, val.green, val.blue)!;
+
+ if(val.red > 12 || val.green > 13 || val.blue > 14) {
+ fmt::printfln(" IMP")!;
+ } else {
+ total += id;
+ fmt::printfln(" POS")!;
+ };
+ };
+
+ fmt::printfln("Total: {}", total)!;
+};