logo

adventofcode

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

day4.1.ha (1630B)


  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. // Start of dum being in dee?
  33. if(dum.0 >= dee.0 && dum.0 <= dee.1) {
  34. fmt::printfln("{}(-{}) is inside {}-{}", dum.0, dum.1, dee.0, dee.1)!;
  35. score += 1;
  36. continue;
  37. };
  38. // Start of dee being in dum?
  39. if(dee.0 >= dum.0 && dee.0 <= dum.1) {
  40. fmt::printfln("{}(-{}) is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
  41. score += 1;
  42. continue;
  43. };
  44. // End of dum being in dee?
  45. if(dum.1 <= dee.1 && dum.1 >= dee.0) {
  46. fmt::printfln("({}-){} is inside {}-{}", dum.0, dum.1, dee.0, dee.1)!;
  47. score += 1;
  48. continue;
  49. };
  50. // End of dee being in dum?
  51. if(dee.1 <= dum.1 && dee.1 >= dum.0) {
  52. fmt::printfln("({}-){} is inside {}-{}", dee.0, dee.1, dum.0, dum.1)!;
  53. score += 1;
  54. continue;
  55. };
  56. };
  57. fmt::printfln("Score: {}", score)!;
  58. };