-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.rs
81 lines (69 loc) · 2.36 KB
/
day05.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::collections::HashMap;
use itertools::Itertools;
use crate::utils::v2::{parser::get_all_ints_signed, solver};
type Update = Vec<i64>;
type Updates = Vec<Update>;
type Constraints = HashMap<i64, Vec<i64>>;
type Input = (Constraints, Updates);
impl Solver {
fn parse_input(&self, input: &str) -> Input {
let (constraints, updates) = input
.split("\n\n")
.map(|section| section.lines().map(get_all_ints_signed).collect_vec())
.collect_tuple()
.unwrap();
let constraints = constraints
.into_iter()
.map(|v| (v[0], v[1]))
.into_group_map();
assert!(updates.iter().all(|v| v.len() % 2 == 1));
(constraints, updates)
}
fn satisfies_constraints(&self, constraints: &Constraints, update: &Update) -> bool {
for (i, r) in update.iter().enumerate() {
if constraints.get(r).is_some() {
for l in &update[..i] {
if constraints.get(&r).unwrap().contains(l) {
return false;
}
}
}
}
true
}
}
pub struct Solver;
impl solver::Solver<2024, 5> for Solver {
type Part1 = i64;
type Part2 = i64;
fn solve_part_one(&self, input: &str) -> Self::Part1 {
let (constraints, updates) = self.parse_input(input);
updates
.into_iter()
.filter(|update| self.satisfies_constraints(&constraints, update))
.map(|update| update[update.len() / 2])
.sum()
}
fn solve_part_two(&self, input: &str) -> Self::Part2 {
let (constraints, updates) = self.parse_input(input);
let mut result = 0;
for mut update in updates {
if self.satisfies_constraints(&constraints, &update) {
continue;
}
while !self.satisfies_constraints(&constraints, &update) {
for i in 0..update.len() {
for j in i..update.len() {
if let Some(c) = constraints.get(&update[j]) {
if c.contains(&update[i]) {
update.swap(i, j);
}
}
}
}
}
result += update[update.len() / 2];
}
result
}
}