1
0
Fork 0

Refined Day 5 Part 2 solution

This commit is contained in:
Ishan Jain 2020-12-05 22:32:25 +05:30
parent 9aea6f16f5
commit 1797dd8b3d
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
1 changed files with 10 additions and 28 deletions

View File

@ -10,38 +10,20 @@ fn main() {
.filter(|x| !x.is_empty()) .filter(|x| !x.is_empty())
.into_iter() .into_iter()
.map(|seat| { .map(|seat| {
let mut region_lower = 0; let seat = seat.chars();
let mut region_upper = 127; let mut region: u16 = 0;
let mut col_lower = 0;
let mut col_upper = 7;
for s in seat.as_bytes() { for s in seat {
match s { region <<= 1;
b'F' => { region |= match s {
let mid = (region_lower + region_upper) / 2; 'B' | 'R' => 1,
region_upper = mid; 'F' | 'L' | _ => 0,
};
} }
b'B' => {
let mid = (region_lower + region_upper + 1) / 2;
region_lower = mid;
}
b'L' => {
let mid = (col_lower + col_upper) / 2;
col_upper = mid;
}
b'R' => {
let mid = (col_lower + col_upper + 1) / 2;
col_lower = mid;
}
_ => unreachable!(),
}
}
let region = region_lower;
let col = col_lower;
region * 8 + col (region >> 3) * 8 + (region & 0b111)
}) })
.collect::<HashSet<usize>>(); .collect::<HashSet<u16>>();
let min = *filled_seats.iter().min().unwrap(); let min = *filled_seats.iter().min().unwrap();
let max = *filled_seats.iter().max().unwrap(); let max = *filled_seats.iter().max().unwrap();