mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 13:21:34 +01:00
2025/day-06: Solve part 2
This commit is contained in:
parent
5bb9bcc7b3
commit
e3b750544f
1 changed files with 75 additions and 25 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{fmt::Debug, fs};
|
use std::{fmt::Debug, fs, str::FromStr};
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum Operation {
|
enum Operation {
|
||||||
|
|
@ -6,6 +6,18 @@ enum Operation {
|
||||||
Multiply,
|
Multiply,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for Operation {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(input: &str) -> Result<Operation, Self::Err> {
|
||||||
|
match input {
|
||||||
|
"+" => Ok(Operation::Add),
|
||||||
|
"*" => Ok(Operation::Multiply),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Problem {
|
struct Problem {
|
||||||
numbers: Vec<u64>,
|
numbers: Vec<u64>,
|
||||||
|
|
@ -14,46 +26,79 @@ struct Problem {
|
||||||
|
|
||||||
impl Problem {
|
impl Problem {
|
||||||
fn solve(&self) -> u64 {
|
fn solve(&self) -> u64 {
|
||||||
if self.operation == Operation::Add {
|
match self.operation {
|
||||||
self.numbers.iter().sum()
|
Operation::Add => self.numbers.iter().sum(),
|
||||||
} else {
|
Operation::Multiply => self.numbers.iter().product(),
|
||||||
self.numbers.iter().product()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let problems = parse_input(&fs::read_to_string("../../inputs/06.txt").unwrap());
|
let content = &mut fs::read_to_string("../../inputs/06.txt").unwrap();
|
||||||
|
|
||||||
println!("Grand total: {}", grand_total(&problems));
|
println!("Grand total 1: {}", grand_total(&parse_input_v1(content)));
|
||||||
|
println!("Grand total 2: {}", grand_total(&parse_input_v2(content)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(contents: &str) -> Vec<Problem> {
|
fn parse_input_v1(contents: &str) -> Vec<Problem> {
|
||||||
let grid: Vec<Vec<&str>> = contents
|
let grid: Vec<Vec<&str>> = contents
|
||||||
.trim()
|
.trim()
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.split_whitespace().collect())
|
.map(|line| line.split_whitespace().collect())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut list: Vec<Problem> = Vec::new();
|
let operations_row = grid.last().unwrap();
|
||||||
|
let num_cols = grid[0].len();
|
||||||
|
|
||||||
for (col, _) in grid[0].iter().enumerate() {
|
(0..num_cols)
|
||||||
let mut numbers: Vec<u64> = Vec::new();
|
.map(|col_index| {
|
||||||
|
let operation = Operation::from_str(operations_row[col_index]).unwrap();
|
||||||
|
|
||||||
let operation = match grid[grid.len() - 1][col] {
|
let numbers = grid[..grid.len() - 1]
|
||||||
"+" => Operation::Add,
|
.iter()
|
||||||
"*" => Operation::Multiply,
|
.map(|row| row[col_index].parse().unwrap())
|
||||||
_ => panic!("Unsupported Operation"),
|
.collect();
|
||||||
};
|
|
||||||
|
|
||||||
for (row, _) in grid[..grid.len() - 1].iter().enumerate() {
|
Problem { numbers, operation }
|
||||||
numbers.push(grid[row][col].parse().unwrap());
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
list.push(Problem { numbers, operation });
|
fn parse_input_v2(contents: &str) -> Vec<Problem> {
|
||||||
|
let grid: Vec<Vec<u8>> = contents
|
||||||
|
.trim_end_matches('\n')
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.as_bytes().to_vec())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let operations_row = grid.last().unwrap();
|
||||||
|
|
||||||
|
let mut problems = Vec::new();
|
||||||
|
|
||||||
|
let mut numbers = Vec::new();
|
||||||
|
let mut operation = Operation::Add;
|
||||||
|
|
||||||
|
for col in 0..grid[0].len() {
|
||||||
|
if let Ok(op) = Operation::from_str(&(operations_row[col] as char).to_string()) {
|
||||||
|
operation = op;
|
||||||
}
|
}
|
||||||
|
|
||||||
list
|
let column: String = (0..grid.len() - 1)
|
||||||
|
.map(|row| grid[row][col] as char)
|
||||||
|
.filter(|c| !c.is_whitespace())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if column.is_empty() {
|
||||||
|
problems.push(Problem { numbers, operation });
|
||||||
|
numbers = Vec::new();
|
||||||
|
operation = Operation::Add;
|
||||||
|
} else {
|
||||||
|
numbers.push(column.parse().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
problems.push(Problem { numbers, operation });
|
||||||
|
|
||||||
|
problems
|
||||||
}
|
}
|
||||||
|
|
||||||
fn grand_total(problems: &[Problem]) -> u64 {
|
fn grand_total(problems: &[Problem]) -> u64 {
|
||||||
|
|
@ -64,14 +109,19 @@ fn grand_total(problems: &[Problem]) -> u64 {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const TEST_INPUT: &str = "
|
const TEST_INPUT: &str = "123 328 51 64
|
||||||
123 328 51 64
|
|
||||||
45 64 387 23
|
45 64 387 23
|
||||||
6 98 215 314
|
6 98 215 314
|
||||||
* + * + ";
|
* + * +
|
||||||
|
";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() {
|
||||||
assert_eq!(grand_total(&parse_input(TEST_INPUT)), 4277556);
|
assert_eq!(grand_total(&parse_input_v1(TEST_INPUT)), 4277556);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2() {
|
||||||
|
assert_eq!(grand_total(&parse_input_v2(TEST_INPUT)), 3263827);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue