mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
simplify code
This commit is contained in:
parent
b25fcbc1a7
commit
213fcdc1d4
@ -2,41 +2,14 @@ advent_of_code_macro::solve_problem!(
|
|||||||
day 1,
|
day 1,
|
||||||
Input Vec<Vec<i32>>,
|
Input Vec<Vec<i32>>,
|
||||||
parse |input: &str| {
|
parse |input: &str| {
|
||||||
let lines = input.split('\n');
|
input.split("\n\n").map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect()).collect()
|
||||||
let mut output = vec![];
|
|
||||||
let mut curr_elf = vec![];
|
|
||||||
for line in lines {
|
|
||||||
if line.is_empty() {
|
|
||||||
output.push(curr_elf);
|
|
||||||
curr_elf = vec![];
|
|
||||||
} else {
|
|
||||||
curr_elf.push(line.parse().unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output
|
|
||||||
},
|
},
|
||||||
part one |data: Input| {
|
part one |data: Input| {
|
||||||
data.iter().map(|cals| cals.iter().sum()).max().unwrap()
|
data.iter().map(|cals| cals.iter().sum()).max().unwrap()
|
||||||
},
|
},
|
||||||
part two |data: Input| {
|
part two |data: Input| {
|
||||||
use std::mem::swap;
|
let mut cals: std::collections::BinaryHeap<i32> = data.iter().map(|cals| cals.iter().sum()).collect();
|
||||||
let cals = data.iter().map(|cals| cals.iter().sum()).fold(
|
cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap()
|
||||||
(-1, -1, -1),
|
|
||||||
|(mut first, mut second, mut third), mut curr: i32| {
|
|
||||||
if curr > first {
|
|
||||||
swap(&mut first, &mut curr);
|
|
||||||
}
|
|
||||||
if curr > second {
|
|
||||||
swap(&mut second, &mut curr);
|
|
||||||
}
|
|
||||||
if curr > third {
|
|
||||||
swap(&mut third, &mut curr);
|
|
||||||
}
|
|
||||||
|
|
||||||
(first, second, third)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
cals.0 + cals.1 + cals.2
|
|
||||||
},
|
},
|
||||||
sample tests [24_000, 45_000],
|
sample tests [24_000, 45_000],
|
||||||
star tests [68_923, 200_044]
|
star tests [68_923, 200_044]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user