generated from luke-else/AdventOfCodeXXXX
Some checks failed
Continuous integration / Check (push) Successful in 38s
Continuous integration / Test Suite (push) Successful in 39s
Continuous integration / Rustfmt (push) Successful in 28s
Continuous integration / Clippy (push) Failing after 39s
Continuous integration / build (push) Successful in 38s
105 lines
2.5 KiB
Rust
105 lines
2.5 KiB
Rust
use super::Solution;
|
|
|
|
pub struct Day07 {}
|
|
|
|
impl Solution for Day07 {
|
|
fn part1(
|
|
&self,
|
|
input: &mut Vec<String>,
|
|
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
|
let (p1, _p2) = self.solve_paths(input);
|
|
Ok(Box::new(p1))
|
|
}
|
|
|
|
fn part2(
|
|
&self,
|
|
input: &mut Vec<String>,
|
|
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
|
let (_p1, p2) = self.solve_paths(input);
|
|
Ok(Box::new(p2))
|
|
}
|
|
|
|
fn get_day(&self) -> u8 {
|
|
7
|
|
}
|
|
}
|
|
|
|
impl Day07 {
|
|
fn solve_paths(&self, input: &Vec<String>) -> (usize, usize) {
|
|
let lines: Vec<_> = input.iter().map(String::as_bytes).collect();
|
|
let width = lines[0].len();
|
|
let start = lines[0].iter().position(|&b| b == b'S').unwrap();
|
|
|
|
let mut splits = 0;
|
|
let mut current = vec![0; width];
|
|
let mut next = vec![0; width];
|
|
|
|
current[start] = 1;
|
|
|
|
for row in lines {
|
|
for (i, &count) in current.iter().enumerate() {
|
|
if count > 0 {
|
|
if row[i] == b'^' {
|
|
splits += 1;
|
|
|
|
if i > 0 {
|
|
next[i - 1] += count;
|
|
}
|
|
if i < width - 1 {
|
|
next[i + 1] += count;
|
|
}
|
|
} else {
|
|
next[i] += count;
|
|
}
|
|
}
|
|
}
|
|
|
|
(current, next) = (next, current);
|
|
next.fill(0);
|
|
}
|
|
|
|
(splits, current.iter().sum())
|
|
}
|
|
}
|
|
|
|
/// Test from puzzle input
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use crate::*;
|
|
|
|
#[test]
|
|
fn part1() {
|
|
let challenge = day07::Day07 {};
|
|
|
|
//Complete the Challenge
|
|
let answer = challenge
|
|
.part1(
|
|
utils::get_input(challenge.get_day(), utils::InputType::Test1)
|
|
.unwrap()
|
|
.as_mut(),
|
|
)
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
assert_eq!(answer, "21");
|
|
}
|
|
|
|
#[test]
|
|
fn part2() {
|
|
let challenge = day07::Day07 {};
|
|
|
|
//Complete the Challenge
|
|
let answer = challenge
|
|
.part2(
|
|
utils::get_input(challenge.get_day(), utils::InputType::Test2)
|
|
.unwrap()
|
|
.as_mut(),
|
|
)
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
assert_eq!(answer, "40");
|
|
}
|
|
}
|