Commit 6aef8501 authored by Jonathan Klimt's avatar Jonathan Klimt

Part 1 for day 7

parent 4794330c
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day7"
version = "0.1.0"
dependencies = [
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
"checksum redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "a84bcd297b87a545980a2d25a0beb72a1f490c31f0a9fde52fca35bfbb1ceb70"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
"checksum regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a2ed29da7a9e1960e1639e7a982e6edc6d49be308a3b02daf511504a16d1"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[package]
name = "day7"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
edition = "2018"
[dependencies]
regex = "1"
termion = "1.5.1"
Step Z must be finished before step V can begin.
Step V must be finished before step K can begin.
Step M must be finished before step Q can begin.
Step E must be finished before step X can begin.
Step J must be finished before step W can begin.
Step L must be finished before step O can begin.
Step Q must be finished before step T can begin.
Step Y must be finished before step P can begin.
Step X must be finished before step R can begin.
Step T must be finished before step U can begin.
Step I must be finished before step O can begin.
Step P must be finished before step H can begin.
Step G must be finished before step A can begin.
Step N must be finished before step A can begin.
Step H must be finished before step B can begin.
Step F must be finished before step D can begin.
Step S must be finished before step O can begin.
Step O must be finished before step W can begin.
Step D must be finished before step U can begin.
Step W must be finished before step B can begin.
Step A must be finished before step K can begin.
Step B must be finished before step R can begin.
Step K must be finished before step C can begin.
Step R must be finished before step C can begin.
Step U must be finished before step C can begin.
Step A must be finished before step U can begin.
Step J must be finished before step I can begin.
Step D must be finished before step K can begin.
Step V must be finished before step S can begin.
Step H must be finished before step C can begin.
Step R must be finished before step U can begin.
Step I must be finished before step G can begin.
Step D must be finished before step R can begin.
Step M must be finished before step B can begin.
Step G must be finished before step R can begin.
Step M must be finished before step I can begin.
Step G must be finished before step N can begin.
Step M must be finished before step N can begin.
Step Q must be finished before step S can begin.
Step I must be finished before step S can begin.
Step J must be finished before step R can begin.
Step O must be finished before step B can begin.
Step G must be finished before step S can begin.
Step J must be finished before step C can begin.
Step M must be finished before step D can begin.
Step T must be finished before step H can begin.
Step P must be finished before step N can begin.
Step S must be finished before step K can begin.
Step T must be finished before step C can begin.
Step J must be finished before step A can begin.
Step G must be finished before step F can begin.
Step N must be finished before step R can begin.
Step N must be finished before step W can begin.
Step T must be finished before step I can begin.
Step S must be finished before step B can begin.
Step H must be finished before step F can begin.
Step B must be finished before step C can begin.
Step L must be finished before step W can begin.
Step N must be finished before step O can begin.
Step O must be finished before step A can begin.
Step H must be finished before step S can begin.
Step F must be finished before step A can begin.
Step F must be finished before step C can begin.
Step M must be finished before step A can begin.
Step Z must be finished before step H can begin.
Step Z must be finished before step L can begin.
Step E must be finished before step H can begin.
Step X must be finished before step T can begin.
Step Y must be finished before step X can begin.
Step E must be finished before step W can begin.
Step P must be finished before step R can begin.
Step Z must be finished before step E can begin.
Step W must be finished before step C can begin.
Step I must be finished before step P can begin.
Step X must be finished before step A can begin.
Step Y must be finished before step C can begin.
Step I must be finished before step F can begin.
Step L must be finished before step T can begin.
Step A must be finished before step B can begin.
Step F must be finished before step W can begin.
Step T must be finished before step R can begin.
Step X must be finished before step F can begin.
Step M must be finished before step O can begin.
Step N must be finished before step K can begin.
Step T must be finished before step S can begin.
Step J must be finished before step N can begin.
Step J must be finished before step S can begin.
Step O must be finished before step D can begin.
Step T must be finished before step P can begin.
Step Z must be finished before step D can begin.
Step L must be finished before step X can begin.
Step Q must be finished before step G can begin.
Step M must be finished before step G can begin.
Step P must be finished before step W can begin.
Step V must be finished before step P can begin.
Step D must be finished before step B can begin.
Step Y must be finished before step D can begin.
Step X must be finished before step S can begin.
Step K must be finished before step U can begin.
Step Z must be finished before step Y can begin.
Step D must be finished before step W can begin.
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step F must be finished before step B can begin.
Step F must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step A must be finished before step E can begin.
-->F--->B--
/ \ \
C -->D----->E
\ /
---->A-----
extern crate regex;
use regex::Regex;
// extern crate termion;
// use termion::color;
// use termion::clear;
use std::io::Error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
// use std::thread;
// use std::time::Duration;
// use std::io::prelude::*;
use std::collections::HashMap;
fn main() -> Result<(), Error> {
let f = File::open("input.txt")?;
let bufread = BufReader::new(f);
let re = Regex::new(r"Step ([[:alpha:]]) must be finished before step ([[:alpha:]]) can begin\.").unwrap();
let mut dependencies : HashMap<char, Vec<char>> = HashMap::new();
// let mut remaining_nodes: Vec<char> = "ABCDEFGIJKLMNOPQRSTUVWXYZ".chars().collect();
let mut remaining_nodes: Vec<char> = Vec::new();
for line in bufread.lines() {
let l = line?;
if let Some(caps) = re.captures(&l) {
let p : (char, char) = (caps.get(1).unwrap().as_str().parse().unwrap(), caps.get(2).unwrap().as_str().parse().unwrap());
if let Some(x) = dependencies.get_mut(&p.0) {
x.push(p.1);
remaining_nodes.push(p.1);
} else {
dependencies.insert(p.0, vec![p.1]);
remaining_nodes.push(p.1);
}
// println!("{} is dependency of {}", p.0, p.1);
} else {
println!("Could not parse: {}", l);
}
}
remaining_nodes.sort();
remaining_nodes.dedup();
// Find first node
let mut possible_next_vals: Vec<char> = dependencies.clone()
.into_iter()
.map(|(c, _)| c)
.collect();
possible_next_vals.sort();
possible_next_vals.dedup();
possible_next_vals.retain(|c| !remaining_nodes.contains(c));
// println!("HashMap: {:?}", dependencies);
// println!("Dep nodes: {:?}", remaining_nodes);
// println!("Poss start: {:?}", possible_next_vals);
let mut solution: String = "".to_string();
while !dependencies.is_empty() {
// take first next possible value
let c = possible_next_vals[0];
solution.push(c);
possible_next_vals.remove(0);
remaining_nodes.retain(|&x| x != c);
// check all nodes whcih are depending on that node, if they have other dependencies
'outer: for d in dependencies.get(&c).unwrap() {
for r in remaining_nodes.clone() {
if let Some(rd) = dependencies.get(&r){
if rd.contains(d) {
continue 'outer;
}
}
}
// Does not have other deps, therefore add to pos vals
possible_next_vals.push(*d);
}
possible_next_vals.sort();
dependencies.remove(&c);
}
solution.push(possible_next_vals.pop().unwrap());
println!("Solution Part 1: {}", solution);
Ok(())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment