Solve day 5
This commit is contained in:
parent
2c2f87ed21
commit
9a7fa0dd65
3 changed files with 1462 additions and 0 deletions
28
input/05-0.in
Normal file
28
input/05-0.in
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
||||
1366
input/05-1.in
Normal file
1366
input/05-1.in
Normal file
File diff suppressed because it is too large
Load diff
68
src/bin/05.rs
Normal file
68
src/bin/05.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
fn main() {
|
||||
let text = std::fs::read_to_string("input/05-1.in").unwrap();
|
||||
|
||||
let (ordering, rules) = text.split_once("\n\n").unwrap();
|
||||
|
||||
let mut orderings: HashMap<u32, HashSet<u32>> = HashMap::new();
|
||||
for (a, b) in ordering.lines().map(|l| {
|
||||
let (a, b) = l.split_once('|').unwrap();
|
||||
(a.parse::<u32>().unwrap(), b.parse::<u32>().unwrap())
|
||||
}) {
|
||||
match orderings.entry(b) {
|
||||
std::collections::hash_map::Entry::Occupied(mut e) => {
|
||||
e.get_mut().insert(a);
|
||||
}
|
||||
std::collections::hash_map::Entry::Vacant(e) => {
|
||||
e.insert(HashSet::from_iter([a]));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let mut sum1 = 0;
|
||||
for rule in rules.lines().map(|l| {
|
||||
l.split(',')
|
||||
.map(|a| a.parse::<u32>().unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
}) {
|
||||
'outer: for i in 0..rule.len() {
|
||||
for j in (i + 1)..rule.len() {
|
||||
if let Some(s) = orderings.get(&rule[i]) {
|
||||
if s.contains(&rule[j]) {
|
||||
break 'outer;
|
||||
} else if i + 2 == rule.len() && j + 1 == rule.len() {
|
||||
sum1 += rule[rule.len() / 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{}", sum1);
|
||||
|
||||
let mut sum2 = 0;
|
||||
for mut rule in rules.lines().map(|l| {
|
||||
l.split(',')
|
||||
.map(|a| a.parse::<u32>().unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
}) {
|
||||
for i in 0..rule.len() {
|
||||
let mut j = i + 1;
|
||||
while j < rule.len() {
|
||||
if let Some(s) = orderings.get(&rule[i]) {
|
||||
if s.contains(&rule[j]) {
|
||||
rule.swap(i, j);
|
||||
} else if i + 2 == rule.len() && j + 1 == rule.len() {
|
||||
sum2 += rule[rule.len() / 2];
|
||||
j += 1;
|
||||
} else {
|
||||
j += 1;
|
||||
}
|
||||
} else {
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{}", sum2 - sum1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue