Created
December 6, 2024 00:03
-
-
Save DengYiping/353293caa38df2c89e4ae0b26a98cce7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cmp::Ordering; | |
use advent_of_code_2024_day5::reading; | |
fn main() { | |
let input = reading::read(); | |
let mut result = 0; | |
for case in input.cases { | |
let mut sorted = case.clone(); | |
sorted.sort_by(|a, b| { | |
if let Some(should_after) = input.should_not_shown_before.get(a) { | |
if should_after.contains(b) { | |
return Ordering::Less; | |
} | |
} | |
if let Some(should_after) = input.should_not_shown_before.get(b) { | |
if should_after.contains(a) { | |
return Ordering::Greater; | |
} | |
} | |
return Ordering::Equal; | |
}); | |
if sorted.iter().zip(case.iter()).filter(|(a, b)| a == b).count() != case.len() { | |
result += if case.len() % 2 == 0 { | |
(sorted[case.len() / 2] + sorted[case.len() / 2 - 1]) / 2 | |
} else { | |
sorted[case.len() / 2] | |
} | |
} | |
} | |
println!("Result: {}", result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment