diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-12-06 20:34:18 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-12-06 20:40:12 +0100 |
| commit | d65dda9e5b818bc1bae6aaf6453145e08f00e5b7 (patch) | |
| tree | 04223988fe7524191be311e8eaf852fea072038e /day04 | |
Day01 - Day06
Diffstat (limited to 'day04')
| -rw-r--r-- | day04/.gitignore | 3 | ||||
| -rw-r--r-- | day04/Cargo.lock | 7 | ||||
| -rw-r--r-- | day04/Cargo.toml | 6 | ||||
| -rw-r--r-- | day04/src/main.rs | 96 |
4 files changed, 112 insertions, 0 deletions
diff --git a/day04/.gitignore b/day04/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day04/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day04/Cargo.lock b/day04/Cargo.lock new file mode 100644 index 0000000..de1e82f --- /dev/null +++ b/day04/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day04" +version = "0.1.0" diff --git a/day04/Cargo.toml b/day04/Cargo.toml new file mode 100644 index 0000000..a3cc735 --- /dev/null +++ b/day04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day04" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day04/src/main.rs b/day04/src/main.rs new file mode 100644 index 0000000..0205021 --- /dev/null +++ b/day04/src/main.rs @@ -0,0 +1,96 @@ +use std::io; + +fn valid(map: &[Vec<char>], x: i32, y: i32) -> bool { + if x < 0 { + return false; + } + if y < 0 { + return false; + } + let uy: usize = y.try_into().unwrap(); + if uy >= map.len() { + return false; + } + let ux: usize = x.try_into().unwrap(); + if ux >= map[uy].len() { + return false; + } + true +} + +fn is_paper(map: &[Vec<char>], x: i32, y: i32) -> bool { + if valid(map, x, y) { + let uy: usize = y.try_into().unwrap(); + let ux: usize = x.try_into().unwrap(); + return map[uy][ux] == '@'; + } + false +} + +fn adjacent(map: &[Vec<char>], x: i32, y: i32) -> usize { + let mut count = 0; + for dx in -1..=1 { + if is_paper(map, x + dx, y - 1) { + count += 1; + } + if is_paper(map, x + dx, y + 1) { + count += 1; + } + } + if is_paper(map, x - 1, y) { + count += 1; + } + if is_paper(map, x + 1, y) { + count += 1; + } + count +} + +fn main() { + let mut map: Vec<Vec<char>> = Vec::new(); + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + map.push(buffer.trim_end().chars().collect()); + } + + let mut total = 0; + for y in 0..map.len() { + for x in 0..map[y].len() { + if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap()) + && adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4 + { + total += 1; + } + } + } + + println!("{}", total); + + let mut total2 = 0; + loop { + total = 0; + let mut next = map.clone(); + for y in 0..map.len() { + for x in 0..map[y].len() { + if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap()) + && adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4 + { + total += 1; + next[y][x] = 'x'; + } + } + } + if total == 0 { + break; + } + map = next; + total2 += total; + } + + println!("{}", total2); +} |
