Skip to content
This repository was archived by the owner on Aug 16, 2025. It is now read-only.

Commit 88a6b11

Browse files
committed
Add Day 25
1 parent d53eaba commit 88a6b11

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

aoc25/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aoc25/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "aoc25"
3+
version = "0.1.0"
4+
authors = ["Sergree <[email protected]>"]
5+
license = "MIT OR Apache-2.0"
6+
edition = "2018"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
anyhow = "1.0.40"

aoc25/input/input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
15335876
2+
15086442

aoc25/src/main.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use anyhow::Result;
2+
use std::io::{self, Error, ErrorKind, Read, Write};
3+
4+
const DIVISOR: u64 = 20201227;
5+
const SUBJECT_NUMBER: u64 = 7;
6+
7+
fn main() -> Result<()> {
8+
let mut input = String::new();
9+
io::stdin().read_to_string(&mut input)?;
10+
11+
part1(&input)?;
12+
Ok(())
13+
}
14+
15+
fn part1(input: &str) -> Result<()> {
16+
let mut input_iter = input.lines().map(|l| l.parse());
17+
18+
let card_pub_key = input_iter
19+
.next()
20+
.ok_or_else(|| Error::from(ErrorKind::InvalidData))??;
21+
let door_pub_key = input_iter
22+
.next()
23+
.ok_or_else(|| Error::from(ErrorKind::InvalidData))??;
24+
25+
let mut value = 1;
26+
let mut loop_size = 0;
27+
while value != card_pub_key {
28+
value = value * SUBJECT_NUMBER % DIVISOR;
29+
loop_size += 1;
30+
}
31+
32+
writeln!(
33+
io::stdout(),
34+
"{}",
35+
mod_pow(door_pub_key, loop_size, DIVISOR)
36+
)?;
37+
Ok(())
38+
}
39+
40+
/// https://en.wikipedia.org/wiki/Modular_exponentiation
41+
fn mod_pow(mut base: u64, mut exp: u64, modulus: u64) -> u64 {
42+
if modulus == 1 {
43+
return 0;
44+
}
45+
let mut result = 1;
46+
base %= modulus;
47+
while exp > 0 {
48+
if exp % 2 == 1 {
49+
result = result * base % modulus;
50+
}
51+
exp >>= 1;
52+
base = base * base % modulus
53+
}
54+
result
55+
}

0 commit comments

Comments
 (0)