diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2025-12-02 22:36:23 +0100 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2025-12-02 22:36:23 +0100 |
| commit | cab6c1ddc066b2af76fac909fde66e3d5b4cdcd8 (patch) | |
| tree | 48f647c69c8138e370f6a72c6fa16117d5af9900 /day2 | |
| parent | feat: day1 part1 complete (diff) | |
| download | aoc-cab6c1ddc066b2af76fac909fde66e3d5b4cdcd8.tar.gz | |
feat: day2 part1 complete
Diffstat (limited to 'day2')
| -rw-r--r-- | day2/part1/main.ha | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/day2/part1/main.ha b/day2/part1/main.ha new file mode 100644 index 0000000..aa4675d --- /dev/null +++ b/day2/part1/main.ha @@ -0,0 +1,54 @@ +use fmt; +use io; +use math; +use os; +use strconv; +use strings; + +fn digits(num: u64) u64 = { + return math::ceilf64(math::log10f64(num: f64)): u64; +}; + +export fn main() void = { + let handle = os::open("input.txt")!; + defer io::close(handle)!; + + let buf = io::drain(handle) as []u8; + defer free(buf); + + let input = strings::fromutf8_unsafe(buf); + let range_strings = strings::split(input, ",")!; + + let answer: u64 = 0; + + for (let i: size = 0; i < len(range_strings); i += 1) { + let range_ends = strings::cut(strings::rtrim(range_strings[i], '\n'), "-"); + + let start = strconv::stou64(range_ends.0)!; + let end = strconv::stou64(range_ends.1)!; + + let start_digits = digits(start); + let end_digits = digits(end); + + if (start_digits % 2 != 0 && end_digits % 2 != 0 && start_digits == end_digits) { + continue; + }; + + for (let j = start; j <= end; j += 1) { + let digits = digits(j); + + if (digits % 2 == 0) { + let num_str = strconv::u64tos(j); + + let first_half = strings::sub(num_str, 0, len(num_str) / 2); + let second_half = strings::sub(num_str, len(num_str) / 2, len(num_str)); + + if (first_half == second_half) { + answer += j; + }; + }; + }; + }; + + fmt::printfln("Answer: {}", answer)!; +}; |
