2020: day03: refacto for part 2
This commit is contained in:
parent
a0ec55dcc4
commit
389693dabc
|
@ -13,25 +13,17 @@ pub fn run() -> aoc::Result<String> {
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> aoc::Result<u64> {
|
fn part1(input: &str) -> aoc::Result<usize> {
|
||||||
let forest = input.parse()?;
|
let forest = input.parse()?;
|
||||||
|
|
||||||
Ok(count_trees(forest))
|
Ok(count_trees(&forest, (3, 1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_trees(forest: Forest) -> u64 {
|
fn count_trees(forest: &Forest, (right, down): (usize, usize)) -> usize {
|
||||||
let mut count = 0;
|
let vertical_range = (0..forest.height()).step_by(down);
|
||||||
|
let horizontal_range = (0..).step_by(right);
|
||||||
|
|
||||||
let vertical_range = 0..forest.height();
|
vertical_range.zip(horizontal_range).filter(|(i, j)| forest[*i][*j]).count()
|
||||||
let horizontal_range = (0..).step_by(3);
|
|
||||||
|
|
||||||
for (i, j) in vertical_range.zip(horizontal_range) {
|
|
||||||
if forest[i][j] {
|
|
||||||
count += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -87,14 +79,7 @@ impl FromStr for Forest {
|
||||||
fn from_str(s: &str) -> aoc::Result<Self> {
|
fn from_str(s: &str) -> aoc::Result<Self> {
|
||||||
let trees = s
|
let trees = s
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| {
|
.map(|line| line.chars().map(|c| matches!(c, '#')).collect())
|
||||||
line.chars()
|
|
||||||
.map(|c| match c {
|
|
||||||
'.' => false,
|
|
||||||
_ => true,
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Self { trees })
|
Ok(Self { trees })
|
||||||
|
|
Loading…
Reference in a new issue