|
| 1 | +use crate::dprintln; |
| 2 | + |
1 | 3 | use super::{Answer, Day, DayImpl}; |
2 | 4 |
|
3 | 5 | const CURRENT_DAY: u8 = 4; |
4 | 6 |
|
5 | | -type Data = Vec<u64>; |
| 7 | +type Data = Vec<Vec<bool>>; |
6 | 8 | impl DayImpl<Data> for Day<CURRENT_DAY> { |
7 | 9 | fn init_test() -> (Self, Data) { |
8 | 10 | Self::init(include_str!("test_inputs/test04.txt")) |
9 | 11 | } |
10 | 12 |
|
11 | 13 | fn expected_results() -> (Answer, Answer) { |
12 | | - (Answer::Number(0), Answer::Number(0)) |
| 14 | + (Answer::Number(13), Answer::Number(0)) |
13 | 15 | } |
14 | 16 |
|
15 | 17 | fn init(input: &str) -> (Self, Data) { |
16 | 18 | ( |
17 | 19 | Self {}, |
18 | 20 | input |
19 | 21 | .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 | + }) |
21 | 31 | .collect(), |
22 | 32 | ) |
23 | 33 | } |
24 | 34 |
|
25 | 35 | 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) |
27 | 72 | } |
28 | 73 |
|
29 | 74 | fn two(&self, data: &mut Data) -> Answer { |
|
0 commit comments