Add day 12 solution

This commit is contained in:
Wesley Moore 2017-12-19 07:58:36 +11:00
parent 183bbd50ca
commit 5f6a8fe251
5 changed files with 2087 additions and 0 deletions

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

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

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

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

2000
2017/day/12/input Normal file

File diff suppressed because it is too large Load diff

34
2017/day/12/problem.txt Normal file
View file

@ -0,0 +1,34 @@
--- Day 12: Digital Plumber ---
Walking along the memory banks of the stream, you find a small village that is experiencing a little confusion: some programs can't communicate with each other.
Programs in this village communicate using a fixed system of pipes. Messages are passed between programs using these pipes, but most programs aren't connected to each other directly. Instead, programs pass messages between each other until the message reaches the intended recipient.
For some reason, though, some of these messages aren't ever reaching their intended recipient, and the programs suspect that some pipes are missing. They would like you to investigate.
You walk through the village and record the ID of each program and the IDs with which it can communicate directly (your puzzle input). Each program has one or more programs with which it can communicate, and these pipes are bidirectional; if 8 says it can communicate with 11, then 11 will say it can communicate with 8.
You need to figure out how many programs are in the group that contains program ID 0.
For example, suppose you go door-to-door like a travelling salesman and record the following list:
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
In this example, the following programs are in the group that contains program ID 0:
Program 0 by definition.
Program 2, directly connected to program 0.
Program 3 via program 2.
Program 4 via program 2.
Program 5 via programs 6, then 4, then 2.
Program 6 via programs 4, then 2.
Therefore, a total of 6 programs are in this group; all but program 1, which has a pipe that connects it to itself.
How many programs are in the group that contains program ID 0?

43
2017/day/12/src/main.rs Normal file
View file

@ -0,0 +1,43 @@
use std::io::Read;
use std::fs::File;
use std::str::FromStr;
use std::collections::HashSet;
fn main() {
let mut buffer = String::new();
let mut file = File::open("input").expect("unable to open input file");
file.read_to_string(&mut buffer).expect("error reading input");
let mut programs = Vec::new();
for line in buffer.lines() {
// 1 <-> 66, 1682
let parts: Vec<&str> = line.split(" <-> ").collect();
let _pid = usize::from_str(parts[0]).unwrap();
let linked: Vec<usize> = parts[1].split(", ").map(|id| usize::from_str(id).unwrap()).collect();
// Assumes input is a sequential list of program ids with no gaps
programs.push(linked);
}
println!("{}", reachable(&programs));
}
fn reachable(list: &Vec<Vec<usize>>) -> usize {
let mut visited = HashSet::new();
visit(0, &mut visited, list);
visited.len()
}
fn visit(pid: usize, visited: &mut HashSet<usize>, list: &Vec<Vec<usize>>) {
if visited.contains(&pid) {
return
}
visited.insert(pid);
for child in list[pid].iter() {
visit(*child, visited, list);
}
}