Add day 7 solution

This commit is contained in:
Wesley Moore 2017-12-09 13:36:35 +10:00
parent ed90986668
commit 4f191a94c7
5 changed files with 1435 additions and 0 deletions

4
2017/day/7/Cargo.lock generated Normal file
View file

@ -0,0 +1,4 @@
[[package]]
name = "day7"
version = "0.1.0"

6
2017/day/7/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "day7"
version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
[dependencies]

1317
2017/day/7/input Normal file

File diff suppressed because it is too large Load diff

48
2017/day/7/problem.txt Normal file
View file

@ -0,0 +1,48 @@
--- Day 7: Recursive Circus ---
Wandering further through the circuits of the computer, you come upon a tower of programs that have gotten themselves into a bit of trouble. A recursive algorithm has gotten out of hand, and now they're balanced precariously in a large tower.
One program at the bottom supports the entire tower. It's holding a large disc, and on the disc are balanced several more sub-towers. At the bottom of these sub-towers, standing on the bottom disc, are other programs, each holding their own disc, and so on. At the very tops of these sub-sub-sub-...-towers, many programs stand simply keeping the disc below them balanced but with no disc of their own.
You offer to help, but first you need to understand the structure of these towers. You ask each program to yell out their name, their weight, and (if they're holding a disc) the names of the programs immediately above them balancing on that disc. You write this information down (your puzzle input). Unfortunately, in their panic, they don't do this in an orderly fashion; by the time you're done, you're not sure which program gave which information.
For example, if your list is the following:
pbga (66)
xhth (57)
ebii (61)
havc (66)
ktlj (57)
fwft (72) -> ktlj, cntj, xhth
qoyq (66)
padx (45) -> pbga, havc, qoyq
tknk (41) -> ugml, padx, fwft
jptl (61)
ugml (68) -> gyxo, ebii, jptl
gyxo (61)
cntj (57)
...then you would be able to recreate the structure of the towers that looks like this:
gyxo
/
ugml - ebii
/ \
| jptl
|
| pbga
/ /
tknk --- padx - havc
\ \
| qoyq
|
| ktlj
\ /
fwft - cntj
\
xhth
In this example, tknk is at the bottom of the tower (the bottom program), and is holding up ugml, padx, and fwft. Those programs are, in turn, holding up other programs; in this example, none of those programs are holding up any other programs, and are all the tops of their own towers. (The actual tower balancing in front of you is much larger.)
Before you're ready to help them, you need to make sure your information is correct. What is the name of the bottom program?

60
2017/day/7/src/main.rs Normal file
View file

@ -0,0 +1,60 @@
use std::io::Read;
use std::fs::File;
use std::collections::HashSet;
fn main() {
let mut input = String::new();
let mut file = File::open("input").expect("unable to open input file");
file.read_to_string(&mut input).expect("error reading input");
println!("{}", bottom_program(&input));
}
fn bottom_program(input: &str) -> String {
// Bottom one must be:
// - "holding other towers"
// - Have no other towers pointing at it
let mut programs = HashSet::new();
let mut referenced = HashSet::new();
for line in input.lines() {
let parts = line.split(" -> ").collect::<Vec<_>>();
let name_and_weight = parts[0].split_whitespace().collect::<Vec<_>>();
let name = name_and_weight[0];
let _weight = name_and_weight[1];
if let Some(holding) = parts.get(1) {
for child in holding.split(", ") {
referenced.insert(child);
}
}
programs.insert(name);
}
let candidates = programs.difference(&referenced).collect::<Vec<_>>();
if candidates.len() != 1 {
panic!("more than one candidate result");
}
(*candidates[0]).to_owned()
}
#[test]
fn test_example() {
let input = r"pbga (66)
xhth (57)
ebii (61)
havc (66)
ktlj (57)
fwft (72) -> ktlj, cntj, xhth
qoyq (66)
padx (45) -> pbga, havc, qoyq
tknk (41) -> ugml, padx, fwft
jptl (61)
ugml (68) -> gyxo, ebii, jptl
gyxo (61)
cntj (57)";
assert_eq!(bottom_program(input), "tknk".to_owned());
}