From 0ac85b5ef00523efc160320e694394553c5b2223 Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Fri, 8 Dec 2023 00:21:19 -0700 Subject: [PATCH] Finished part 2. Not really satisfied with the answer. --- day_08/Cargo.lock | 69 +++++++++++++++++++++++++++++++++++++++++++++++ day_08/Cargo.toml | 2 ++ day_08/src/lib.rs | 65 +++++++++++++++++++++++++++++++------------- 3 files changed, 117 insertions(+), 19 deletions(-) diff --git a/day_08/Cargo.lock b/day_08/Cargo.lock index 630349a..b494f05 100644 --- a/day_08/Cargo.lock +++ b/day_08/Cargo.lock @@ -181,6 +181,8 @@ version = "0.1.0" dependencies = [ "criterion", "nom", + "num", + "rayon", "strum", "strum_macros", ] @@ -301,6 +303,73 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[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-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "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-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "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" diff --git a/day_08/Cargo.toml b/day_08/Cargo.toml index 93635e3..f45cc84 100644 --- a/day_08/Cargo.toml +++ b/day_08/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] nom = "7.1.3" +num = "0.4.1" +rayon = "1.8.0" strum = "0.25.0" strum_macros = "0.25.3" diff --git a/day_08/src/lib.rs b/day_08/src/lib.rs index ae11bfd..fe9ad74 100644 --- a/day_08/src/lib.rs +++ b/day_08/src/lib.rs @@ -1,14 +1,11 @@ use nom::{ - bytes::complete::{take_until, take, tag}, - character::complete::{ - newline, multispace1, alphanumeric1, - }, + bytes::complete::{take, tag}, + character::complete::alphanumeric1, sequence::{preceded, separated_pair}, - multi::separated_list1, IResult, }; use std::collections::HashMap; -use std::str::Chars; +use num::integer::lcm; fn parse_mapline(input: &str) -> IResult<&str, (&str, (&str, &str))> { let (input, start) = take(3 as usize)(input)?; @@ -40,29 +37,59 @@ pub fn part1(input: &str) -> String { } pub fn part2(input: &str) -> String { + // let mut lines = input.lines(); + // let instructions = lines.next().unwrap().chars(); + // let map = lines.skip(1).map(|line| { + // parse_mapline(line).unwrap().1 + // }).collect::>(); + // let start_nodes = map.keys().into_iter().filter(|x| x.chars().last().unwrap() == 'A').collect::>(); + // let mut total_steps = 0_u64; + // let mut positions = start_nodes; + // for _ in 0..1000000 { + // for instruction in instructions.clone() { + // total_steps += 1; + // for i in 0..positions.len() { + // let (left, right) = map.get(positions[i]).unwrap(); + // if instruction == 'L' { + // positions[i] = &left; + // } else { + // positions[i] = &right; + // } + // } + // if positions.iter().all(|x| {x.chars().last().unwrap() == 'Z'}) { return total_steps.to_string(); }; + // } + // }; + // total_steps.to_string() let mut lines = input.lines(); let instructions = lines.next().unwrap().chars(); let map = lines.skip(1).map(|line| { parse_mapline(line).unwrap().1 }).collect::>(); let start_nodes = map.keys().into_iter().filter(|x| x.chars().last().unwrap() == 'A').collect::>(); - let mut total_steps = 0; - let mut positions = start_nodes; - for _ in 0..10000000 { - for instruction in instructions.clone() { - total_steps += 1; - for i in 0..positions.len() { - let (left, right) = map.get(positions[i]).unwrap(); + let mut solutions = Vec::::new(); + for ind in 0..start_nodes.len() { + let mut total_steps = 0; + let start_node = start_nodes[ind]; + let mut position = *start_node; + for _ in 0..100000 { + for instruction in instructions.clone() { + total_steps += 1; + let (left, right) = map.get(position).unwrap(); if instruction == 'L' { - positions[i] = &left; + position = left; } else { - positions[i] = &right; + position = right; } + if position.chars().last().unwrap() == 'Z' { break; }; } - if positions.iter().all(|x| {x.chars().last().unwrap() == 'Z'}) { return total_steps.to_string(); }; - } - }; - total_steps.to_string() + if position.chars().last().unwrap() == 'Z' { break; }; + }; + solutions.push(total_steps); + } + println!("{:?}", &solutions); + let first = solutions[0]; + solutions.into_iter().skip(1).fold(first, |acc, x| {lcm(acc, x)}).to_string() + } pub mod prelude {