diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2025-12-04 16:26:36 +0100 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2025-12-04 16:26:36 +0100 |
| commit | b54093838238084a939cb53fb270c802b782258b (patch) | |
| tree | a3c1cd5443f0ce51f1e6939aba71aeb30807d31b | |
| parent | feat: day3 part2 complete (diff) | |
| download | aoc-b54093838238084a939cb53fb270c802b782258b.tar.gz | |
feat: day4 part1 complete
| -rw-r--r-- | day4/part1/main.ha | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/day4/part1/main.ha b/day4/part1/main.ha new file mode 100644 index 0000000..a1cc957 --- /dev/null +++ b/day4/part1/main.ha @@ -0,0 +1,63 @@ +use fmt; +use io; +use os; +use strconv; +use strings::{fromutf8_unsafe, split, torunes}; + +const adjacent_positions: [8][2]int = [ + [-1, -1], + [ 0, -1], + [ 1, -1], + [-1, 0], + [ 1, 0], + [-1, 1], + [ 0, 1], + [ 1, 1] +]; + +fn adjacent_rolls(grid: [][]rune, x: int, y: int) u64 = { + let rolls: u64 = 0; + + for (let pos .. adjacent_positions) { + let col = x: int + pos[0]; + let row = y: int + pos[1]; + + if (col < 0 || col >= len(grid[0]): int || + row < 0 || row >= len(grid): int) + continue; + + + if (grid[row][col] == '@') + rolls += 1; + }; + + return rolls; +}; + +export fn main() void = { + let handle = os::open("input.txt")!; + defer io::close(handle)!; + + let buf = io::drain(handle)!; + defer free(buf); + + let lines = split(fromutf8_unsafe(buf), "\n")!; + let grid: [][]rune = []; + + let accessable_rolls: u64 = 0; + + for (let line .. lines) { + if (line != "") + append(grid, torunes(line)!)!; + }; + + for (let x: size = 0; x < len(grid[0]); x += 1) { + for (let y: size = 0; y < len(grid); y += 1) { + if (grid[y][x] == '@' && + adjacent_rolls(grid, x: int, y: int) < 4) + accessable_rolls += 1; + }; + }; + + fmt::printfln("Answer: {}", accessable_rolls)!; +}; |
