From f928714eaeabb1497afca28f877ba8bb5fede25f Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 4 Dec 2025 18:12:25 +0800 Subject: new: day 4 --- 2025/d3.rs | 8 ++- 2025/d4.rs | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2025/e | 10 +++ 2025/fetch.rs | 1 + 2025/main.rs | 3 +- 2025/read.rs | 1 + 6 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 2025/d4.rs create mode 100644 2025/e (limited to '2025') diff --git a/2025/d3.rs b/2025/d3.rs index 3d56ed6..b8e3f23 100644 --- a/2025/d3.rs +++ b/2025/d3.rs @@ -75,7 +75,9 @@ impl JoltageBank { } pub fn part1() { - let input = fetch(3, '\n'); + let input = fetch( + 3, '\n', + ); let mut res = 0; for i in input { if i.is_empty() { @@ -98,7 +100,9 @@ pub fn part1() { pub fn part2() { // let input = fetch(3, '\n'); - let input = read("d", '\n'); + let input = read( + "d", '\n', + ); let mut res = 0; for i in input { if i.is_empty() { diff --git a/2025/d4.rs b/2025/d4.rs new file mode 100644 index 0000000..b814aa1 --- /dev/null +++ b/2025/d4.rs @@ -0,0 +1,209 @@ +use std::ops::{Deref, DerefMut}; + +use aoc::{fetch::fetch, read::read}; + +#[derive(PartialEq, Clone, Debug)] +enum Symbol { + None, + Paper, +} + +#[derive(Debug)] +struct Finder { + map: Map, + x: usize, + y: usize, +} + +impl Finder { + fn new(map: Map) -> Self { + Self { + map, + x: usize::MAX, + y: Default::default(), + } + } + + fn next_paper(&mut self) -> bool { + for i in self.y + ..self + .map + .len() + { + let x = if i == self.y { + self.x + .wrapping_add(1) + } else { + 0 + }; + for j in x..self.map[i].len() { + match &self.map[i][j] { + Symbol::Paper => { + self.x = j; + self.y = i; + return true; + } + Symbol::None => {} + } + } + } + false + } + + fn get_prox(&self) -> usize { + let mut total = 0; + for y in -1i32..=1 { + for x in -1i32..=1 { + if x == 0 && y == 0 { + continue; + } + let ny = self + .y + .wrapping_add(y as usize); + let nx = self + .x + .wrapping_add(x as usize); + if ny + < self + .map + .len() + && nx < self.map[ny].len() + && self.map[ny][nx] == Symbol::Paper + { + total += 1; + } + } + } + total + } +} + +#[derive(Clone, Debug)] +struct Map { + inner: Vec>, +} + +impl Map { + fn new(inner: Vec>) -> Self { + Self { + inner, + } + } +} + +impl DerefMut for Map { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.inner + } +} + +impl Deref for Map { + type Target = Vec>; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +pub fn part2() { + let input = fetch( + 4, '\n', + ); + // let input: Vec = read( + // "e", '\n', + // ); + // .into_iter() + // .filter(|s| !s.is_empty()) + // .collect(); + let mut vec: Vec> = vec![Vec::new(); input.len()]; + for (i, s) in input + .iter() + .enumerate() + { + for c in s.chars() { + vec[i].push( + match c { + '.' => Symbol::None, + '@' => Symbol::Paper, + _ => panic!(), + }, + ); + } + } + let map = Map::new(vec); + let mut finder = Finder::new(map); + let mut res = 0; + let mut ires = 0; + loop { + if !finder.next_paper() { + if ires == 0 { + break; + } else { + res += ires; + ires = 0; + finder.x = usize::MAX; + finder.y = 0; + continue; + } + } + println!( + "{} at {},{}", + finder.get_prox(), + finder.x, + finder.y + ); + if finder.get_prox() < 4 { + finder.map[finder.y][finder.x] = Symbol::None; + ires += 1; + } + } + println!("{res}"); +} + +pub fn part1() { + // let input = fetch( + // 4, '\n', + // ); + let input: Vec = read( + "e", '\n', + ); + // .into_iter() + // .filter(|s| !s.is_empty()) + // .collect(); + let mut vec: Vec> = vec![Vec::new(); input.len()]; + for (i, s) in input + .iter() + .enumerate() + { + for c in s.chars() { + vec[i].push( + match c { + '.' => Symbol::None, + '@' => Symbol::Paper, + _ => panic!(), + }, + ); + } + } + let map = Map::new(vec); + let mut finder = Finder::new(map); + let mut res = 0; + loop { + if !finder.next_paper() { + break; + } + println!( + "{} at {},{}", + finder.get_prox(), + finder.x, + finder.y + ); + if finder.get_prox() < 4 { + res += 1 + } + } + // We skipped the first since we directly x+1 when going into the loop, + // could prolly fix it via init x: usize::MAX then just wrapping add, but + // too lazy + println!("{res}"); +} diff --git a/2025/e b/2025/e new file mode 100644 index 0000000..8209399 --- /dev/null +++ b/2025/e @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. diff --git a/2025/fetch.rs b/2025/fetch.rs index 6fc7ed7..6f94ec8 100644 --- a/2025/fetch.rs +++ b/2025/fetch.rs @@ -25,5 +25,6 @@ pub fn fetch(lv: usize, p: impl Pattern) -> Vec { .unwrap() .split(p) .map(|s| s.to_string()) + .filter(|s| !s.is_empty()) .collect() } diff --git a/2025/main.rs b/2025/main.rs index 350a50d..dbebf26 100644 --- a/2025/main.rs +++ b/2025/main.rs @@ -1,7 +1,8 @@ pub mod d1; pub mod d2; pub mod d3; +pub mod d4; fn main() { - d3::part2(); + d4::part2(); } diff --git a/2025/read.rs b/2025/read.rs index ebd41bd..871b0bd 100644 --- a/2025/read.rs +++ b/2025/read.rs @@ -5,5 +5,6 @@ pub fn read(file: impl AsRef, p: impl Pattern) -> Vec { .expect("Failed to read file") .split(p) .map(|l| l.to_string()) + .filter(|s| !s.is_empty()) .collect() } -- cgit v1.2.3