Skip to content

Commit 52e527a

Browse files
committed
day4 part1
1 parent 0f9dfa2 commit 52e527a

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

rust2025/src/days/d04.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,74 @@
1+
use crate::dprintln;
2+
13
use super::{Answer, Day, DayImpl};
24

35
const CURRENT_DAY: u8 = 4;
46

5-
type Data = Vec<u64>;
7+
type Data = Vec<Vec<bool>>;
68
impl DayImpl<Data> for Day<CURRENT_DAY> {
79
fn init_test() -> (Self, Data) {
810
Self::init(include_str!("test_inputs/test04.txt"))
911
}
1012

1113
fn expected_results() -> (Answer, Answer) {
12-
(Answer::Number(0), Answer::Number(0))
14+
(Answer::Number(13), Answer::Number(0))
1315
}
1416

1517
fn init(input: &str) -> (Self, Data) {
1618
(
1719
Self {},
1820
input
1921
.lines()
20-
.map(|v| v.parse::<u64>().expect("error while parsing input."))
22+
.map(|line| {
23+
line.chars()
24+
.map(|c| match c {
25+
'@' => true,
26+
'.' => false,
27+
_ => panic!("Unexpected character in input"),
28+
})
29+
.collect()
30+
})
2131
.collect(),
2232
)
2333
}
2434

2535
fn one(&self, data: &mut Data) -> Answer {
26-
Answer::Number(data.len() as u64)
36+
dprintln!("input data: {:?}", data);
37+
38+
let mut count: u64 = 0;
39+
40+
let (w, h) = (data[0].len(), data.len());
41+
42+
for i in 0..h {
43+
for j in 0..w {
44+
if !data[i][j] { continue; }
45+
46+
let mut neighbors = 0;
47+
48+
for di in -1..=1 {
49+
for dj in -1..=1 {
50+
if di == 0 && dj == 0 { continue; }
51+
52+
let neighbor_i = i as isize + di;
53+
let neighbor_j = j as isize + dj;
54+
55+
if neighbor_i < 0 || neighbor_i >= h as isize || neighbor_j < 0 || neighbor_j >= w as isize {
56+
continue;
57+
}
58+
59+
if data[neighbor_i as usize][neighbor_j as usize] {
60+
neighbors += 1;
61+
}
62+
}
63+
}
64+
65+
if neighbors < 4 {
66+
count += 1;
67+
}
68+
}
69+
}
70+
71+
Answer::Number(count)
2772
}
2873

2974
fn two(&self, data: &mut Data) -> Answer {
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+
@.@.@@@.@.

0 commit comments

Comments
 (0)