From 0091045fb2405f841decd4c34003d337193717af Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 20 Dec 2022 22:39:21 +0100 Subject: [PATCH] add 2022/20/2 --- 2022/src/bin/day20.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/2022/src/bin/day20.rs b/2022/src/bin/day20.rs index 47c1dff..2bae073 100644 --- a/2022/src/bin/day20.rs +++ b/2022/src/bin/day20.rs @@ -4,14 +4,14 @@ use aoc2022::{boilerplate, common::*}; use std::{collections::VecDeque, iter::repeat}; const DAY: usize = 20; -type Parsed = VecDeque<(isize, bool)>; +type Parsed = Vec; fn parse_input(raw: &str) -> Parsed { - raw.lines().map(|l| l.parse().unwrap()).zip(repeat(false)).collect() + raw.lines().map(|l| l.parse().unwrap()).collect() } fn part1(parsed: &Parsed) -> isize { - let mut xs = parsed.to_owned(); + let mut xs: VecDeque<_> = parsed.iter().copied().zip(repeat(false)).collect(); let mut moved = 0; while moved < parsed.len() { let x = xs.pop_front().unwrap(); @@ -33,8 +33,32 @@ fn part1(parsed: &Parsed) -> isize { a + b + c } -fn part2(parsed: &Parsed) -> usize { - unimplemented!() +const DECRYPTION_KEY: isize = 811589153; + +fn part2(parsed: &Parsed) -> isize { + let mut xs: VecDeque<_> = parsed.iter().enumerate().map(|(p, x)| (p, x * DECRYPTION_KEY)).collect(); + for i in 0..10 { + dbg!(i, &xs); + let mut moved = 0; + while moved < parsed.len() { + let i = xs.iter().position(|(x, _)| x == &moved).unwrap(); + xs.rotate_left(i); + let x = xs.pop_front().unwrap(); + let idx = ((x.1 + (xs.len() * 2 * DECRYPTION_KEY as usize) as isize) % xs.len() as isize) as usize; + dbg!("Inserting at", idx); + xs.insert(idx, x); + moved += 1; + } + } + let i = xs.iter().position(|(_, x)| x == &0).unwrap(); + xs.rotate_left(i); + xs.rotate_left(1000 % parsed.len()); + let a = xs.front().unwrap().1; + xs.rotate_left(1000 % parsed.len()); + let b = xs.front().unwrap().1; + xs.rotate_left(1000 % parsed.len()); + let c = xs.front().unwrap().1; + a + b + c } boilerplate! { @@ -47,9 +71,9 @@ boilerplate! { 4", tests: { part1: { TEST_INPUT => 3 }, - part2: { TEST_INPUT => 0 }, + part2: { TEST_INPUT => 1623178306 }, }, bench1 == 23321, - bench2 == 0, - bench_parse: VecDeque::len => 5000, + bench2 == 1428396909280, + bench_parse: Vec::len => 5000, }