51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
//! Day 03 of 2020.
|
|
|
|
use crate::prelude::*;
|
|
|
|
/// Get the solution for day 03 of 2020.
|
|
pub fn solution() -> Solution {
|
|
Solution::new(Day::new(3, 2020), part_1, part_2)
|
|
.with_expected(198, 5140884672_i64)
|
|
}
|
|
|
|
/// Generic solver that takes in a horizontal and vertical movement for the
|
|
/// slope.
|
|
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
|
|
}
|
|
|
|
/// The logic to solve part one.
|
|
fn part_1(input: &str) -> Result<String> {
|
|
Ok(solve(input, (3, 1)).to_string())
|
|
}
|
|
|
|
/// The logic to solve part two.
|
|
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(),
|
|
)
|
|
}
|