commit: db4fb712bffff95580d5270d6123544dca4624d2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 1 Dec 2023 20:46:37 +0100
day1.ha: new
Diffstat:
A | day1.ha | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 55 insertions(+), 0 deletions(-)
diff --git a/day1.ha b/day1.ha
@@ -0,0 +1,55 @@
+use bufio;
+use strings;
+use ascii;
+use fmt;
+use os;
+use io;
+
+export fn main() void = {
+ let total = 0u32;
+
+ for(true) {
+ const line = match(bufio::scanline(os::stdin)) {
+ case let l: []u8 => yield l;
+ case io::EOF => break;
+ case => abort();
+ };
+
+ const line = strings::fromutf8(line)!;
+
+ let val = 0u32;
+
+ let iter = strings::iter(line);
+ for(true) {
+ match(strings::next(&iter)) {
+ case let c: rune =>
+ if(ascii::isdigit(c)) {
+ let c = c: u32;
+ val = (c - '0')*10;
+ break;
+ };
+ case =>
+ abort();
+ };
+ };
+
+ let iter = strings::riter(line);
+ for(true) {
+ match(strings::next(&iter)) {
+ case let c: rune =>
+ if(ascii::isdigit(c)) {
+ let c = c: u32;
+ val += (c - '0');
+ break;
+ };
+ case =>
+ abort();
+ };
+ };
+
+ fmt::printfln("= {}", val)!;
+ total += val;
+ };
+
+ fmt::printfln("Total: {}", total)!;
+};