day2.ha (1881B)
- // 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)!;
- };