day6
This commit is contained in:
129
src/day6.rs
Normal file
129
src/day6.rs
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#[aoc(day6, part1)]
|
||||||
|
pub fn part1(input: &str) -> u64 {
|
||||||
|
let (nums_rows, ops) = parse_input(input);
|
||||||
|
let nums_cols = transpose_nums(&nums_rows);
|
||||||
|
ops.iter()
|
||||||
|
.zip(nums_cols)
|
||||||
|
.map(|(op, nums)| match op {
|
||||||
|
Operator::Add => nums.iter().sum(),
|
||||||
|
Operator::Mult => nums.iter().fold(1, |x, y| x * y),
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day6, part2)]
|
||||||
|
pub fn part2(input: &str) -> u64 {
|
||||||
|
let (nums, ops) = parse_input_2(input);
|
||||||
|
ops.iter()
|
||||||
|
.zip(nums)
|
||||||
|
.map(|(op, nums)| match op {
|
||||||
|
Operator::Add => nums.iter().sum(),
|
||||||
|
Operator::Mult => nums.iter().fold(1, |x, y| x * y),
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Operator {
|
||||||
|
Add,
|
||||||
|
Mult,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> (Vec<Vec<u64>>, Vec<Operator>) {
|
||||||
|
(
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.take_while(|l| l.trim_ascii_start().chars().next().unwrap().is_digit(10))
|
||||||
|
.map(|l| {
|
||||||
|
{
|
||||||
|
l.split_ascii_whitespace()
|
||||||
|
.map(|x| x.parse::<u64>().unwrap())
|
||||||
|
.collect::<Vec<u64>>()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.last()
|
||||||
|
.unwrap()
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|x| match x {
|
||||||
|
"+" => Operator::Add,
|
||||||
|
"*" => Operator::Mult,
|
||||||
|
_ => panic!("Undefined operation {}", x),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input_2(input: &str) -> (Vec<Vec<u64>>, Vec<Operator>) {
|
||||||
|
let n = input.lines().count() - 1;
|
||||||
|
let m_chars = input.lines().next().unwrap().chars().count();
|
||||||
|
let mut char_cols = vec![Vec::with_capacity(n); m_chars];
|
||||||
|
|
||||||
|
for (i, line) in input.lines().take(n).enumerate() {
|
||||||
|
for (j, char) in line.chars().enumerate() {
|
||||||
|
char_cols[j].insert(i, char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ops: Vec<Operator> = input
|
||||||
|
.lines()
|
||||||
|
.last()
|
||||||
|
.unwrap()
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|x| match x {
|
||||||
|
"+" => Operator::Add,
|
||||||
|
"*" => Operator::Mult,
|
||||||
|
_ => panic!("Undefined operation {}", x),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut nums: Vec<Vec<u64>> = vec![Vec::new(); ops.len()];
|
||||||
|
|
||||||
|
let mut op_counter = 0;
|
||||||
|
for col in char_cols {
|
||||||
|
let numstr = col.iter().collect::<String>();
|
||||||
|
if numstr.trim().is_empty() {
|
||||||
|
op_counter = op_counter + 1;
|
||||||
|
} else {
|
||||||
|
nums[op_counter].push(numstr.trim().parse::<u64>().unwrap_or(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(ops.len(), nums.len());
|
||||||
|
(nums, ops)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transpose_nums(nums: &Vec<Vec<u64>>) -> Vec<Vec<u64>> {
|
||||||
|
let n = nums.len();
|
||||||
|
let m = nums[0].len();
|
||||||
|
let mut columns = vec![Vec::with_capacity(n); m];
|
||||||
|
for (i, line) in nums.iter().enumerate() {
|
||||||
|
for (j, num) in line.iter().enumerate() {
|
||||||
|
columns[j].insert(i, *num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
columns
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const EXAMPLE_INPUT: &str = "\
|
||||||
|
123 328 51 64
|
||||||
|
45 64 387 23
|
||||||
|
6 98 215 314
|
||||||
|
* + * +
|
||||||
|
";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1() {
|
||||||
|
assert_eq!(part1(EXAMPLE_INPUT), 4277556);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2() {
|
||||||
|
assert_eq!(part2(EXAMPLE_INPUT), 3263827);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ extern crate aoc_runner_derive;
|
|||||||
// pub mod day2;
|
// pub mod day2;
|
||||||
// pub mod day3;
|
// pub mod day3;
|
||||||
// pub mod day4;
|
// pub mod day4;
|
||||||
pub mod day5;
|
// pub mod day5;
|
||||||
|
pub mod day6;
|
||||||
|
|
||||||
aoc_lib! { year = 2025 }
|
aoc_lib! { year = 2025 }
|
||||||
|
|||||||
Reference in New Issue
Block a user