summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2018/input/7.txt101
-rw-r--r--2018/src/day7.rs61
-rw-r--r--2018/src/main.rs4
3 files changed, 165 insertions, 1 deletions
diff --git a/2018/input/7.txt b/2018/input/7.txt
new file mode 100644
index 0000000..59be316
--- /dev/null
+++ b/2018/input/7.txt
@@ -0,0 +1,101 @@
+Step J must be finished before step K can begin.
+Step N must be finished before step X can begin.
+Step S must be finished before step G can begin.
+Step T must be finished before step R can begin.
+Step H must be finished before step L can begin.
+Step V must be finished before step W can begin.
+Step G must be finished before step U can begin.
+Step K must be finished before step A can begin.
+Step D must be finished before step Z can begin.
+Step C must be finished before step E can begin.
+Step X must be finished before step P can begin.
+Step Y must be finished before step U can begin.
+Step R must be finished before step O can begin.
+Step W must be finished before step U can begin.
+Step O must be finished before step Q can begin.
+Step A must be finished before step P can begin.
+Step B must be finished before step E can begin.
+Step F must be finished before step E can begin.
+Step Q must be finished before step U can begin.
+Step M must be finished before step E can begin.
+Step P must be finished before step U can begin.
+Step L must be finished before step Z can begin.
+Step Z must be finished before step U can begin.
+Step U must be finished before step E can begin.
+Step I must be finished before step E can begin.
+Step H must be finished before step G can begin.
+Step X must be finished before step I can begin.
+Step K must be finished before step X can begin.
+Step Z must be finished before step I can begin.
+Step S must be finished before step M can begin.
+Step L must be finished before step U can begin.
+Step A must be finished before step M can begin.
+Step W must be finished before step A can begin.
+Step N must be finished before step A can begin.
+Step S must be finished before step E can begin.
+Step W must be finished before step Q can begin.
+Step J must be finished before step L can begin.
+Step Q must be finished before step L can begin.
+Step M must be finished before step U can begin.
+Step H must be finished before step E can begin.
+Step D must be finished before step E can begin.
+Step V must be finished before step P can begin.
+Step Q must be finished before step M can begin.
+Step X must be finished before step W can begin.
+Step K must be finished before step I can begin.
+Step T must be finished before step H can begin.
+Step Y must be finished before step L can begin.
+Step G must be finished before step O can begin.
+Step M must be finished before step Z can begin.
+Step F must be finished before step Z can begin.
+Step Q must be finished before step E can begin.
+Step H must be finished before step C can begin.
+Step Q must be finished before step P can begin.
+Step D must be finished before step U can begin.
+Step Z must be finished before step E can begin.
+Step O must be finished before step M can begin.
+Step L must be finished before step I can begin.
+Step J must be finished before step A can begin.
+Step Q must be finished before step Z can begin.
+Step P must be finished before step I can begin.
+Step K must be finished before step O can begin.
+Step R must be finished before step E can begin.
+Step W must be finished before step F can begin.
+Step D must be finished before step Q can begin.
+Step R must be finished before step U can begin.
+Step W must be finished before step P can begin.
+Step S must be finished before step Z can begin.
+Step T must be finished before step P can begin.
+Step B must be finished before step Q can begin.
+Step S must be finished before step T can begin.
+Step R must be finished before step A can begin.
+Step K must be finished before step R can begin.
+Step N must be finished before step G can begin.
+Step C must be finished before step W can begin.
+Step T must be finished before step A can begin.
+Step B must be finished before step Z can begin.
+Step C must be finished before step P can begin.
+Step D must be finished before step P can begin.
+Step B must be finished before step P can begin.
+Step F must be finished before step U can begin.
+Step V must be finished before step X can begin.
+Step K must be finished before step W can begin.
+Step Y must be finished before step I can begin.
+Step C must be finished before step B can begin.
+Step X must be finished before step L can begin.
+Step X must be finished before step M can begin.
+Step H must be finished before step P can begin.
+Step S must be finished before step F can begin.
+Step J must be finished before step Y can begin.
+Step Y must be finished before step Z can begin.
+Step B must be finished before step I can begin.
+Step S must be finished before step C can begin.
+Step K must be finished before step E can begin.
+Step N must be finished before step Q can begin.
+Step A must be finished before step Z can begin.
+Step J must be finished before step I can begin.
+Step Y must be finished before step O can begin.
+Step Y must be finished before step F can begin.
+Step S must be finished before step U can begin.
+Step D must be finished before step W can begin.
+Step V must be finished before step D can begin.
diff --git a/2018/src/day7.rs b/2018/src/day7.rs
new file mode 100644
index 0000000..1924916
--- /dev/null
+++ b/2018/src/day7.rs
@@ -0,0 +1,61 @@
+use std::io;
+use std::io::BufRead;
+use std::cmp::Ordering;
+use std::collections::BinaryHeap;
+
+#[derive(PartialEq, Eq)]
+struct DecreasingU8(u8);
+
+impl PartialOrd for DecreasingU8 {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl Ord for DecreasingU8 {
+ fn cmp(&self, other: &Self) -> Ordering {
+ other.0.cmp(&self.0)
+ }
+}
+
+fn simulation(nexts: &[Vec<u8>], nelves: i32, basetime: i32, inctime: i32) -> (String, i32) {
+ let mut incoming = [0; 26];
+ for tos in nexts.iter() {
+ for &to in tos {
+ incoming[to as usize] += 1;
+ }
+ }
+
+ let mut sources = BinaryHeap::new();
+ for i in 0..26 {
+ if incoming[i as usize] == 0 {
+ sources.push(DecreasingU8(i));
+ }
+ }
+
+ let mut order = String::new();
+ while sources.len() > 0 {
+ let DecreasingU8(i) = sources.pop().unwrap();
+ order.push(char::from(i + 65));
+ for &to in &nexts[i as usize] {
+ incoming[to as usize] -= 1;
+ if incoming[to as usize] == 0 {
+ sources.push(DecreasingU8(to));
+ }
+ }
+ }
+
+ (order, 0)
+}
+
+pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> {
+ let mut nexts: Vec<Vec<u8>> = vec![vec![]; 26];
+ for l in reader.lines().map(|l| l.unwrap()) {
+ let (from, to) = (l.as_bytes()[5] - 65, l.as_bytes()[36] - 65);
+ nexts[from as usize].push(to);
+ }
+
+ let (part1, _) = simulation(&nexts, 1, 1, 0);
+
+ Ok((part1, String::new()))
+}
diff --git a/2018/src/main.rs b/2018/src/main.rs
index c35e142..fccb319 100644
--- a/2018/src/main.rs
+++ b/2018/src/main.rs
@@ -15,8 +15,9 @@ mod day3;
mod day4;
mod day5;
mod day6;
+mod day7;
-static NUM_DAYS: i32 = 6;
+static NUM_DAYS: i32 = 7;
fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
match day {
@@ -26,6 +27,7 @@ fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
4 => day4::main(reader),
5 => day5::main(reader),
6 => day6::main(reader),
+ 7 => day7::main(reader),
_ => Err(Error::new(ErrorKind::Other, "Invalid day"))
}
}