commit: 028080313eb63468a87081d77ef1add9ad1bef92
parent d8e07cb4b76db643e38caa0bcab20e56a9e1b529
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 2 Dec 2023 15:26:11 +0100
day2_part2.ha: new
Diffstat:
A | day2_part2.ha | 93 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 93 insertions(+), 0 deletions(-)
diff --git a/day2_part2.ha b/day2_part2.ha
@@ -0,0 +1,93 @@
+// 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)!;
+
+ const pow = val.red * val.green * val.blue;
+
+ fmt::printfln(" POW: {}", pow)!;
+
+ total += pow;
+ };
+
+ fmt::printfln("Total: {}", total)!;
+};