day4.0.ha (1263B)
- // 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;
- use strconv;
- fn parse_range(in: str) (uint, uint) = {
- let (head, tail) = strings::cut(in, "-");
- let head = strconv::stou(head) as uint;
- let tail = strconv::stou(tail) as uint;
- return (head,tail);
- };
- export fn main() void = {
- let score = 0;
- 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("day4.0: {}", io::strerror(e));
- };
- let line = strings::fromutf8(line);
- let (dum, dee) = strings::cut(line, ",");
- let dum = parse_range(dum);
- let dee = parse_range(dee);
- fmt::printfln("Checking {}-{} and {}-{}", dum.0, dum.1, dee.0, dee.1)!;
- // Is dee fully inside dum?
- if(dum.0 >= dee.0 && dee.1 >= dum.1) {
- fmt::printfln("{}-{} is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
- score += 1;
- continue;
- };
- // Is dum fully inside dee?
- if(dee.0 >= dum.0 && dum.1 >= dee.1) {
- fmt::printfln("{}-{} contains {}-{}", dum.0, dum.1, dee.0, dee.1)!;
- score += 1;
- continue;
- };
- };
- fmt::printfln("Score: {}", score)!;
- };