logo

adventofcode

Code used to solve https://adventofcode.com/, one branch per year git clone https://hacktivis.me/git/adventofcode.git

day4.0.ha (1263B)


  1. // SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+adventofcode@hacktivis.me>
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. use bufio;
  4. use os;
  5. use io;
  6. use fmt;
  7. use strings;
  8. use ascii;
  9. use strconv;
  10. fn parse_range(in: str) (uint, uint) = {
  11. let (head, tail) = strings::cut(in, "-");
  12. let head = strconv::stou(head) as uint;
  13. let tail = strconv::stou(tail) as uint;
  14. return (head,tail);
  15. };
  16. export fn main() void = {
  17. let score = 0;
  18. for(true) {
  19. let line = match(bufio::scanline(os::stdin)) {
  20. case let l: []u8 =>
  21. yield l;
  22. case io::EOF =>
  23. break;
  24. case let e: io::error =>
  25. fmt::fatal("day4.0: {}", io::strerror(e));
  26. };
  27. let line = strings::fromutf8(line);
  28. let (dum, dee) = strings::cut(line, ",");
  29. let dum = parse_range(dum);
  30. let dee = parse_range(dee);
  31. fmt::printfln("Checking {}-{} and {}-{}", dum.0, dum.1, dee.0, dee.1)!;
  32. // Is dee fully inside dum?
  33. if(dum.0 >= dee.0 && dee.1 >= dum.1) {
  34. fmt::printfln("{}-{} is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
  35. score += 1;
  36. continue;
  37. };
  38. // Is dum fully inside dee?
  39. if(dee.0 >= dum.0 && dum.1 >= dee.1) {
  40. fmt::printfln("{}-{} contains {}-{}", dum.0, dum.1, dee.0, dee.1)!;
  41. score += 1;
  42. continue;
  43. };
  44. };
  45. fmt::printfln("Score: {}", score)!;
  46. };