From aab3aa235894edfd4eed3e984261f3cae4757081 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Wed, 21 Dec 2022 19:09:45 -0500 Subject: [PATCH] FINALLY fix day 16 --- rs/Cargo.lock | 127 +++++++++++++++++++++++++++++++++------- rs/Cargo.toml | 3 +- rs/benches/criterion.rs | 3 + rs/src/day16.rs | 16 +---- 4 files changed, 113 insertions(+), 36 deletions(-) diff --git a/rs/Cargo.lock b/rs/Cargo.lock index c8a6ba0..59d34c3 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -14,6 +14,7 @@ version = "0.1.0" dependencies = [ "build_const", "criterion", + "gag", "pretty_assertions", "rayon", "smallvec", @@ -26,7 +27,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi", ] @@ -222,6 +223,36 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "fastrand" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" +dependencies = [ + "instant", +] + +[[package]] +name = "filedescriptor" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7199d965852c3bac31f779ef99cbb4537f80e952e2d6aa0ffeb30cce00f4f46e" +dependencies = [ + "libc", + "thiserror", + "winapi", +] + +[[package]] +name = "gag" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a713bee13966e9fbffdf7193af71d54a6b35a0bb34997cd6c9519ebeb5005972" +dependencies = [ + "filedescriptor", + "tempfile", +] + [[package]] name = "half" version = "1.8.2" @@ -243,6 +274,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + [[package]] name = "indexmap" version = "1.9.2" @@ -273,9 +313,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" [[package]] name = "js-sys" @@ -343,11 +383,11 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi", + "hermit-abi 0.2.6", "libc", ] @@ -445,18 +485,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" dependencies = [ "proc-macro2", ] @@ -507,11 +547,20 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "ryu" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" [[package]] name = "same-file" @@ -530,18 +579,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.149" +version = "1.0.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055" +checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.149" +version = "1.0.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4eae9b04cbffdfd550eb462ed33bc6a1b68c935127d008b27444d08380f94e4" +checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" dependencies = [ "proc-macro2", "quote", @@ -550,9 +599,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.89" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" dependencies = [ "itoa", "ryu", @@ -595,21 +644,55 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.105" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +dependencies = [ + "cfg-if", + "fastrand", + "libc", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "textwrap" version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" +[[package]] +name = "thiserror" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -622,9 +705,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "walkdir" diff --git a/rs/Cargo.toml b/rs/Cargo.toml index 44cc869..7d990f2 100644 --- a/rs/Cargo.toml +++ b/rs/Cargo.toml @@ -28,7 +28,8 @@ static_init = "1.0.3" [dev-dependencies] criterion = "0.4.0" -pretty_assertions = "1" +gag = "1.0.0" +pretty_assertions = "1.3.0" [[bench]] name = "criterion" diff --git a/rs/benches/criterion.rs b/rs/benches/criterion.rs index d31199d..3e9921e 100644 --- a/rs/benches/criterion.rs +++ b/rs/benches/criterion.rs @@ -6,6 +6,7 @@ use aoc2022::{ day20, day21, day3, day4, day5, day6, day7, day8, day9, }; use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use gag::Gag; build_const!("aoc2022"); @@ -80,10 +81,12 @@ fn aoc2022_bench(c: &mut Criterion) { b.iter(|| day15::part2(4000000, black_box(DAY15))) }); g.finish(); + let gag = Gag::stderr(); let mut g = c.benchmark_group("day 16"); g.bench_function("part 1", |b| b.iter(|| day16::part1(black_box(DAY16)))); g.bench_function("part 2", |b| b.iter(|| day16::part2(black_box(DAY16)))); g.finish(); + drop(gag); let mut g = c.benchmark_group("day 17"); g.bench_function("part 1", |b| b.iter(|| day17::part1(black_box(DAY17)))); g.bench_function("part 2", |b| b.iter(|| day17::part2(black_box(DAY17)))); diff --git a/rs/src/day16.rs b/rs/src/day16.rs index 3476319..385193a 100644 --- a/rs/src/day16.rs +++ b/rs/src/day16.rs @@ -76,12 +76,8 @@ where } self.best_estimate = max(self.best_estimate, Some(r)); let (potential, nexts) = (self.next)(&t); - if let Some(potential) = potential { - if let Some(best_estimate) = self.best_estimate { - if potential < best_estimate { - continue; - } - } + if potential.is_some() && potential < self.best_estimate { + continue; } for next in nexts { if !self.seen.contains(&next.1) { @@ -147,7 +143,7 @@ where .get(&(room, valve)) .and_then(|d| d.checked_sub(age)) .filter(|&d| d < state.time) - .map(|d| graph[room].0 * (state.time - d - 1)) + .map(|d| graph[valve].0 * (state.time - d - 1)) }) .max() }) @@ -162,12 +158,6 @@ where moves.entry(d).or_insert_with(|| [(); N].map(|_| vec![]))[i].push(valve); } } - // if moves - // .iter() - // .all(|(_, moves)| moves.iter().all(|moves| moves.is_empty())) - // { - // return (None, vec![]); - // } let mut options = vec![]; for (d, moves) in moves { let mut indices = [None; N];