use std::io; use std::io::BufRead; use std::cmp::Ordering; use std::collections::BinaryHeap; #[derive(PartialEq, Eq)] struct DecreasingU8(u8); impl PartialOrd for DecreasingU8 { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for DecreasingU8 { fn cmp(&self, other: &Self) -> Ordering { other.0.cmp(&self.0) } } fn simulation(nexts: &[Vec], nelves: i32, basetime: i32, inctime: i32) -> (String, i32) { let mut incoming = [0; 26]; for tos in nexts.iter() { for &to in tos { incoming[to as usize] += 1; } } let mut sources = BinaryHeap::new(); for i in 0..26 { if incoming[i as usize] == 0 { sources.push(DecreasingU8(i)); } } let mut order = String::new(); while sources.len() > 0 { let DecreasingU8(i) = sources.pop().unwrap(); order.push(char::from(i + 65)); for &to in &nexts[i as usize] { incoming[to as usize] -= 1; if incoming[to as usize] == 0 { sources.push(DecreasingU8(to)); } } } (order, 0) } pub fn main(reader: T) -> io::Result<(String, String)> { let mut nexts: Vec> = vec![vec![]; 26]; for l in reader.lines().map(|l| l.unwrap()) { let (from, to) = (l.as_bytes()[5] - 65, l.as_bytes()[36] - 65); nexts[from as usize].push(to); } let (part1, _) = simulation(&nexts, 1, 1, 0); Ok((part1, String::new())) }