day 22 task 1

This commit is contained in:
Johannes
2020-01-11 12:09:23 +01:00
parent d18cfca5ed
commit 949b405872
6 changed files with 161 additions and 1 deletions

100
input/day22.txt Normal file
View File

@@ -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

View File

@@ -1,5 +1,5 @@
mod tasks; mod tasks;
fn main() { fn main() {
tasks::day18::run(); tasks::day22::run();
} }

View File

@@ -80,6 +80,7 @@ struct StateSummaryDist(Pos, HashSet<char>, usize);
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
struct Map(Vec<Vec<char>>); struct Map(Vec<Vec<char>>);
#[allow(dead_code)]
impl Map { impl Map {
fn coordinate_of(&self, symbol: char) -> Vec<Pos> { fn coordinate_of(&self, symbol: char) -> Vec<Pos> {
self.0 self.0

57
src/tasks/day22.rs Normal file
View File

@@ -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<u16>;
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::<Vec<_>>();
let n = s[1].parse::<i32>().unwrap();
deck = cut_cards(deck, n);
} else if line.starts_with("deal with increment") {
let s = line.split("deal with increment ").collect::<Vec<_>>();
let n = s[1].parse::<usize>().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()
}

View File

@@ -325,6 +325,7 @@ impl Pos {
} }
} }
#[allow(unused)]
mod test { mod test {
use super::Field; use super::Field;
#[test] #[test]

View File

@@ -16,4 +16,5 @@ pub mod day16;
pub mod day17; pub mod day17;
pub mod day18; pub mod day18;
pub mod day21; pub mod day21;
pub mod day22;
pub mod day24; pub mod day24;