Created
December 27, 2016 01:33
-
-
Save isaacg1/da9cd096b8fc1fc77cdb1dfc3288c2ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
use std::fs::File; | |
use std::io::prelude::*; | |
fn main() { | |
/* let rooms = "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 mut file = File::open("data.txt").unwrap(); | |
let mut string = String::new(); | |
file.read_to_string(&mut string).unwrap(); | |
let rooms = string; | |
let total_value: usize = rooms.split('\n').map(value).sum(); | |
println!("Total value is {}", total_value); | |
} | |
fn value(name: &str) -> usize { | |
if name == "" { return 0 }; | |
let start: &str = &name[..name.find(char::is_numeric).unwrap()]; | |
//println!("Start: {:?}", start); | |
let mut char_dedup_vec = start.chars().filter(|&c| c != '-').collect::<HashSet<char>>().into_iter().collect::<Vec<char>>(); | |
char_dedup_vec.sort(); | |
char_dedup_vec.sort_by_key(|&k| start.len()-start.matches(k).count()); | |
let true_checksum: String = char_dedup_vec.into_iter().take(5).collect(); | |
let claimed_checksum = &name[name.len()-6..name.len()-1]; | |
//println!("True checksum: {:?}, claimed checksum: {:?}", true_checksum, claimed_checksum); | |
let id: usize = name.matches(char::is_numeric).collect::<String>().parse().unwrap(); | |
//println!("Id: {}", id); | |
if true_checksum == claimed_checksum { | |
let decrypted = start.chars().map(|x| shift(x, id)).collect::<String>(); | |
if decrypted.matches("north").count() > 0 { | |
println!("Decryped: {}", decrypted); | |
id | |
} else { | |
0 | |
} | |
} else { | |
0 | |
} | |
} | |
fn shift(c: char, amount: usize) -> char { | |
match c { | |
'-' => ' ', | |
'a' ... 'z' => { | |
let base = 'a' as usize; | |
let ord = c as usize - base; | |
let shifted_ord = (ord + amount) % 26; | |
(shifted_ord + base) as u8 as char | |
}, | |
_ => panic!("Unexpected character input to shift: {}", c), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment