Skip to content

Instantly share code, notes, and snippets.

Created December 9, 2016 00:01
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/b5de81532cea28044839a298c443e940 to your computer and use it in GitHub Desktop.
Save anonymous/b5de81532cea28044839a298c443e940 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
fn is_real(encrypted: &str, sum: &str) -> bool {
let mut counts : std::collections::HashMap<char, i32> = std::collections::HashMap::new();
for c in encrypted.chars() {
counts.insert(c, 0);
}
for c in encrypted.chars() {
match c {
'-' => {},
_ => {
let count = counts.get(&c).unwrap() + 1;
counts.insert(c, count);
}
}
}
let mut count = std::i32::MAX;
let mut last_c = '0';
for c in sum.chars() {
if c == ']' {return true};
match counts.get(&c) {
Some(current_count) => {
if current_count > &count {
return false
} else if current_count == &count && last_c > c {
return false
} else {
println!("{:?}", last_c);
count = *current_count;
last_c = c;
}
},
None => return false
}
}
return true
}
fn main () {
let input = "aaaaa-bbb-z-y-x-123[abxyz]
a-b-c-d-e-f-g-h-987[abcde]
not-a-real-room-404[oarel]
totally-real-room-200[decoy]";
let rooms = input.split("\n");
let mut count = 0;
for room in rooms {
println!("{:?}", room);
let split1 : Vec<&str> = room.rsplitn(2, "[").collect();
println!("{:?}", split1);
let (sum, name) = (split1[0], split1[1]);
let split2 : Vec<&str> = name.rsplitn(2, "-").collect();
println!("{:?}", split2);
let (sector, encrypted) = (split2[0], split2[1]);
if is_real(encrypted, sum){
count += sector.parse::<i32>().unwrap();
}
}
println!("{:?}", count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment