43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
use crate::utils;
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
pub fn task1() {
|
|
let input = utils::read_file("input/day07.txt");
|
|
let mut tasks: HashSet<char> = HashSet::new();
|
|
let dependencies: Vec<(char, char)> = input
|
|
.lines()
|
|
.map(|line| (line.chars().nth(5).unwrap(), line.chars().nth(36).unwrap()))
|
|
.collect();
|
|
|
|
let mut depends_on: HashMap<char, HashSet<char>> = HashMap::new();
|
|
for (a, b) in dependencies {
|
|
depends_on.entry(b).or_insert(HashSet::new()).insert(a);
|
|
tasks.insert(a);
|
|
tasks.insert(b);
|
|
}
|
|
|
|
let mut open: HashSet<char> = tasks
|
|
.iter()
|
|
.filter(|task| !depends_on.contains_key(task))
|
|
.map(|c| *c)
|
|
.collect();
|
|
|
|
let mut result = String::new();
|
|
while open.len() > 0 {
|
|
let next = open.iter().min().unwrap().clone();
|
|
open.remove(&next);
|
|
result.push(next);
|
|
let newly_open: HashSet<char> = depends_on
|
|
.iter()
|
|
.filter(|(_task, deps)| deps.iter().all(|dep| result.chars().any(|c| c == *dep)))
|
|
.map(|(task, _)| *task)
|
|
.collect();
|
|
open = open.union(&newly_open).map(|c| *c).collect();
|
|
for c in newly_open {
|
|
depends_on.remove(&c);
|
|
}
|
|
}
|
|
|
|
println!("{}", result);
|
|
}
|