diff --git a/2019/22/Cargo.toml b/2019/22/Cargo.toml new file mode 100644 index 0000000..285d766 --- /dev/null +++ b/2019/22/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "d22" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2019/22/input b/2019/22/input new file mode 100644 index 0000000..3323c16 --- /dev/null +++ b/2019/22/input @@ -0,0 +1,100 @@ +cut 1470 +deal with increment 46 +cut -6481 +deal with increment 70 +cut 547 +deal with increment 48 +cut -6479 +deal with increment 69 +cut -5203 +deal with increment 13 +deal into new stack +deal with increment 73 +deal into new stack +cut -6689 +deal with increment 61 +cut -9853 +deal with increment 48 +cut -9673 +deal into new stack +deal with increment 3 +deal into new stack +deal with increment 64 +cut 5789 +deal with increment 66 +deal into new stack +deal with increment 70 +cut -2588 +deal with increment 6 +deal into new stack +deal with increment 6 +cut -7829 +deal with increment 49 +deal into new stack +deal with increment 19 +cut 9777 +deal into new stack +deal with increment 27 +cut 6210 +deal into new stack +deal with increment 12 +cut 6309 +deal with increment 12 +cut -9458 +deal with increment 5 +cut 6369 +deal with increment 27 +cut 2278 +deal with increment 42 +cut 6656 +deal with increment 62 +cut -1424 +deal with increment 25 +deal into new stack +deal with increment 12 +deal into new stack +cut -7399 +deal into new stack +cut -8925 +deal with increment 47 +deal into new stack +cut 5249 +deal with increment 65 +cut -213 +deal into new stack +cut 6426 +deal with increment 22 +cut -6683 +deal with increment 38 +deal into new stack +deal with increment 62 +cut 6855 +deal with increment 75 +cut 4965 +deal into new stack +cut -5792 +deal with increment 30 +cut 9250 +deal with increment 19 +cut -948 +deal with increment 26 +cut -5123 +deal with increment 68 +cut -604 +deal with increment 41 +deal into new stack +deal with increment 45 +cut 5572 +deal into new stack +cut 3853 +deal with increment 21 +cut 1036 +deal into new stack +deal with increment 6 +cut 8114 +deal into new stack +deal with increment 38 +cut -5 +deal with increment 58 +cut 9539 +deal with increment 19 diff --git a/2019/22/src/main.rs b/2019/22/src/main.rs new file mode 100644 index 0000000..905c6fc --- /dev/null +++ b/2019/22/src/main.rs @@ -0,0 +1,96 @@ +use std::io::{self, BufRead}; + +fn apply(deck: &mut Vec, operation: &str) { + let words: Vec<_> = operation.split(' ').collect(); + match words[0] { + "cut" => { + let offset: isize = words[1].parse().expect("Not a valid number for cutting"); + if offset < 0 { + let offset = deck.len() - (offset.abs()) as usize; + let cut: Vec<_> = deck.drain(0..offset).collect(); + cut.into_iter().for_each(|n| deck.push(n)); + } else { + let clone = deck.clone(); + let (left, right) = clone.split_at(offset as usize); + *deck = right.to_vec(); + deck.append(&mut left.to_vec()); + } + } + // deal + "deal" => match words[1] { + // with increment + "with" => { + let length = deck.len(); + let increment: usize = words[3].parse().unwrap(); + let old_deck = deck.clone(); + for i in 0..length { + deck[(i * increment) % length] = old_deck[i]; + } + } + // into new stack + "into" => deck.reverse(), + _ => unreachable!("Unknown deal command"), + }, + _ => unreachable!("Unknown shuffle command"), + }; +} + +const DECK_SIZE: usize = 10007; + +fn main() { + let input: Vec<_> = io::stdin().lock().lines().map(|l| l.unwrap()).collect(); + let mut deck = Vec::with_capacity(DECK_SIZE); + (0..DECK_SIZE).for_each(|n| deck.push(n)); + for line in input { + apply(&mut deck, &line); + } + println!("{}", deck.iter().position(|&x| x == 2019).unwrap()); + assert_eq!(deck.capacity(), 10007); +} + +mod tests { + use super::*; + + #[test] + fn deal_test() { + let mut deck: Vec = (0..10).collect(); + let instructions = "deal with increment 7 +deal into new stack +deal into new stack"; + for line in instructions.lines() { + apply(&mut deck, line); + } + assert_eq!(deck, &[0, 3, 6, 9, 2, 5, 8, 1, 4, 7]); + } + + #[test] + fn cut_deal_test() { + let mut deck: Vec = (0..10).collect(); + let instructions = "cut 6 +deal with increment 7 +deal into new stack"; + for line in instructions.lines() { + apply(&mut deck, line); + } + assert_eq!(deck, &[3, 0, 7, 4, 1, 8, 5, 2, 9, 6]); + } + + #[test] + fn test_all() { + let mut deck: Vec = (0..10).collect(); + let instructions = "deal into new stack +cut -2 +deal with increment 7 +cut 8 +cut -4 +deal with increment 7 +cut 3 +deal with increment 9 +deal with increment 3 +cut -1"; + for line in instructions.lines() { + apply(&mut deck, line); + } + assert_eq!(deck, &[9, 2, 5, 8, 1, 4, 7, 0, 3, 6]); + } +}