2020: day25: part 1
This commit is contained in:
parent
fda311a7ee
commit
7249045d0c
|
@ -24,6 +24,7 @@ use aoc2020::day21;
|
||||||
use aoc2020::day22;
|
use aoc2020::day22;
|
||||||
use aoc2020::day23;
|
use aoc2020::day23;
|
||||||
use aoc2020::day24;
|
use aoc2020::day24;
|
||||||
|
use aoc2020::day25;
|
||||||
|
|
||||||
fn aoc2020_all(c: &mut Criterion) {
|
fn aoc2020_all(c: &mut Criterion) {
|
||||||
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
|
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
|
||||||
|
@ -50,6 +51,7 @@ fn aoc2020_all(c: &mut Criterion) {
|
||||||
c.bench_function("day22", |b| b.iter(|| day22::run().unwrap()));
|
c.bench_function("day22", |b| b.iter(|| day22::run().unwrap()));
|
||||||
c.bench_function("day23", |b| b.iter(|| day23::run().unwrap()));
|
c.bench_function("day23", |b| b.iter(|| day23::run().unwrap()));
|
||||||
c.bench_function("day24", |b| b.iter(|| day24::run().unwrap()));
|
c.bench_function("day24", |b| b.iter(|| day24::run().unwrap()));
|
||||||
|
c.bench_function("day25", |b| b.iter(|| day25::run().unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group! {
|
criterion_group! {
|
||||||
|
|
2
aoc2020/input/day25.txt
Normal file
2
aoc2020/input/day25.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
12090988
|
||||||
|
240583
|
2
aoc2020/input/day25_provided.txt
Normal file
2
aoc2020/input/day25_provided.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
5764801
|
||||||
|
17807724
|
77
aoc2020/src/day25.rs
Normal file
77
aoc2020/src/day25.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../input/day25.txt");
|
||||||
|
|
||||||
|
pub fn run() -> Result<String> {
|
||||||
|
let mut res = String::with_capacity(128);
|
||||||
|
|
||||||
|
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
const PUB_KEY_SUBJECT: u64 = 7;
|
||||||
|
|
||||||
|
// - Set the value to itself multiplied by the subject number.
|
||||||
|
// - Set the value to the remainder after dividing the value by 20201227.
|
||||||
|
fn transform(mut value: u64, subject: u64) -> u64 {
|
||||||
|
value *= subject;
|
||||||
|
|
||||||
|
value % 20201227
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_loop_size(pub_key: u64) -> usize {
|
||||||
|
let mut value = 1;
|
||||||
|
for i in 1.. {
|
||||||
|
value = transform(value, PUB_KEY_SUBJECT);
|
||||||
|
|
||||||
|
if value == pub_key {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> Result<u64> {
|
||||||
|
let mut lines = input.lines();
|
||||||
|
|
||||||
|
let card_pub_key: u64 = lines
|
||||||
|
.next()
|
||||||
|
.context("no public key found for card")?
|
||||||
|
.parse()?;
|
||||||
|
let door_pub_key: u64 = lines
|
||||||
|
.next()
|
||||||
|
.context("no public key found for door")?
|
||||||
|
.parse()?;
|
||||||
|
|
||||||
|
let card_loop_size = find_loop_size(card_pub_key);
|
||||||
|
|
||||||
|
let mut encryption_key = 1;
|
||||||
|
for _ in 0..card_loop_size {
|
||||||
|
encryption_key = transform(encryption_key, door_pub_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(encryption_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const PROVIDED: &str = include_str!("../input/day25_provided.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_provided() {
|
||||||
|
assert_eq!(find_loop_size(5764801), 8);
|
||||||
|
assert_eq!(find_loop_size(17807724), 11);
|
||||||
|
assert_eq!(part1(PROVIDED).unwrap(), 14897079);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_real() {
|
||||||
|
assert_eq!(part1(INPUT).unwrap(), 3015200);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,3 +24,4 @@ pub mod day21;
|
||||||
pub mod day22;
|
pub mod day22;
|
||||||
pub mod day23;
|
pub mod day23;
|
||||||
pub mod day24;
|
pub mod day24;
|
||||||
|
pub mod day25;
|
||||||
|
|
|
@ -26,6 +26,7 @@ use aoc2020::day21;
|
||||||
use aoc2020::day22;
|
use aoc2020::day22;
|
||||||
use aoc2020::day23;
|
use aoc2020::day23;
|
||||||
use aoc2020::day24;
|
use aoc2020::day24;
|
||||||
|
use aoc2020::day25;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[DayFunc] = &[
|
let days: &[DayFunc] = &[
|
||||||
|
@ -53,6 +54,7 @@ fn main() -> Result<()> {
|
||||||
day22::run,
|
day22::run,
|
||||||
day23::run,
|
day23::run,
|
||||||
day24::run,
|
day24::run,
|
||||||
|
day25::run,
|
||||||
];
|
];
|
||||||
|
|
||||||
aoc::run(days)
|
aoc::run(days)
|
||||||
|
|
Loading…
Reference in a new issue