Missed several updates. Working on 8 pt 2
This commit is contained in:
72
day_08/src/lib.rs
Normal file
72
day_08/src/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use nom::{
|
||||
bytes::complete::{take_until, take, tag},
|
||||
character::complete::{
|
||||
newline, multispace1, alphanumeric1,
|
||||
},
|
||||
sequence::{preceded, separated_pair},
|
||||
multi::separated_list1,
|
||||
IResult,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::str::Chars;
|
||||
|
||||
fn parse_mapline(input: &str) -> IResult<&str, (&str, (&str, &str))> {
|
||||
let (input, start) = take(3 as usize)(input)?;
|
||||
let (input, (left, right)) = preceded(tag(" = ("), separated_pair(alphanumeric1, tag(", "), alphanumeric1))(input)?;
|
||||
Ok((input, (start, (left, right))))
|
||||
}
|
||||
|
||||
pub fn part1(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 mut total_steps = 0;
|
||||
let mut position = "AAA";
|
||||
for _ in 0..100 {
|
||||
for instruction in instructions.clone() {
|
||||
total_steps += 1;
|
||||
let (left, right) = map.get(position).unwrap();
|
||||
if instruction == 'L' {
|
||||
position = left;
|
||||
} else {
|
||||
position = right;
|
||||
}
|
||||
if position == "ZZZ" { return total_steps.to_string(); };
|
||||
}
|
||||
};
|
||||
total_steps.to_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;
|
||||
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();
|
||||
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()
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::part1;
|
||||
pub use super::part2;
|
||||
}
|
||||
|
||||
48
day_08/src/main.rs
Normal file
48
day_08/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use day_08::prelude::*;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
println!("{}", part1(input));
|
||||
println!("{}", part2(input));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let test_input = "RL
|
||||
|
||||
AAA = (BBB, CCC)
|
||||
BBB = (DDD, EEE)
|
||||
CCC = (ZZZ, GGG)
|
||||
DDD = (DDD, DDD)
|
||||
EEE = (EEE, EEE)
|
||||
GGG = (GGG, GGG)
|
||||
ZZZ = (ZZZ, ZZZ)";
|
||||
assert_eq!(part1(test_input), "2".to_string());
|
||||
let new_test_input = "LLR
|
||||
|
||||
AAA = (BBB, BBB)
|
||||
BBB = (AAA, ZZZ)
|
||||
ZZZ = (ZZZ, ZZZ)";
|
||||
assert_eq!(part1(new_test_input), "6".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let test_input = "LR
|
||||
|
||||
11A = (11B, XXX)
|
||||
11B = (XXX, 11Z)
|
||||
11Z = (11B, XXX)
|
||||
22A = (22B, XXX)
|
||||
22B = (22C, 22C)
|
||||
22C = (22Z, 22Z)
|
||||
22Z = (22B, 22B)
|
||||
XXX = (XXX, XXX)";
|
||||
assert_eq!(part2(test_input), "6".to_string());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user