diff --git a/input/day22.txt b/input/day22.txt new file mode 100644 index 0000000..d416852 --- /dev/null +++ b/input/day22.txt @@ -0,0 +1,100 @@ +deal with increment 3 +deal into new stack +cut -2846 +deal with increment 33 +cut -8467 +deal into new stack +deal with increment 46 +cut 6752 +deal with increment 63 +deal into new stack +deal with increment 70 +deal into new stack +deal with increment 14 +cut -1804 +deal with increment 68 +cut -4936 +deal with increment 15 +cut -3217 +deal with increment 49 +cut -1694 +deal with increment 58 +cut -6918 +deal with increment 13 +cut -4254 +deal with increment 4 +deal into new stack +cut 5490 +deal into new stack +deal with increment 35 +deal into new stack +deal with increment 7 +cut 854 +deal with increment 46 +cut -8619 +deal with increment 32 +deal into new stack +cut -6319 +deal with increment 31 +cut 1379 +deal with increment 66 +cut -7328 +deal with increment 55 +cut -6326 +deal with increment 10 +deal into new stack +cut 4590 +deal with increment 18 +cut -9588 +deal with increment 5 +cut 3047 +deal with increment 24 +cut -1485 +deal into new stack +deal with increment 53 +cut 5993 +deal with increment 54 +cut -5935 +deal with increment 49 +cut -3349 +deal into new stack +deal with increment 28 +cut -4978 +deal into new stack +deal with increment 30 +cut -1657 +deal with increment 50 +cut 3732 +deal with increment 30 +cut 6838 +deal with increment 30 +deal into new stack +cut -3087 +deal with increment 42 +deal into new stack +deal with increment 68 +cut 3376 +deal with increment 51 +cut -3124 +deal with increment 57 +deal into new stack +cut -158 +deal into new stack +cut -3350 +deal with increment 33 +deal into new stack +cut 3387 +deal with increment 54 +cut 1517 +deal with increment 20 +cut -3981 +deal with increment 64 +cut 6264 +deal with increment 3 +deal into new stack +deal with increment 5 +cut 232 +deal with increment 29 +deal into new stack +cut -5147 +deal with increment 51 diff --git a/src/main.rs b/src/main.rs index 4f6627c..a47eb2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod tasks; fn main() { - tasks::day18::run(); + tasks::day22::run(); } diff --git a/src/tasks/day18.rs b/src/tasks/day18.rs index e4d2bf7..835bdce 100644 --- a/src/tasks/day18.rs +++ b/src/tasks/day18.rs @@ -80,6 +80,7 @@ struct StateSummaryDist(Pos, HashSet, usize); #[derive(PartialEq, Eq, Clone)] struct Map(Vec>); +#[allow(dead_code)] impl Map { fn coordinate_of(&self, symbol: char) -> Vec { self.0 diff --git a/src/tasks/day22.rs b/src/tasks/day22.rs new file mode 100644 index 0000000..0d8276f --- /dev/null +++ b/src/tasks/day22.rs @@ -0,0 +1,57 @@ +use std::collections::VecDeque; + +#[allow(dead_code)] +pub fn run() { + let input = std::fs::read_to_string("input/day22.txt").unwrap(); + task1(input); +} + +type Deck = VecDeque; + +fn task1(input: String) { + let deck_size = 10_007; + let mut deck: Deck = (0..deck_size).collect(); + for line in input.lines() { + if line.starts_with("deal into new stack") { + deck = into_new_stack(deck); + } else if line.starts_with("cut") { + let s = line.split("cut ").collect::>(); + let n = s[1].parse::().unwrap(); + deck = cut_cards(deck, n); + } else if line.starts_with("deal with increment") { + let s = line.split("deal with increment ").collect::>(); + let n = s[1].parse::().unwrap(); + deck = deal_with_increment(deck, n); + } + } + + let pos_2019 = deck + .iter() + .enumerate() + .find_map(|(i, card)| if *card == 2019 { Some(i) } else { None }); + + println!("Result: {:?}", pos_2019); // 4104 too low +} + +fn into_new_stack(deck: Deck) -> Deck { + deck.into_iter().rev().collect() +} + +fn cut_cards(mut deck: Deck, n: i32) -> Deck { + if n > 0 { + deck.rotate_left(n as usize); + } else { + deck.rotate_right(-n as usize); + }; + deck +} + +fn deal_with_increment(deck: Deck, n: usize) -> Deck { + let mut new = vec![0; deck.len()]; + let mut index = 0; + for card in deck.into_iter() { + new[index] = card; + index = (index + n) % new.len(); + } + new.into_iter().collect() +} diff --git a/src/tasks/day24.rs b/src/tasks/day24.rs index 5558d02..2db44d1 100644 --- a/src/tasks/day24.rs +++ b/src/tasks/day24.rs @@ -325,6 +325,7 @@ impl Pos { } } +#[allow(unused)] mod test { use super::Field; #[test] diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index a3b74d3..6d266a2 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -16,4 +16,5 @@ pub mod day16; pub mod day17; pub mod day18; pub mod day21; +pub mod day22; pub mod day24;