Faster day14 solution

This commit is contained in:
Adrian Groh 2024-12-14 18:37:18 +01:00
parent 8adfd753cb
commit 4f5c52e553
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771
3 changed files with 25 additions and 6 deletions

16
day14/Cargo.lock generated
View File

@ -16,15 +16,31 @@ name = "day14"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"glam", "glam",
"itertools",
"regex", "regex",
] ]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "glam" name = "glam"
version = "0.29.2" version = "0.29.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677" checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.4" version = "2.7.4"

View File

@ -5,4 +5,5 @@ edition = "2021"
[dependencies] [dependencies]
glam = "0.29.2" glam = "0.29.2"
itertools = "0.13.0"
regex = "1.11.1" regex = "1.11.1"

View File

@ -1,4 +1,5 @@
use glam::IVec2; use glam::IVec2;
use itertools::Itertools;
use regex::Regex; use regex::Regex;
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
@ -76,12 +77,13 @@ fn print_map(robots: &[Robot], dimensions: &IVec2) -> String {
} }
fn has_tree(robots: &[Robot]) -> bool { fn has_tree(robots: &[Robot]) -> bool {
let check: Vec<_> = (0..16).map(|i| IVec2::new(i, 0)).collect(); robots.len() == robots.iter().map(|r| r.position).unique().count()
robots.iter().any(|p| { // let check: Vec<_> = (0..16).map(|i| IVec2::new(i, 0)).collect();
check // robots.iter().any(|p| {
.iter() // check
.all(|c| robots.iter().any(|p2| p2.position == p.position + c)) // .iter()
}) // .all(|c| robots.iter().any(|p2| p2.position == p.position + c))
// })
} }
fn part2(robots: &mut [Robot], dimensions: &IVec2) -> i32 { fn part2(robots: &mut [Robot], dimensions: &IVec2) -> i32 {