day3.1.ha (1484B)
- // SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+adventofcode@hacktivis.me>
- // SPDX-License-Identifier: BSD-3-Clause
- use bufio;
- use os;
- use io;
- use fmt;
- use strings;
- use ascii;
- fn getline() ([]u8 | void) = {
- return match(bufio::scanline(os::stdin)) {
- case let l: []u8 =>
- yield l;
- case io::EOF =>
- yield void;
- case let e: io::error =>
- fmt::fatal("day3.1: {}", io::strerror(e));
- };
- };
- export fn main() void = {
- let scores: []u32 = [];
- for(true) {
- let line1 = match(getline()) {
- case let l: []u8 => yield strings::fromutf8(l);
- case => break;
- };
- let line2 = match(getline()) {
- case let l: []u8 => yield strings::fromutf8(l);
- case => break;
- };
- let line3 = match(getline()) {
- case let l: []u8 => yield strings::fromutf8(l);
- case => break;
- };
- let i = strings::iter(line1);
- let common = ' ': rune;
- for(true) {
- let dup = match(strings::next(&i)) {
- case let r: rune => yield r;
- case void => break;
- };
- if(strings::contains(line2, dup)) {
- if(strings::contains(line3, dup)) {
- common = dup;
- break;
- };
- };
- };
- let score = if(ascii::islower(common)) {
- yield (common: u32)-('a': u32)+1;
- } else {
- yield (common: u32)-('A': u32)+27;
- };
- let common = strings::fromrunes([common]);
- fmt::printfln("{}, {}", common, score)!;
- append(scores, score);
- };
- let final = 0u32;
- for(let i = 0z; i < len(scores); i += 1) {
- final += scores[i];
- };
- fmt::printfln("Score: {}", final)!;
- };