|
| 1 | +use std::cmp::Ordering; |
| 2 | + |
| 3 | +use aoc::input_str; |
| 4 | + |
| 5 | +/// Eh, every problem is a graph problem. |
| 6 | +/// |
| 7 | +/// If (x, y) is in the matrix then X | Y, X comes before Y |
| 8 | +#[derive(Debug)] |
| 9 | +pub struct Graph { |
| 10 | + n: usize, |
| 11 | + matrix: Vec<bool>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Graph { |
| 15 | + pub fn new(n: usize) -> Self { |
| 16 | + Self { |
| 17 | + n, |
| 18 | + matrix: vec![false; (n + 1) * (n + 1)], |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + pub fn index(&self, x: usize, y: usize) -> usize { |
| 23 | + y * self.n + x |
| 24 | + } |
| 25 | + |
| 26 | + pub fn insert(&mut self, x: usize, y: usize) { |
| 27 | + let idx = self.index(x, y); |
| 28 | + self.matrix[idx] = true |
| 29 | + } |
| 30 | + |
| 31 | + pub fn contains(&self, x: usize, y: usize) -> bool { |
| 32 | + let idx = self.index(x, y); |
| 33 | + self.matrix[idx] |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn main() { |
| 38 | + let input = input_str!(2024, 5); |
| 39 | + |
| 40 | + let start = std::time::Instant::now(); |
| 41 | + |
| 42 | + let mut lines = input.lines(); |
| 43 | + |
| 44 | + let mut counter = 0; |
| 45 | + let mut mapping = [None; 100]; |
| 46 | + let mut reverse = vec![]; |
| 47 | + let mut constraints = vec![]; |
| 48 | + |
| 49 | + for line in lines.by_ref() { |
| 50 | + if line.is_empty() { |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + let mut split = line.split('|'); |
| 55 | + let x: usize = split.next().unwrap().parse().unwrap(); |
| 56 | + let y: usize = split.next().unwrap().parse().unwrap(); |
| 57 | + |
| 58 | + if mapping[x].is_none() { |
| 59 | + counter += 1; |
| 60 | + mapping[x] = Some(counter); |
| 61 | + reverse.push(x); |
| 62 | + } |
| 63 | + |
| 64 | + if mapping[y].is_none() { |
| 65 | + counter += 1; |
| 66 | + mapping[y] = Some(counter); |
| 67 | + reverse.push(y); |
| 68 | + } |
| 69 | + |
| 70 | + constraints.push((mapping[x].unwrap(), mapping[y].unwrap())); |
| 71 | + } |
| 72 | + |
| 73 | + debug_assert_eq!(mapping.iter().filter(|m| m.is_some()).count(), counter); |
| 74 | + let mut graph = Graph::new(counter); |
| 75 | + for (x, y) in constraints { |
| 76 | + graph.insert(x, y); |
| 77 | + } |
| 78 | + |
| 79 | + println!("Created graph: {:?}", start.elapsed()); |
| 80 | + let start = std::time::Instant::now(); |
| 81 | + |
| 82 | + let (good, bad): (Vec<_>, Vec<_>) = lines |
| 83 | + .map(|line| { |
| 84 | + line.split(',') |
| 85 | + .map(|i| i.parse::<usize>().unwrap()) |
| 86 | + .map(|i| mapping[i].unwrap()) |
| 87 | + .collect::<Vec<_>>() |
| 88 | + }) |
| 89 | + .partition(|pages| pages.windows(2).all(|v| graph.contains(v[0], v[1]))); |
| 90 | + |
| 91 | + println!("Partitioned: {:?}", start.elapsed()); |
| 92 | + let start = std::time::Instant::now(); |
| 93 | + |
| 94 | + let part1: usize = good |
| 95 | + .into_iter() |
| 96 | + .map(|pages| reverse[pages[pages.len() / 2] - 1]) |
| 97 | + .sum(); |
| 98 | + |
| 99 | + println!("Part 1: {:?}", start.elapsed()); |
| 100 | + println!("Part 1: {}", part1); |
| 101 | + let start = std::time::Instant::now(); |
| 102 | + |
| 103 | + let part2: usize = bad |
| 104 | + .into_iter() |
| 105 | + .map(|mut page| { |
| 106 | + page.sort_unstable_by(|x, y| match graph.contains(*x, *y) { |
| 107 | + true => Ordering::Greater, |
| 108 | + false => Ordering::Less, |
| 109 | + }); |
| 110 | + page |
| 111 | + }) |
| 112 | + .map(|pages| reverse[pages[pages.len() / 2] - 1]) |
| 113 | + .sum(); |
| 114 | + |
| 115 | + println!("Part 2: {:?}", start.elapsed()); |
| 116 | + println!("Part 2: {}", part2); |
| 117 | +} |
0 commit comments