Skip to content

Commit ef5d7e6

Browse files
committed
Day 4 down
1 parent b924c2a commit ef5d7e6

5 files changed

Lines changed: 90 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1010

1111
| Year | Completed |
1212
| :---: | :---: |
13-
| [2025](aoc2025) | 6/24 |
13+
| [2025](aoc2025) | 8/24 |
1414
| [2024](aoc2024) | 50/50 |
1515
| [2023](aoc2023) | 50/50 |
1616

aoc2025/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
99
| [Day 1](https://adventofcode.com/2025/day/1) | [code](src/bin/01.rs) |||
1010
| [Day 2](https://adventofcode.com/2025/day/2) | [code](src/bin/02.rs) |||
1111
| [Day 3](https://adventofcode.com/2025/day/3) | [code](src/bin/03.rs) |||
12-
| [Day 4](https://adventofcode.com/2025/day/4) | [code](src/bin/04.rs) | _ | _ |
12+
| [Day 4](https://adventofcode.com/2025/day/4) | [code](src/bin/04.rs) | | |
1313
| [Day 5](https://adventofcode.com/2025/day/5) | [code](src/bin/05.rs) | _ | _ |
1414
| [Day 6](https://adventofcode.com/2025/day/6) | [code](src/bin/06.rs) | _ | _ |
1515
| [Day 7](https://adventofcode.com/2025/day/7) | [code](src/bin/07.rs) | _ | _ |

aoc2025/data/examples/04.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..@@.@@@@.
2+
@@@.@.@.@@
3+
@@@@@.@.@@
4+
@.@@@@..@.
5+
@@.@@@@.@@
6+
.@@@@@@@.@
7+
.@.@.@.@@@
8+
@.@@@.@@@@
9+
.@@@@@@@@.
10+
@.@.@@@.@.

aoc2025/src/bin/04.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::collections::BTreeSet;
2+
3+
use aoc_utils::*;
4+
use itertools::Itertools;
5+
6+
advent_of_code::solution!(4);
7+
8+
pub fn part_one(input: &str) -> Option<u64> {
9+
let map = input.c_map();
10+
let num = map
11+
.iter()
12+
.enumerate()
13+
.flat_map(|(x, v)| {
14+
v.iter()
15+
.enumerate()
16+
.filter(|(_, c)| **c == '@')
17+
.map(|(y, _)| Point(x, y))
18+
.collect_vec()
19+
})
20+
.filter(|p| {
21+
DirExt::neighbors(*p, Bounds(map.len() - 1, map[0].len() - 1))
22+
.iter()
23+
.filter(|p2| map[p2.0][p2.1] == '@')
24+
.count()
25+
< 4
26+
})
27+
.count();
28+
Some(num as u64)
29+
}
30+
31+
pub fn part_two(input: &str) -> Option<u64> {
32+
let mut map = input.c_map();
33+
let mut queue = map
34+
.iter()
35+
.enumerate()
36+
.flat_map(|(x, v)| {
37+
v.iter()
38+
.enumerate()
39+
.filter(|(_, c)| **c == '@')
40+
.map(|(y, _)| Point(x, y))
41+
.collect_vec()
42+
})
43+
.collect::<BTreeSet<Point>>();
44+
let mut removed = 0;
45+
while !queue.is_empty() {
46+
let curr = queue.pop_first().unwrap();
47+
let neighbors = DirExt::neighbors(curr, Bounds(map.len() - 1, map[0].len() - 1));
48+
if neighbors.iter().filter(|p| map[p.0][p.1] == '@').count() < 4 {
49+
removed += 1;
50+
map[curr.0][curr.1] = '.';
51+
neighbors
52+
.into_iter()
53+
.filter(|p| map[p.0][p.1] == '@')
54+
.for_each(|p| {
55+
queue.insert(p);
56+
});
57+
}
58+
}
59+
Some(removed)
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_part_one() {
68+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
69+
assert_eq!(result, Some(13));
70+
}
71+
72+
#[test]
73+
fn test_part_two() {
74+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
75+
assert_eq!(result, Some(43));
76+
}
77+
}

aoc_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use regex::{Captures, Regex};
55
use tinyvec::{ArrayVec, array_vec};
66

77
/// 2D grid point: `Point(row, col)`
8-
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
8+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Hash, Ord)]
99
pub struct Point(pub usize, pub usize);
1010

1111
/// Find first occurrence of value in 2D grid

0 commit comments

Comments
 (0)