I love large numbers

This commit is contained in:
Tyler Beckman 2023-12-06 07:27:27 -07:00
parent c15d9534bd
commit 92046cd091
Signed by: Ty
GPG key ID: 2813440C772555A4
4 changed files with 109 additions and 126 deletions

201
Cargo.lock generated
View file

@ -22,6 +22,9 @@ name = "advent"
version = "0.1.0"
dependencies = [
"clap",
"num-bigint",
"num-irrational",
"num-rational",
"regex",
"reqwest",
"scraper",
@ -35,7 +38,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a"
dependencies = [
"cfg-if",
"getrandom 0.2.11",
"getrandom",
"once_cell",
"version_check",
"zerocopy",
@ -241,7 +244,7 @@ dependencies = [
"cssparser-macros",
"dtoa-short",
"itoa",
"phf 0.8.0",
"phf",
"smallvec",
]
@ -415,17 +418,6 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.11"
@ -434,7 +426,7 @@ checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasi",
]
[[package]]
@ -653,7 +645,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2629bb1404f3d34c2e921f21fd34ba00b206124c81f65c50b43b6aaefeb016"
dependencies = [
"log",
"phf 0.10.1",
"phf",
"phf_codegen",
"string_cache",
"string_cache_codegen",
@ -688,7 +680,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0"
dependencies = [
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasi",
"windows-sys 0.48.0",
]
@ -716,6 +708,60 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-irrational"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa0f32746f69c782504c52e5baf85243acd5205f83ef6186ffc79a404a62ebdd"
dependencies = [
"num-bigint",
"num-integer",
"num-rational",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
@ -814,24 +860,15 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "phf"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_macros",
"phf_shared 0.8.0",
"proc-macro-hack",
]
[[package]]
name = "phf"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259"
dependencies = [
"phf_shared 0.10.0",
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
@ -840,18 +877,8 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd"
dependencies = [
"phf_generator 0.10.0",
"phf_shared 0.10.0",
]
[[package]]
name = "phf_generator"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526"
dependencies = [
"phf_shared 0.8.0",
"rand 0.7.3",
"phf_generator",
"phf_shared",
]
[[package]]
@ -860,33 +887,24 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared 0.10.0",
"rand 0.8.5",
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6fde18ff429ffc8fe78e2bf7f8b7a5a5a6e2a8b58bc5a9ac69198bbda9189c"
checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0"
dependencies = [
"phf_generator 0.8.0",
"phf_shared 0.8.0",
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "phf_shared"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
dependencies = [
"siphasher",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
@ -950,20 +968,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc",
"rand_pcg",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -971,18 +975,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
"rand_chacha",
"rand_core",
]
[[package]]
@ -992,16 +986,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
"rand_core",
]
[[package]]
@ -1010,25 +995,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom 0.2.11",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_pcg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
dependencies = [
"rand_core 0.5.1",
"getrandom",
]
[[package]]
@ -1198,7 +1165,7 @@ dependencies = [
"fxhash",
"log",
"new_debug_unreachable",
"phf 0.10.1",
"phf",
"phf_codegen",
"precomputed-hash",
"servo_arc",
@ -1313,7 +1280,7 @@ dependencies = [
"new_debug_unreachable",
"once_cell",
"parking_lot",
"phf_shared 0.10.0",
"phf_shared",
"precomputed-hash",
"serde",
]
@ -1324,8 +1291,8 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bb30289b722be4ff74a408c3cc27edeaad656e06cb1fe8fa9231fa59c728988"
dependencies = [
"phf_generator 0.10.0",
"phf_shared 0.10.0",
"phf_generator",
"phf_shared",
"proc-macro2",
"quote",
]
@ -1572,12 +1539,6 @@ dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View file

@ -7,6 +7,9 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.10", features = ["derive"] }
num-bigint = "0.4.4"
num-irrational = { version = "0.3.0", features = ["num-bigint"] }
num-rational = { version = "0.4.1", features = ["num-bigint"] }
regex = "1.10.2"
reqwest = "0.11.22"
scraper = "0.18.1"

View file

@ -1,3 +1,8 @@
use std::str::FromStr;
use num_bigint::BigInt;
use num_irrational::QuadraticSurd;
use crate::utils::Day;
#[derive(Debug, Default)]
@ -36,7 +41,6 @@ impl Day for Day6 {
fn part2(&mut self) -> String {
let lines = self.input.lines().map(|l| l[10..].split_ascii_whitespace().collect::<String>()).collect::<Vec<_>>();
let (time, record_distance): (f64, f64) = (lines[0].parse().unwrap(), lines[1].parse().unwrap());
// Boring solution:
// let mut wins = 0u128;
// for time_holding_button in 0..time {
@ -46,9 +50,18 @@ impl Day for Day6 {
// wins.to_string()
// Mathy solution:
let b = ((time+((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
let a = ((time-((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
// let b = ((time+((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
// let a = ((time-((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
(b - a).to_string()
// (b - a).to_string()
// Arbitrary precision solution:
let lower = QuadraticSurd::from_equation(
Into::<BigInt>::into(-1).into(),
BigInt::from_str(&lines[0]).unwrap().into(),
BigInt::from_str(&("-".to_string() + &lines[1])).unwrap().into()
).unwrap();
let higher = lower.conj_ref();
((higher - lower).floor().to_integer().value() + 1u8).to_string()
}
}

View file

@ -39,6 +39,9 @@ enum Subcommands {
/// If used, then the example input will be solved and checked rather than the real one
#[arg(short, long)]
example: bool,
/// If used, then the custom input will be solved and checked rather than the real one
#[arg(short, long)]
custom: bool,
},
/// Runs solution N amount of times, averaging speed
Benchmark {
@ -71,9 +74,12 @@ async fn main() {
let client = reqwest::Client::new();
match cli.subcommand {
Subcommands::Solve { day, part, example } => {
Subcommands::Solve { day, part, example, custom } => {
let (input, solution) = match example {
false => (fetcher::fetch_input(cli.auth, day).await, None),
false => match custom {
false => (fetcher::fetch_input(cli.auth, day).await, None),
true => (include_str!("input.txt").to_string(), None)
},
true => {
let example = fetcher::fetch_example(cli.auth, day, part).await;
(example.0, Some(example.1))