day3.0.ha (1262B)
- // 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;
- export fn main() void = {
- let scores: []u32 = [];
- for(true) {
- let line = match(bufio::scanline(os::stdin)) {
- case let l: []u8 =>
- yield l;
- case io::EOF =>
- break;
- case let e: io::error =>
- fmt::fatal("day3.0: {}", io::strerror(e));
- };
- let line = strings::fromutf8(line);
- let head = strings::sub(line, 0, len(line)/2);
- let tail = strings::sub(line, len(line)/2, strings::end);
- let i = strings::iter(head);
- let common = ' ': rune;
- for(true) {
- let dup = match(strings::next(&i)) {
- case let r: rune => yield r;
- case void => break;
- };
- if(strings::contains(tail, 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("{} | {} -> {}, {}", head, tail, common, score)!;
- append(scores, score);
- };
- let final = 0u32;
- for(let i = 0z; i < len(scores); i += 1) {
- final += scores[i];
- };
- fmt::printfln("Score: {}", final)!;
- };