Finished part 2. Not really satisfied with the answer.

This commit is contained in:
Connor Johnstone
2023-12-08 00:21:19 -07:00
parent b3f768f211
commit 0ac85b5ef0
3 changed files with 117 additions and 19 deletions

69
day_08/Cargo.lock generated
View File

@@ -181,6 +181,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"criterion", "criterion",
"nom", "nom",
"num",
"rayon",
"strum", "strum",
"strum_macros", "strum_macros",
] ]
@@ -301,6 +303,73 @@ dependencies = [
"minimal-lexical", "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]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.17" version = "0.2.17"

View File

@@ -7,6 +7,8 @@ edition = "2021"
[dependencies] [dependencies]
nom = "7.1.3" nom = "7.1.3"
num = "0.4.1"
rayon = "1.8.0"
strum = "0.25.0" strum = "0.25.0"
strum_macros = "0.25.3" strum_macros = "0.25.3"

View File

@@ -1,14 +1,11 @@
use nom::{ use nom::{
bytes::complete::{take_until, take, tag}, bytes::complete::{take, tag},
character::complete::{ character::complete::alphanumeric1,
newline, multispace1, alphanumeric1,
},
sequence::{preceded, separated_pair}, sequence::{preceded, separated_pair},
multi::separated_list1,
IResult, IResult,
}; };
use std::collections::HashMap; use std::collections::HashMap;
use std::str::Chars; use num::integer::lcm;
fn parse_mapline(input: &str) -> IResult<&str, (&str, (&str, &str))> { fn parse_mapline(input: &str) -> IResult<&str, (&str, (&str, &str))> {
let (input, start) = take(3 as usize)(input)?; let (input, start) = take(3 as usize)(input)?;
@@ -40,29 +37,59 @@ pub fn part1(input: &str) -> String {
} }
pub fn part2(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::<HashMap<&str, (&str, &str)>>();
// let start_nodes = map.keys().into_iter().filter(|x| x.chars().last().unwrap() == 'A').collect::<Vec<_>>();
// 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 mut lines = input.lines();
let instructions = lines.next().unwrap().chars(); let instructions = lines.next().unwrap().chars();
let map = lines.skip(1).map(|line| { let map = lines.skip(1).map(|line| {
parse_mapline(line).unwrap().1 parse_mapline(line).unwrap().1
}).collect::<HashMap<&str, (&str, &str)>>(); }).collect::<HashMap<&str, (&str, &str)>>();
let start_nodes = map.keys().into_iter().filter(|x| x.chars().last().unwrap() == 'A').collect::<Vec<_>>(); let start_nodes = map.keys().into_iter().filter(|x| x.chars().last().unwrap() == 'A').collect::<Vec<_>>();
let mut total_steps = 0; let mut solutions = Vec::<u64>::new();
let mut positions = start_nodes; for ind in 0..start_nodes.len() {
for _ in 0..10000000 { let mut total_steps = 0;
for instruction in instructions.clone() { let start_node = start_nodes[ind];
total_steps += 1; let mut position = *start_node;
for i in 0..positions.len() { for _ in 0..100000 {
let (left, right) = map.get(positions[i]).unwrap(); for instruction in instructions.clone() {
total_steps += 1;
let (left, right) = map.get(position).unwrap();
if instruction == 'L' { if instruction == 'L' {
positions[i] = &left; position = left;
} else { } 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(); }; if position.chars().last().unwrap() == 'Z' { break; };
} };
}; solutions.push(total_steps);
total_steps.to_string() }
println!("{:?}", &solutions);
let first = solutions[0];
solutions.into_iter().skip(1).fold(first, |acc, x| {lcm(acc, x)}).to_string()
} }
pub mod prelude { pub mod prelude {