[TASK] Solved day 8, but it wasn't easy

This commit is contained in:
2023-12-11 23:06:00 +01:00
parent 4b79613e19
commit d1a50a8d18
4 changed files with 894 additions and 2 deletions

164
src/day8.rs Normal file
View File

@@ -0,0 +1,164 @@
use std::collections::HashMap;
use num::{BigInt, Integer};
use crate::day_solver::DaySolver;
#[cfg(test)]
use crate::util::read_file;
pub struct Day8 {
instructions: Vec<char>,
nodes: HashMap<String, (String, String)>
}
impl Day8 {
pub fn create(input: String) -> Self {
let mut lines = input.lines();
let instructions = lines.next().unwrap();
// Skip blank line
lines.next();
let mut nodes = HashMap::new();
for line in lines {
let (node_id, options) = line.split_once(" = ").unwrap();
let (left, right) = options[1..(options.len() - 1)].split_once(", ").unwrap();
nodes.insert(node_id.to_string(), (left.to_string(), right.to_string()));
}
// Put the input into the day struct
return Day8 {
instructions: instructions.chars().collect(),
nodes
}
}
}
impl Day8 {
fn follow_instruction(&self, cur_loc: &str, instruction: &char) -> &String {
let cur_options = self.nodes.get(cur_loc).unwrap();
return if instruction == &'L' {
&cur_options.0
} else {
&cur_options.1
}
}
}
#[derive(Debug, Clone)]
struct Period {
start: BigInt,
repeat: BigInt
}
impl DaySolver for Day8 {
fn solve_part1(&mut self) -> String {
let mut i = 0usize;
let mut cur_loc = "AAA";
while cur_loc != "ZZZ" {
let instruction = &self.instructions[i % self.instructions.len()];
cur_loc = self.follow_instruction(cur_loc, instruction);
i += 1;
}
return i.to_string();
}
fn solve_part2(&mut self) -> String {
// Let's start with the naive approach
let start_nodes: Vec<&String> = self.nodes.keys().filter(|n| n.ends_with('A')).collect();
// let's assume periodicity, so we're gonna try and found out at which iterations we get a valid endpoint for each node
let mut periods_per_start_node: Vec<Period> = Vec::new();
for start_node in start_nodes {
let mut cur_loc = start_node;
let mut i = 0;
let mut end_times: Vec<usize> = Vec::new();
// Stop if i % instruction.len() == 0 && cur_loc == start_node, so continue if not that
while i == 0 || !(i % self.instructions.len() == 0 && cur_loc == start_node) {
let instruction = &self.instructions[i % self.instructions.len()];
cur_loc = self.follow_instruction(cur_loc, instruction);
i += 1;
if cur_loc.ends_with('Z') {
end_times.push(i);
// Using this logging, we found out that each of the start_nodes has basically some time before it finds a valid end, and then it repeats that after a while:
// println!("Found period at {} for {}", i, cur_loc);
// if let Some(last_period) = last_period_option{
// println!("iterations since last period: {}", i - last_period);
// }
// if periods.len() > 10 {
// break
// }
if end_times.len() > 2 && end_times[2] - end_times[1] == end_times[1] - end_times[0] {
break
}
}
}
// println!("Start node {}, after {}: {:?}", start_node, i, end_times);
periods_per_start_node.push(Period {
start: BigInt::from(end_times[0]),
repeat: BigInt::from(end_times[1] - end_times[0])
})
}
// Now we just need to find out when these periods fall at the same time
// This appears to be the LCM of all periods
let mut solution_period: Period = periods_per_start_node.first().unwrap().to_owned();
for period in periods_per_start_node.iter().skip(1) {
solution_period = Period {
start: solution_period.start,
repeat: solution_period.repeat.lcm(&period.repeat)
};
}
// note: I don't understand why we can just ignore how long it takes to get to the end the first time...
// let mut i = 0usize;
//
// while !cur_nodes.iter().all(|n| n.ends_with('Z')) {
// let instruction = &self.instructions[i % self.instructions.len()];
// for j in 0..cur_nodes.len() {
// cur_nodes[j] = self.follow_instruction(&cur_nodes[j], instruction);
// }
// i += 1;
// }
return solution_period.repeat.to_string();
}
}
#[test]
fn test_part1() {
let mut day = Day8::create(read_file("input/day08_example.txt"));
assert_eq!("2", day.solve_part1());
}
#[test]
fn test_part1_2() {
let mut day = Day8::create(String::from("LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
"));
assert_eq!("6", day.solve_part1());
}
#[test]
fn test_part2() {
let mut day = Day8::create(String::from("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!("6", day.solve_part2());
}

View File

@@ -8,10 +8,12 @@ use crate::day4::Day4;
use crate::day5::Day5;
use crate::day6::Day6;
use crate::day7::Day7;
use crate::day8::Day8;
use crate::day_solver::DaySolver;
use crate::util::read_file;
mod util;
mod day_solver;
mod day1;
mod day2;
mod day3;
@@ -19,9 +21,9 @@ mod day4;
mod day5;
mod day6;
mod day7;
mod day_solver;
mod day8;
const MAX_DAY: u8 = 7;
const MAX_DAY: u8 = 8;
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
fn main() {
@@ -99,6 +101,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
5 => Some(Box::new(Day5::create(input))),
6 => Some(Box::new(Day6::create(input))),
7 => Some(Box::new(Day7::create(input))),
8 => Some(Box::new(Day8::create(input))),
_ => None
}
}