day4.1.ha (1630B)
- // 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)!;
- // Start of dum being in dee?
- if(dum.0 >= dee.0 && dum.0 <= dee.1) {
- fmt::printfln("{}(-{}) is inside {}-{}", dum.0, dum.1, dee.0, dee.1)!;
- score += 1;
- continue;
- };
- // Start of dee being in dum?
- if(dee.0 >= dum.0 && dee.0 <= dum.1) {
- fmt::printfln("{}(-{}) is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
- score += 1;
- continue;
- };
- // End of dum being in dee?
- if(dum.1 <= dee.1 && dum.1 >= dee.0) {
- fmt::printfln("({}-){} is inside {}-{}", dum.0, dum.1, dee.0, dee.1)!;
- score += 1;
- continue;
- };
- // End of dee being in dum?
- if(dee.1 <= dum.1 && dee.1 >= dum.0) {
- fmt::printfln("({}-){} is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
- score += 1;
- continue;
- };
- };
- fmt::printfln("Score: {}", score)!;
- };