42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
use std::error::Error;
|
|
use std::fs;
|
|
use std::str;
|
|
use std::i64;
|
|
use std::collections::HashMap;
|
|
|
|
pub fn both(filename: &str) -> Result<(), Box<dyn Error>> {
|
|
let contents = fs::read_to_string(filename)?;
|
|
|
|
let mut lowest_sid = 2000;
|
|
let mut highest_sid = 0;
|
|
let mut sids: HashMap<i64, bool> = HashMap::new();
|
|
|
|
for line in contents.lines() {
|
|
let mut bstr = line.replace("B", "1");
|
|
bstr = bstr.replace("F", "0");
|
|
bstr = bstr.replace("R", "1");
|
|
bstr = bstr.replace("L", "0");
|
|
|
|
let sid = i64::from_str_radix(&bstr, 2).unwrap();
|
|
sids.insert(sid, true);
|
|
if sid > highest_sid {
|
|
highest_sid = sid;
|
|
}
|
|
if sid < lowest_sid {
|
|
lowest_sid = sid;
|
|
}
|
|
}
|
|
|
|
let mut my_sid = 0;
|
|
for i in lowest_sid..highest_sid {
|
|
if !sids.contains_key(&i) {
|
|
my_sid = i;
|
|
break
|
|
}
|
|
}
|
|
|
|
println!("Highest seat id seen: {}", highest_sid);
|
|
println!("Missing seat id (so mine!): {}", my_sid);
|
|
|
|
Ok(())
|
|
} |