|
1 | 1 | //! [Day 22: Slam Shuffle](https://adventofcode.com/2019/day/22) |
2 | 2 |
|
3 | | -#![allow(clippy::unreadable_literal)] |
| 3 | +// #![allow(clippy::unreadable_literal)] |
4 | 4 |
|
5 | 5 | // Calculates (n^x) % p |
6 | | -fn modular_exponent(mut n: i128, mut x: i128, p: i128) -> i128 { |
| 6 | +const fn modular_exponent(mut n: i128, mut x: i128, p: i128) -> i128 { |
7 | 7 | let mut ans = 1; |
8 | 8 | if x <= 0 { |
9 | 9 | return 1; |
@@ -59,14 +59,14 @@ struct Congruence { |
59 | 59 | impl Congruence { |
60 | 60 | fn compose(&self, lhs: &Self) -> Self { |
61 | 61 | assert_eq!(self.m, lhs.m); |
62 | | - Congruence { |
| 62 | + Self { |
63 | 63 | a: (self.a * lhs.a) % self.m, |
64 | 64 | c: (self.c * lhs.a + lhs.c) % self.m, |
65 | 65 | m: self.m, |
66 | 66 | } |
67 | 67 | } |
68 | 68 |
|
69 | | - fn value(&self, index: i128) -> i128 { |
| 69 | + const fn value(&self, index: i128) -> i128 { |
70 | 70 | (self.a * index + self.c) % self.m |
71 | 71 | } |
72 | 72 |
|
@@ -129,8 +129,8 @@ struct Puzzle { |
129 | 129 | } |
130 | 130 |
|
131 | 131 | impl Puzzle { |
132 | | - fn new() -> Puzzle { |
133 | | - Puzzle { shuffles: vec![] } |
| 132 | + const fn new() -> Self { |
| 133 | + Self { shuffles: vec![] } |
134 | 134 | } |
135 | 135 |
|
136 | 136 | /// Get the puzzle input. |
@@ -196,12 +196,12 @@ impl Puzzle { |
196 | 196 |
|
197 | 197 | /// Solve part two. |
198 | 198 | fn part2(&self) -> i128 { |
199 | | - let m = 119315717514047; |
| 199 | + let m = 119_315_717_514_047; |
200 | 200 | self.shuffles |
201 | 201 | .iter() |
202 | 202 | .fold(Congruence { a: 1, c: 0, m }, |acc, t| acc.compose(&t.op(m))) |
203 | 203 | .inv() |
204 | | - .pow(101741582076661) |
| 204 | + .pow(101_741_582_076_661) |
205 | 205 | .value(2020) |
206 | 206 | } |
207 | 207 | } |
|
0 commit comments