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

View File

@@ -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::<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 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;
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::<u64>::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 {