diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2025-12-01 10:49:09 +0100 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2025-12-01 12:41:34 +0100 |
| commit | 0c01e8805063e1dccdb4ce467b8f4fde11f02f86 (patch) | |
| tree | 70e6af16552f96c2dda94ad1f5d3b9a944c33a1f /day1/part1 | |
| download | aoc-0c01e8805063e1dccdb4ce467b8f4fde11f02f86.tar.gz | |
feat: day1 part1 complete
Diffstat (limited to '')
| -rw-r--r-- | day1/part1/main.ha | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/day1/part1/main.ha b/day1/part1/main.ha new file mode 100644 index 0000000..4f9b2b2 --- /dev/null +++ b/day1/part1/main.ha @@ -0,0 +1,43 @@ +use os; +use io; +use fmt; +use strings; +use strconv; + +export fn main() void = { + const 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 lines = strings::split(input, "\n")!; + + let dial: i16 = 50; + let zeroes_seen: u16 = 0; + + for (let line .. lines[..]) { + if (strings::hasprefix(line, "L")) { + dial -= strconv::stoi16(strings::ltrim(line, 'L'))!; + } else if (strings::hasprefix(line, "R")) { + dial += strconv::stoi16(strings::ltrim(line, 'R'))!; + } else { + continue; + }; + + for (dial < 0 || dial > 99) { + if (dial < 0) { + dial += 100; + } else if (dial > 99) { + dial -= 100; + }; + }; + + if (dial == 0) { + zeroes_seen += 1; + }; + }; + + fmt::printfln("Password: {}", zeroes_seen)!; +}; |
