Compare commits

...

2 Commits

Author SHA1 Message Date
Johannes
126833bbaa Day 22 part 2. 2022-03-23 20:23:18 +01:00
Johannes
333d99539d Update dependencies. 2022-03-21 21:58:56 +01:00
4 changed files with 126 additions and 50 deletions

82
Cargo.lock generated
View File

@@ -1,100 +1,94 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.6"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr",
]
[[package]]
name = "aoc_2019"
version = "0.1.0"
dependencies = [
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools",
"modinverse",
"num-integer",
"regex",
]
[[package]]
name = "autocfg"
version = "0.1.7"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
[[package]]
name = "itertools"
version = "0.8.2"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"either",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memchr"
version = "2.2.1"
name = "modinverse"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f62f577f148cc1a9466e7065a22e59466a7d537cceba5e77e57181d0f706633"
dependencies = [
"num-integer",
]
[[package]]
name = "num-integer"
version = "0.1.41"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.10"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"autocfg",
]
[[package]]
name = "regex"
version = "1.3.1"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.12"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"

View File

@@ -7,6 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.8.2"
regex = "1.3.1"
num-integer = "0.1"
itertools = "0.10.3"
regex = "1.5.5"
num-integer = "0.1.44"
modinverse = "0.1.1"

View File

@@ -1,14 +1,16 @@
use modinverse::modinverse;
use std::collections::HashMap;
use std::collections::VecDeque;
#[allow(dead_code)]
pub fn run() {
let input = std::fs::read_to_string("input/day22.txt").unwrap();
task1(input);
println!("{}", task2(input, 119315717514047, 2020, 101741582076661));
}
type Deck = VecDeque<u16>;
type Size = i128;
fn task1(input: String) {
fn task1(input: String) -> usize {
let deck_size = 10_007;
let mut deck: Deck = (0..deck_size).collect();
for line in input.lines() {
@@ -31,6 +33,7 @@ fn task1(input: String) {
.find_map(|(i, card)| if *card == 2019 { Some(i) } else { None });
println!("Result: {:?}", pos_2019);
pos_2019.unwrap()
}
fn into_new_stack(deck: Deck) -> Deck {
@@ -55,3 +58,80 @@ fn deal_with_increment(deck: Deck, n: usize) -> Deck {
}
new.into_iter().collect()
}
fn task2(input: String, deck_size: Size, field_after: Size, num_iterations: usize) -> Size {
let shuffles: Vec<String> = input.trim().lines().rev().map(|it| it.into()).collect();
let mut results: HashMap<Size, usize> = HashMap::new();
let mut result = field_after;
results.insert(result, 0);
let mut iteration = 1;
while iteration <= num_iterations {
result = revert_iteration_find_start(&shuffles, deck_size, result);
if let Some(last_iteration) = results.get(&result) {
println!("check after {} iterations", iteration);
let iterations = iteration - last_iteration;
let full_cycles_remaining = (num_iterations - iteration) / iterations;
iteration += full_cycles_remaining * iterations;
} else {
results.insert(result, iteration);
}
iteration += 1;
if iteration % 1000 == 0 {
println!("iteration {}", iteration);
}
}
result
}
fn revert_iteration_find_start(shuffles: &Vec<String>, deck_size: Size, field_after: Size) -> Size {
let mut current: Size = field_after;
for line in shuffles {
if line.starts_with("deal into new stack") {
current = deck_size - 1 - current;
} else if line.starts_with("cut") {
let s = line.split("cut ").collect::<Vec<_>>();
let n = s[1].parse::<Size>().unwrap();
current += n;
current += deck_size;
current %= deck_size;
} else if line.starts_with("deal with increment") {
let s = line.split("deal with increment ").collect::<Vec<_>>();
let n = s[1].parse::<Size>().unwrap();
current = revert_finite_field_multiplikation(deck_size, n, current);
}
}
current
}
fn revert_finite_field_multiplikation(
field_length: Size,
multiplicator: Size,
result: Size,
) -> Size {
let multiplicator_inverse =
modinverse(multiplicator, field_length).expect("no modular inverse found");
(result * multiplicator_inverse) % field_length
}
#[cfg(test)]
mod test {
use crate::tasks::day22::task1;
use crate::tasks::day22::task2;
#[test]
fn part1() {
let input = std::fs::read_to_string("input/day22.txt").unwrap();
assert_eq!(7545, task1(input));
}
#[test]
fn part2() {
let input = std::fs::read_to_string("input/day22.txt").unwrap();
assert_eq!(2019, task2(input, 10007, 7545, 1));
}
}

View File

@@ -16,5 +16,6 @@ pub mod day16;
pub mod day17;
pub mod day18;
pub mod day21;
#[allow(dead_code)]
pub mod day22;
pub mod day24;