From 18effe21f371a709926577de795b696ad1476c40 Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Tue, 26 Dec 2023 10:29:13 -0500 Subject: [PATCH] more correct is a random shuffling of the vertices, otherwise an evil input could make us go slow --- Cargo.lock | 1 + Cargo.toml | 1 + README.md | 4 ++-- src/bin/25.rs | 7 ++++++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6da62b0..04e4434 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,7 @@ dependencies = [ "ndarray-linalg", "num-integer", "pico-args", + "rand", "rayon", "smallvec", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 94aa64b..c2512d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,3 +37,4 @@ tinyvec = "1.6.0" fxhash = "0.2.1" ndarray = "0.15.6" ndarray-linalg = { version = "0.16.0", features = ["openblas-static"] } +rand = "0.8.5" diff --git a/README.md b/README.md index 3e07e98..f93da2b 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 22](./src/bin/22.rs) | `78.3µs` | `194.8µs` | | [Day 23](./src/bin/23.rs) | `330.4µs` | `13.3ms` | | [Day 24](./src/bin/24.rs) | `257.2µs` | `16.2µs` | -| [Day 25](./src/bin/25.rs) | `239.4µs` | `-` | +| [Day 25](./src/bin/25.rs) | `256.4µs` | `-` | -**Total: 30.75ms** +**Total: 30.77ms** --- diff --git a/src/bin/25.rs b/src/bin/25.rs index f5c8948..145f3b6 100644 --- a/src/bin/25.rs +++ b/src/bin/25.rs @@ -1,13 +1,18 @@ use std::collections::VecDeque; use fxhash::FxHashMap; +use rand::{seq::SliceRandom, thread_rng}; advent_of_code::solution!(25, 1); pub fn part_one(input: &str) -> Option { let mut flow: NetworkFlow = parse_graph(input).into(); let s = 0; - (1..flow.vertices).find_map(|t| find_cut_of_size(&mut flow, s, t, 3)) + let mut vertices = (1..flow.vertices).collect::>(); + vertices.shuffle(&mut thread_rng()); + vertices + .into_iter() + .find_map(|t| find_cut_of_size(&mut flow, s, t, 3)) } fn find_cut_of_size(net: &mut NetworkFlow, s: usize, t: usize, cut: i16) -> Option {