use std::error::Error; use std::fs::File; use std::io::{self, BufRead}; use std::vec::Vec; fn main() -> Result<(), Box> { let file = File::open("inputs/day23.txt")?; let mut lines = io::BufReader::new(file).lines().map(|l| l.unwrap()); let line = lines.nth(0).expect("No line in file."); let input: Vec = line .chars() .map(|c| c.to_string().parse().expect("Can't parse number")) .collect(); let cup_count = 1_000_000; // Initilize the linked list with cup_count ordered cups let mut cups = Vec::with_capacity(cup_count + 1); for v in 0..cup_count + 1 { cups.push(v + 1) } // Order the first ten according to the input for i in 0..input.len() - 1 { let cup = input[i]; let next_cup = input[i + 1]; cups[cup] = next_cup; } // Initialize cup zero, used as the 'current cup' later cups[0] = input[0]; // Connect the ends // The last from the input, points to the first autogenerated cup cups[input[input.len() - 1]] = input.len() + 1; let idx = cups.len() - 1; cups[idx] = input[0]; for _ in 1..10_000_001 { let cur_cup = cups[0]; let taken1 = cups[cur_cup]; let taken2 = cups[taken1]; let taken3 = cups[taken2]; cups[cur_cup] = cups[taken3]; cups[0] = cups[taken3]; let mut dest = cur_cup; while dest == taken1 || dest == taken2 || dest == taken3 || dest == cur_cup { dest = if dest - 1 == 0 { cup_count } else { dest - 1 } } let cut = cups[dest]; cups[dest] = taken1; cups[taken3] = cut; } let a = cups[1]; let b = cups[a]; println!("Cups: {} {}", a, b); let answer = a * b; println!("Answer: {}", answer); Ok(()) } #[cfg(test)] mod tests { use crate::*; }