year2015/
day25.rs

1use utils::number::mod_pow;
2use utils::prelude::*;
3
4/// Modular exponentiation.
5#[derive(Clone, Debug)]
6pub struct Day25 {
7    row: u64,
8    column: u64,
9}
10
11impl Day25 {
12    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
13        let (row, column) = parser::u64()
14            .with_prefix(
15                "To continue, please consult the code grid in the manual.  Enter the code at row ",
16            )
17            .then(parser::u64().with_prefix(", column ").with_suffix("."))
18            .parse_complete(input)?;
19        Ok(Self { row, column })
20    }
21
22    #[must_use]
23    pub fn part1(&self) -> u64 {
24        let triangle = (self.row + self.column - 2) * (self.row + self.column - 1) / 2;
25        let index = triangle + self.column - 1;
26
27        (20151125 * mod_pow(252533, index, 33554393)) % 33554393
28    }
29
30    #[must_use]
31    pub fn part2(&self) -> &'static str {
32        "🎄"
33    }
34}
35
36examples!(Day25 -> (u64, &'static str) [
37    {
38        input: "To continue, please consult the code grid in the manual.  Enter the code at row 1, column 1.",
39        part1: 20151125,
40    },
41    {
42        input: "To continue, please consult the code grid in the manual.  Enter the code at row 3, column 4.",
43        part1: 7981243,
44    },
45    {
46        input: "To continue, please consult the code grid in the manual.  Enter the code at row 6, column 3.",
47        part1: 25397450,
48    },
49    {
50        input: "To continue, please consult the code grid in the manual.  Enter the code at row 6, column 6.",
51        part1: 27995004,
52    },
53]);