1
Fork 0
advent-of-code/source/year_2020/day_03/mod.rs

44 lines
927 B
Rust

use crate::prelude::*;
pub fn solution() -> Solution {
Solution::new(Day::new(3, 2020), part_1, part_2)
.with_expected(198, 5140884672_i64)
}
fn solve(data: &str, (horizontal, vertical): (usize, usize)) -> usize {
let line_length = data.find('\n').unwrap();
let mut result = 0;
let mut x_position = 0;
for (y_position, line) in data.lines().enumerate() {
if y_position % vertical != 0 {
continue;
}
if line.chars().nth(x_position) == Some('#') {
result += 1;
}
x_position += horizontal;
if x_position >= line_length {
x_position -= line_length;
}
}
result
}
fn part_1(input: &str) -> Result<String> {
Ok(solve(input, (3, 1)).to_string())
}
fn part_2(input: &str) -> Result<String> {
Ok(
[(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
.into_iter()
.map(|increment| solve(input, increment))
.product::<usize>()
.to_string(),
)
}