summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2025/d3.rs8
-rw-r--r--2025/d4.rs209
-rw-r--r--2025/e10
-rw-r--r--2025/fetch.rs1
-rw-r--r--2025/main.rs3
-rw-r--r--2025/read.rs1
6 files changed, 229 insertions, 3 deletions
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<Vec<Symbol>>,
+}
+
+impl Map {
+ fn new(inner: Vec<Vec<Symbol>>) -> 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<Vec<Symbol>>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.inner
+ }
+}
+
+pub fn part2() {
+ let input = fetch(
+ 4, '\n',
+ );
+ // let input: Vec<String> = read(
+ // "e", '\n',
+ // );
+ // .into_iter()
+ // .filter(|s| !s.is_empty())
+ // .collect();
+ let mut vec: Vec<Vec<Symbol>> = 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<String> = read(
+ "e", '\n',
+ );
+ // .into_iter()
+ // .filter(|s| !s.is_empty())
+ // .collect();
+ let mut vec: Vec<Vec<Symbol>> = 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<String> {
.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<Path>, p: impl Pattern) -> Vec<String> {
.expect("Failed to read file")
.split(p)
.map(|l| l.to_string())
+ .filter(|s| !s.is_empty())
.collect()
}