This commit is contained in:
JonOfUs 2024-12-06 12:11:29 +01:00
parent 4d132cbe7c
commit 5dffb6f929
4 changed files with 1482 additions and 1 deletions

28
res/05/example Normal file
View 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

1371
res/05/input Normal file

File diff suppressed because it is too large Load diff

82
src/days/d05.rs Normal file
View file

@ -0,0 +1,82 @@
use std::fs;
pub fn solve() {
let path = "res/05/input";
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
let (rules_str, updates_str) = contents.split_once("\n\n").unwrap();
let rules: Vec<(usize, usize)> = rules_str
.lines()
.map(|s| {
s.split_once("|")
.map(|(n1, n2)| (n1.parse::<usize>().unwrap(), n2.parse::<usize>().unwrap()))
.unwrap()
})
.collect();
let updates: Vec<Vec<usize>> = updates_str
.lines()
.map(|line| {
line.split(",")
.map(|n| n.parse::<usize>().unwrap())
.collect::<Vec<usize>>()
})
.collect();
let result: usize = updates
.iter()
.filter(|upd| {
upd.iter().enumerate().all(|(i, n)| {
rules
.iter()
.filter(|(n1, _)| *n1 == *n)
.all(|(_, n2)| upd.iter().take(i).all(|n| n != n2))
})
})
.map(|upd| upd[(upd.len() / 2) as usize])
.sum();
println!("Result 1: {}", result);
let result: usize = updates
.iter()
.filter(|upd| {
!upd.iter().enumerate().all(|(i, n)| {
rules
.iter()
.filter(|(n1, _)| *n1 == *n)
.all(|(_, n2)| upd.iter().take(i).all(|n| n != n2))
})
})
.map(|upd| {
let mut upd = upd.clone();
while upd.iter().enumerate().any(|(i, n)| {
rules
.iter()
.filter(|(n1, _)| *n1 == *n)
.any(|(_, n2)| upd.iter().take(i).any(|n| n == n2))
}) {
for i in 0..upd.len() {
let mut upd2 = upd.clone();
rules
.iter()
.filter(|(n1, _)| *n1 == upd[i])
.filter(|(_, n2)| upd.iter().take(i).any(|n| n == n2))
.take(1)
.for_each(|(_, n2)| {
let pos = upd.iter().position(|n| *n == *n2).unwrap();
upd2[pos] = upd[i];
upd2[i] = *n2;
});
upd = upd2;
}
}
upd
})
.map(|upd| upd[(upd.len() / 2) as usize])
.sum();
println!("Result 2: {}", result);
}

View file

@ -29,11 +29,11 @@ fn _all_days() {
days::d04::solve(); days::d04::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 5"); println!("\nDay 5");
days::d05::solve(); days::d05::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 6"); println!("\nDay 6");
days::d06::solve(); days::d06::solve();
time = _print_elapsed(time); time = _print_elapsed(time);