summaryrefslogtreecommitdiff
path: root/src/bin/day_22.rs
blob: cadfac44892265c09a8730520275c0903a23ab81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use cached::cached;
use rpds::rbt_set;
use rpds::vector::Vector;
use rpds::RedBlackTreeMap;
use rpds::RedBlackTreeMapSync;
use std::fmt;
use std::io;
use std::io::prelude::*;
use std::iter;
use std::iter::FromIterator;
use std::process;
use std::str::FromStr;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "Day 22: Slam Shuffle")]
/// Shuffles some cards.
///
/// See https://adventofcode.com/2019/day/22 for details.
struct Opt {
    /// The size of the deck
    deck_size: usize,
    /// At the end, query the position of card
    card: usize,
}

fn main() {
    let stdin = io::stdin();
    let opt = Opt::from_args();

    let instructions = stdin
        .lock()
        .lines()
        .map(|x| exit_on_failed_assertion(x, "Error reading input"))
        .map(|x| exit_on_failed_assertion(x.parse::<Instruction>(), "Parse error"))
        .collect::<Vec<Instruction>>();

    //eprintln!("{:?}", instructions);

    println!(
        "{}",
        Deck::new(opt.deck_size)
            .shuffle(&instructions)
            .find_position(opt.card)
    );
}

fn exit_on_failed_assertion<A, E: std::error::Error>(data: Result<A, E>, message: &str) -> A {
    match data {
        Ok(data) => data,
        Err(e) => {
            eprintln!("{}: {}", message, e);
            process::exit(1);
        }
    }
}

#[derive(Clone)]
struct Deck {
    zero: usize,
    cards: Vector<usize>,
}

impl Deck {
    fn new(deck_size: usize) -> Self {
        Deck {
            zero: 0,
            cards: (0..deck_size).collect(),
        }
    }

    fn shuffle(&self, instructions: &[Instruction]) -> Deck {
        instructions.iter().fold(self.clone(), |deck, instruction| {
            //eprintln!("{} :: {}", deck.zero, deck.cards);
            deck.single_shuffle(instruction)
        })
    }

    fn single_shuffle(&self, instruction: &Instruction) -> Deck {
        match instruction {
            Instruction::DealIntoNewStack => Deck {
                cards: (0..self.cards.len())
                    .map(|i| self.at(self.cards.len() - i - 1))
                    .collect(),
                zero: 0,
            },
            Instruction::Cut(n) => Deck {
                zero: mod_plus(self.zero, *n, self.cards.len()),
                ..self.clone()
            },
            Instruction::ReverseCut(n) => Deck {
                zero: mod_sub(self.zero, *n, self.cards.len()),
                ..self.clone()
            },
            Instruction::DealWithIncrement(n) => Deck {
                cards: (0..self.cards.len())
                    .map(|i| self.at(*layout_after_shift(*n, self.cards.len()).get(&i).unwrap()))
                    .collect(),
                zero: 0,
            },
        }
    }

    fn at(&self, index: usize) -> usize {
        self.cards[mod_plus(index, self.zero, self.cards.len())]
    }

    fn find_position(&self, value: usize) -> usize {
        mod_sub(
            self.cards.iter().position(|x| *x == value).unwrap(),
            self.zero,
            self.cards.len(),
        )
    }
}

fn mod_plus(a: usize, b: usize, modulus: usize) -> usize {
    (a + b) % modulus
}

fn mod_sub(a: usize, b: usize, modulus: usize) -> usize {
    (a + modulus - b) % modulus
}

fn mod_times(a: usize, b: usize, modulus: usize) -> usize {
    (a * b) % modulus
}

cached! {
    LAYOUT_AFTER_SHIFT;
    fn layout_after_shift(n: usize, modulus: usize) -> RedBlackTreeMapSync<usize, usize> = {
        (0..modulus).fold(RedBlackTreeMapSync::new_sync(), |acc, next| {
            acc.insert(mod_times(next, n, modulus), next)
        })
    }
}

#[derive(Debug)]
enum Instruction {
    DealIntoNewStack,
    Cut(usize),
    ReverseCut(usize),
    DealWithIncrement(usize),
}

impl FromStr for Instruction {
    type Err = ParseErr;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.starts_with("deal into new stack") {
            Ok(Instruction::DealIntoNewStack)
        } else if s.starts_with("cut -") {
            s.split(' ')
                .nth(1)
                .map(|val| {
                    val.parse::<isize>()
                        .map_err(|_| ParseErr)
                        .map(|parsed| Instruction::ReverseCut(parsed.abs() as usize))
                })
                .unwrap_or(Err(ParseErr))
        } else if s.starts_with("cut") {
            s.split(' ')
                .nth(1)
                .map(|val| {
                    val.parse::<usize>()
                        .map_err(|_| ParseErr)
                        .map(|parsed| Instruction::Cut(parsed))
                })
                .unwrap_or(Err(ParseErr))
        } else if s.starts_with("deal with increment") {
            s.split(' ')
                .nth(3)
                .map(|val| {
                    val.parse::<usize>()
                        .map_err(|_| ParseErr)
                        .map(|parsed| Instruction::DealWithIncrement(parsed))
                })
                .unwrap_or(Err(ParseErr))
        } else {
            Err(ParseErr)
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct ParseErr;

impl fmt::Display for ParseErr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Error parsing input")
    }
}

impl std::error::Error for ParseErr {}