logo

adventofcode

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

day1.ha (993B)


  1. // SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+aoc2023@hacktivis.me>
  2. // SPDX-License-Identifier: MIT
  3. use bufio;
  4. use strings;
  5. use ascii;
  6. use fmt;
  7. use os;
  8. use io;
  9. export fn main() void = {
  10. let total = 0u32;
  11. for(true) {
  12. const line = match(bufio::scanline(os::stdin)) {
  13. case let l: []u8 => yield l;
  14. case io::EOF => break;
  15. case => abort();
  16. };
  17. const line = strings::fromutf8(line)!;
  18. let val = 0u32;
  19. let iter = strings::iter(line);
  20. for(true) {
  21. match(strings::next(&iter)) {
  22. case let c: rune =>
  23. if(ascii::isdigit(c)) {
  24. let c = c: u32;
  25. val = (c - '0')*10;
  26. break;
  27. };
  28. case =>
  29. abort();
  30. };
  31. };
  32. let iter = strings::riter(line);
  33. for(true) {
  34. match(strings::next(&iter)) {
  35. case let c: rune =>
  36. if(ascii::isdigit(c)) {
  37. let c = c: u32;
  38. val += (c - '0');
  39. break;
  40. };
  41. case =>
  42. abort();
  43. };
  44. };
  45. fmt::printfln("= {}", val)!;
  46. total += val;
  47. };
  48. fmt::printfln("Total: {}", total)!;
  49. };