Initial bits for day6.

This commit is contained in:
2019-12-16 17:34:05 -08:00
parent 384f78e57b
commit 83a3acbc05
4 changed files with 1603 additions and 4 deletions

1438
inputs/day6 Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,6 @@
use clap::{App,Arg,SubCommand}; use clap::{App,Arg,SubCommand};
use crate::machine::Computer; use crate::machine::Computer;
use crate::orbits::UniversalOrbitMap;
use crate::wiremap::{Wire}; use crate::wiremap::{Wire};
use std::fs; use std::fs;
use std::iter::FromIterator; use std::iter::FromIterator;
@@ -10,6 +11,7 @@ pub enum Command {
ComputeFuel(Vec<u64>), ComputeFuel(Vec<u64>),
RunComputer(Computer), RunComputer(Computer),
WireMap(Vec<Wire>), WireMap(Vec<Wire>),
Orbits(UniversalOrbitMap),
PasswordCrack(u32, u32), PasswordCrack(u32, u32),
} }
@@ -63,6 +65,14 @@ impl Command {
.required(true) .required(true)
.validator(is_file)) .validator(is_file))
) )
.subcommand(SubCommand::with_name("orbits")
.about("compute the given orbit map")
.arg(Arg::with_name("MAP")
.index(1)
.help("The orbits to run.")
.required(true)
.validator(is_file))
)
.subcommand(SubCommand::with_name("crack") .subcommand(SubCommand::with_name("crack")
.about("crack a code in the given range") .about("crack a code in the given range")
.arg(Arg::with_name("START") .arg(Arg::with_name("START")
@@ -77,7 +87,7 @@ impl Command {
.validator(is_number)) .validator(is_number))
) )
.get_matches(); .get_matches();
if let Some(problem1) = matches.subcommand_matches("fuel") { if let Some(problem1) = matches.subcommand_matches("fuel") {
match problem1.values_of("NUM") { match problem1.values_of("NUM") {
None => None =>
@@ -88,7 +98,7 @@ impl Command {
} }
} }
} }
if let Some(problem2) = matches.subcommand_matches("compute") { if let Some(problem2) = matches.subcommand_matches("compute") {
let start_pos_str = problem2.value_of("START_POSITION").unwrap(); let start_pos_str = problem2.value_of("START_POSITION").unwrap();
let start_pos = usize::from_str_radix(&start_pos_str, 10).unwrap(); let start_pos = usize::from_str_radix(&start_pos_str, 10).unwrap();
@@ -120,7 +130,14 @@ impl Command {
return Command::PasswordCrack(start, end); return Command::PasswordCrack(start, end);
} }
if let Some(problem5) = matches.subcommand_matches("orbits") {
let file_contents = fs::read(problem5.value_of("MAP").unwrap()).unwrap();
let str_contents = str::from_utf8(&file_contents).unwrap();
let res = UniversalOrbitMap::from_str(&str_contents).unwrap();
return Command::Orbits(res);
}
panic!("Failed to run a reasonable command."); panic!("Failed to run a reasonable command.");
} }
} }

View File

@@ -2,6 +2,7 @@ mod args;
mod endchannel; mod endchannel;
mod fuel; mod fuel;
mod machine; mod machine;
mod orbits;
mod wiremap; mod wiremap;
use crate::args::Command; use crate::args::Command;
@@ -102,5 +103,10 @@ fn main() {
// 353 is wrong (low) // 353 is wrong (low)
println!("Successful digits: {}", count); println!("Successful digits: {}", count);
} }
Command::Orbits(uom) => {
println!("Got orbits.");
println!("Base map has {} orbits.", uom.num_orbits());
}
} }
} }

138
src/orbits.rs Normal file
View File

@@ -0,0 +1,138 @@
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
pub enum Object {
CenterOfMass,
Object(String),
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Object::CenterOfMass => write!(f, "COM"),
Object::Object(n) => write!(f, "{}", n),
}
}
}
impl FromStr for Object {
type Err = ();
fn from_str(s: &str) -> Result<Object,Self::Err> {
if s == "COM" {
Ok(Object::CenterOfMass)
} else {
Ok(Object::Object(s.to_string()))
}
}
}
pub struct UniversalOrbitMap {
orbits: HashMap<Object,Vec<Object>>
}
impl FromStr for UniversalOrbitMap {
type Err = ();
fn from_str(s: &str) -> Result<Self,Self::Err> {
let lines = s.split('\n');
let mut orbits: HashMap<Object,Vec<Object>> = HashMap::new();
for nextline in lines {
if nextline.trim().len() == 0 {
continue;
}
let mut splits = nextline.split(')');
let obj1str = splits.next().unwrap_or_else(|| panic!("Bad orbit line: {}", nextline));
let obj2str = splits.next().unwrap_or_else(|| panic!("Bad orbit line: {}", nextline));
let obj1 = Object::from_str(&obj1str)?;
let obj2 = Object::from_str(&obj2str)?;
if let Some(prev) = orbits.get_mut(&obj1) {
prev.push(obj2);
} else {
orbits.insert(obj1, vec![obj2]);
}
}
Ok(UniversalOrbitMap{ orbits })
}
}
impl UniversalOrbitMap {
fn orbits(&self, obj1: &Object, obj2: &Object) -> bool {
match self.orbits.get(obj2) {
None => false,
Some(items) => {
items.contains(obj1)
}
}
}
fn indirectly_orbits(&self, obj1: &Object, obj2: &Object) -> bool {
let mut search_stack = vec![obj2];
let mut history = vec![];
while let Some(nextobj) = search_stack.pop() {
if nextobj == obj1 {
return true;
}
if history.contains(nextobj) {
continue;
} else {
history.push(nextobj.clone());
}
match self.orbits.get(nextobj) {
None =>
continue,
Some(newitems) => {
for item in newitems.iter() {
search_stack.push(item);
}
}
}
}
false
}
fn objects(&self) -> Vec<Object> {
let mut res = vec![];
for (key, vals) in self.orbits.iter() {
if !res.contains(key) { res.push(key.clone()); };
for val in vals.iter() {
if !res.contains(val) { res.push(val.clone()); };
}
}
res
}
pub fn num_orbits(&self) -> usize {
let mut result = 0;
for obj1 in self.objects().iter() {
for obj2 in self.objects().iter() {
if (obj1 != obj2) && self.indirectly_orbits(obj1, obj2) {
result += 1;
}
}
}
result
}
}
#[test]
fn examples() {
let input = "COM)B\nB)C\nC)D\nD)E\nE)F\nB)G\nG)H\nD)I\nE)J\nJ)K\nK)L";
let map = UniversalOrbitMap::from_str(&input).unwrap();
assert!(map.orbits(&Object::Object("B".to_string()), &Object::CenterOfMass));
assert!(map.indirectly_orbits(&Object::Object("B".to_string()), &Object::CenterOfMass));
assert!(map.indirectly_orbits(&Object::Object("E".to_string()),&Object::CenterOfMass));
assert_eq!(map.num_orbits(), 42);
}