use std::cmp::Ordering; #[path = "../lib.rs"] mod lib; fn parse_input() -> (Vec<(u8, u8)>, Vec>) { let mut state = true; let mut rules = vec![]; let mut updates = vec![]; for line in lib::iter_input() { if line == "" { state = false; } else if state { let (a, b) = line.split_once('|').unwrap(); rules.push((a.parse::().unwrap(), b.parse::().unwrap())); } else { updates.push(line.split(',').map(|s| s.parse::().unwrap()).collect()); } } return (rules, updates); } fn order(update: &Vec, rules: &Vec<(u8, u8)>) -> Vec { let mut clone = update.clone(); clone.sort_by(|a, b| { if rules.contains(&(*a, *b)) { return Ordering::Less; } else if rules.contains(&(*b, *a)) { return Ordering::Greater; } else { return Ordering::Equal; } }); return clone; } fn main() { let (rules, updates) = parse_input(); let mut count1 = 0; let mut count2 = 0; for update in updates.iter() { let ordered = order(update, &rules); if ordered == *update { count1 += ordered[ordered.len() / 2] as u64; } else { count2 += ordered[ordered.len() / 2] as u64; } } println!("part1: {}", count1); println!("part2: {}", count2); }