This commit is contained in:
2017-12-23 09:53:39 +01:00
parent d63b1a7e84
commit 045ea63bd9
4 changed files with 2088 additions and 0 deletions

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

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

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

@@ -0,0 +1,6 @@
[package]
name = "day-12"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

2000
2017/12/input.txt Normal file

File diff suppressed because it is too large Load Diff

78
2017/12/src/main.rs Normal file
View File

@@ -0,0 +1,78 @@
use std::io::{self, Read};
use std::collections::{HashMap, HashSet};
fn parse(input: &str) -> (usize, Vec<usize>) {
let parts = input.split("<->").collect::<Vec<_>>();
let from = parts[0].trim().parse::<usize>()
.expect("faild to parse from");
let to = parts[1].split(',').filter_map(|to| to.trim().parse::<usize>().ok()).collect::<Vec<_>>();
(from, to)
}
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input)
.expect("faild to read input");
let pipes = input.lines().filter(|line| !line.is_empty()).map(parse).collect::<Vec<_>>();
let mut groups = HashMap::new();
groups.entry(0).or_insert_with(HashSet::new).insert(0);
loop {
let mut done = true;
pipes.iter().for_each(|&(from, ref to)| {
let mut create = true;
for group in groups.values_mut() {
if group.contains(&from) {
create = false;
for id in to {
if group.insert(*id) {
done = false;
}
}
}
}
if create {
let group = groups.entry(from).or_insert_with(HashSet::new);
group.insert(from);
for id in to {
group.insert(*id);
}
}
});
if done {
break;
}
}
println!("part_one={}", groups[&0].len());
println!("part_two={}", groups.len());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
assert_eq!(parse("0 <-> 2"), (0, vec![2]));
assert_eq!(parse("1 <-> 1"), (1, vec![1]));
assert_eq!(parse("2 <-> 0, 3, 4"), (2, vec![0, 3, 4]));
}
}