Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2016 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0f4632e1a57943f6c2303c79baf51ba2 to your computer and use it in GitHub Desktop.
Save anonymous/0f4632e1a57943f6c2303c79baf51ba2 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#[derive(Debug)]
struct Floor {
micros : std::collections::HashSet<char>,
gens : std::collections::HashSet<char>,
}
fn parsefloor(room: &str) -> Floor {
let mut floor = Floor{
micros: std::collections::HashSet::<char>::new(),
gens: std::collections::HashSet::<char>::new()};
let mut items = room.split(" a ");
items.next(); // throw away the preamble
for item in items {
let element = item.chars().next().unwrap();
let shape = item.split(" ").nth(1).unwrap().chars().next().unwrap();
match shape {
'm' => {
floor.micros.insert(element);
},
'g' => {
floor.gens.insert(element);
},
_ => {
println!("{:?}", floor);
}
};
}
return floor
}
fn main () {
// sneaky empty 0th floor
let input = "The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip.
The second floor contains a hydrogen generator.
The third floor contains a lithium generator.
The fourth floor contains nothing relevant.";
let floorstrings = input.split("\n");
let mut floors : Vec<Floor> = vec![];
for floorstring in floorstrings {
floors.push(parsefloor(floorstring));
};
for floor in floors.into_iter().rev() {
//floor.micros.insert('b');
println!("{:?}", floor)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment