Skip to main content

year2015/
day25.rs

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