day22: improvements
Some checks failed
test / AoC 2024 (push) Failing after 6m37s

This commit is contained in:
Keenan Tims 2024-12-21 23:45:05 -08:00
parent 40d5e820bc
commit c5857ed449
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -1,4 +1,4 @@
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::aoc;
use itertools::Itertools; use itertools::Itertools;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
@ -31,7 +31,7 @@ fn prices(mut secret: i64, n: i64) -> Vec<i8> {
prices prices
} }
fn changes(prices: &Vec<i8>) -> Vec<Change> { fn changes(prices: &[i8]) -> Vec<Change> {
prices prices
.windows(2) .windows(2)
.map(|a| Change { .map(|a| Change {
@ -45,14 +45,10 @@ fn profit_for_sequence(changes: &Vec<Vec<Change>>, seq: &[i8]) -> i64 {
changes changes
.par_iter() .par_iter()
.filter_map(|inner| { .filter_map(|inner| {
if let Some(buy) = inner inner
.windows(seq.len()) .windows(seq.len())
.find(|window| window.iter().zip(seq).all(|(w, z)| w.delta == *z)) .find(|window| window.iter().zip(seq).all(|(w, z)| w.delta == *z))
{ .map(|buy| buy[seq.len() - 1].price as i64)
Some(buy[seq.len() - 1].price as i64)
} else {
None
}
}) })
.sum() .sum()
} }
@ -60,7 +56,7 @@ fn profit_for_sequence(changes: &Vec<Vec<Change>>, seq: &[i8]) -> i64 {
fn find_best_sequence(changes: &Vec<Vec<Change>>) -> [i8; 4] { fn find_best_sequence(changes: &Vec<Vec<Change>>) -> [i8; 4] {
let mut best_seq = [0, 0, 0, 0]; let mut best_seq = [0, 0, 0, 0];
let mut best_profit = 0; let mut best_profit = 0;
for seq in (0..4).map(|_| (-9..=9 as i8)).multi_cartesian_product() { for seq in (0..4).map(|_| (-9..=9i8)).multi_cartesian_product() {
let profit = profit_for_sequence(changes, &seq); let profit = profit_for_sequence(changes, &seq);
if profit > best_profit { if profit > best_profit {
best_seq = seq.try_into().unwrap(); best_seq = seq.try_into().unwrap();
@ -75,14 +71,14 @@ fn parse(input: &str) -> Vec<i64> {
} }
#[aoc(day22, part1)] #[aoc(day22, part1)]
fn part1(input: &str) -> i64 { pub fn part1(input: &str) -> i64 {
let secrets = parse(input); let secrets = parse(input);
secrets.iter().map(|s| rounds(*s, 2000)).sum::<i64>() secrets.iter().map(|s| rounds(*s, 2000)).sum::<i64>()
} }
#[aoc(day22, part2)] #[aoc(day22, part2)]
fn part2(input: &str) -> i64 { pub fn part2(input: &str) -> i64 {
let secrets = parse(input); let secrets = parse(input);
let price_changes = secrets.iter().map(|s| changes(&prices(*s, 2000))).collect_vec(); let price_changes = secrets.iter().map(|s| changes(&prices(*s, 2000))).collect_vec();
@ -132,10 +128,7 @@ mod tests {
let secrets = parse(EXAMPLE2); let secrets = parse(EXAMPLE2);
let price_changes = secrets.iter().map(|s| changes(&prices(*s, 2000))).collect_vec(); let price_changes = secrets.iter().map(|s| changes(&prices(*s, 2000))).collect_vec();
assert_eq!( assert_eq!(profit_for_sequence(&price_changes, &[-2, 1, -1, 3]), 23);
profit_for_sequence(&price_changes, &[-2, 1, -1, 3]),
23
);
} }
#[test] #[test]