Adds Day 7
This commit is contained in:
111
src/days/day7.rs
Normal file
111
src/days/day7.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use std::path::Path;
|
||||
|
||||
use advent_derive::advent_day;
|
||||
|
||||
use crate::{error::AdventError, input::get_lines};
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Equation {
|
||||
result: u64,
|
||||
operands: Vec<u64>
|
||||
}
|
||||
|
||||
struct Day7Part1 {
|
||||
equations: Vec<Equation>
|
||||
}
|
||||
|
||||
impl Day7Part1 {
|
||||
fn new() -> Self {
|
||||
Self{
|
||||
equations: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Day7Part2 {
|
||||
equations: Vec<Equation>
|
||||
}
|
||||
|
||||
impl Day7Part2 {
|
||||
fn new() -> Self {
|
||||
Self{
|
||||
equations: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input_part1(part1: &mut Day7Part1, path: &Path) -> Result<(), AdventError> {
|
||||
for line in get_lines(path) {
|
||||
let line = line?;
|
||||
let mut split = line.split(": ");
|
||||
let result = split.next().unwrap().parse::<u64>()?;
|
||||
let mut operands = vec![];
|
||||
for operand in split.next().unwrap().split(" ").into_iter() {
|
||||
operands.push(operand.parse::<u64>()?);
|
||||
}
|
||||
part1.equations.push(Equation { result, operands});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn possible(equation: &&Equation) -> bool {
|
||||
let possible_answers = equation.operands.iter().fold(vec![0u64], |acc, operand| {
|
||||
acc.iter().chain(acc.iter()).enumerate().map(|(index, part)| {
|
||||
if acc.len() == 1 {
|
||||
*operand
|
||||
} else if index < acc.len() {
|
||||
part + operand
|
||||
} else {
|
||||
part * operand
|
||||
}
|
||||
}).collect::<Vec<u64>>()
|
||||
});
|
||||
possible_answers.contains(&equation.result)
|
||||
}
|
||||
|
||||
fn solve_part1(part1: &mut Day7Part1) -> Result<u64, AdventError> {
|
||||
Ok(part1.equations.iter().filter(possible).fold(0, |acc, equation| {
|
||||
acc + equation.result
|
||||
}))
|
||||
}
|
||||
|
||||
fn read_input_part2(part2: &mut Day7Part2, path: &Path) -> Result<(), AdventError> {
|
||||
for line in get_lines(path) {
|
||||
let line = line?;
|
||||
let mut split = line.split(": ");
|
||||
let result = split.next().unwrap().parse::<u64>()?;
|
||||
let mut operands = vec![];
|
||||
for operand in split.next().unwrap().split(" ").into_iter() {
|
||||
operands.push(operand.parse::<u64>()?);
|
||||
}
|
||||
part2.equations.push(Equation { result, operands});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn possible2(equation: &&Equation) -> bool {
|
||||
let possible_answers = equation.operands.iter().fold(vec![0u64], |acc, operand| {
|
||||
acc.iter().chain(acc.iter()).chain(acc.iter()).enumerate().map(|(index, part)| {
|
||||
if acc.len() == 1 {
|
||||
*operand
|
||||
} else if index < acc.len() {
|
||||
part + operand
|
||||
} else if index < acc.len() * 2 {
|
||||
part * operand
|
||||
} else {
|
||||
part * 10_u64.pow(operand.ilog10() + 1) + operand
|
||||
}
|
||||
}).collect::<Vec<u64>>()
|
||||
});
|
||||
possible_answers.contains(&equation.result)
|
||||
}
|
||||
|
||||
fn solve_part2(part2: &mut Day7Part2) -> Result<u64, AdventError> {
|
||||
Ok(part2.equations.iter().filter(possible2).fold(0, |acc, equation| {
|
||||
acc + equation.result
|
||||
}))
|
||||
}
|
||||
|
||||
#[advent_day(Day7Part1, Day7Part2)]
|
||||
struct Day7;
|
||||
@@ -21,6 +21,7 @@ pub mod day3;
|
||||
pub mod day4;
|
||||
pub mod day5;
|
||||
pub mod day6;
|
||||
pub mod day7;
|
||||
|
||||
pub mod day14;
|
||||
|
||||
|
||||
@@ -5,12 +5,13 @@ mod input;
|
||||
|
||||
use std::env;
|
||||
use day1::Day1;
|
||||
use day14::Day14;
|
||||
use day2::Day2;
|
||||
use day3::Day3;
|
||||
use day4::Day4;
|
||||
use day5::Day5;
|
||||
use day6::Day6;
|
||||
use day7::Day7;
|
||||
use day14::Day14;
|
||||
|
||||
use crate::error::AdventError;
|
||||
use crate::days::*;
|
||||
@@ -44,7 +45,7 @@ fn main() -> Result<(), AdventError> {
|
||||
Box::new(Day4::new()),
|
||||
Box::new(Day5::new()),
|
||||
Box::new(Day6::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(Day7::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
Box::new(DummyDay::new()),
|
||||
|
||||
Reference in New Issue
Block a user