day 5 solution
This commit is contained in:
4 files changed
+791
No files matched your search
@@ -0,0 +1,44 @@
|
||||
// ==== day 5
|
||||
|
||||
use crate::utils;
|
||||
use anyhow::Result;
|
||||
|
||||
fn seat_code_to_usize(code: &str) -> usize {
|
||||
assert_eq!(code.len(), 10);
|
||||
let bincode = str::replace(code, "B", "1");
|
||||
let bincode = str::replace(&bincode, "F", "0");
|
||||
let bincode = str::replace(&bincode, "R", "1");
|
||||
let bincode = str::replace(&bincode, "L", "0");
|
||||
usize::from_str_radix(&bincode, 2).unwrap()
|
||||
}
|
||||
|
||||
fn usize_to_seat_id(x: usize) -> usize {
|
||||
((x >> 3) * 8) + (x & 0b111)
|
||||
}
|
||||
|
||||
pub fn run() -> Result<(usize, usize)> {
|
||||
let data = utils::read_file("data/input-05.txt")?;
|
||||
let answers = utils::read_i64s("data/output-05.txt")?;
|
||||
|
||||
// compute all seats (as integers)
|
||||
let mut seat_ints: Vec<usize> = data.lines().map(|code| seat_code_to_usize(code)).collect();
|
||||
seat_ints.sort_unstable();
|
||||
|
||||
// find the seat with the highest seat code
|
||||
let p1 = usize_to_seat_id(*seat_ints.last().unwrap());
|
||||
|
||||
// find the empty seat
|
||||
let mut empty = 0;
|
||||
for i in 0..seat_ints.len() - 1 {
|
||||
if seat_ints[i] + 2 == seat_ints[i + 1] {
|
||||
empty = seat_ints[i] + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let p2 = usize_to_seat_id(empty);
|
||||
|
||||
assert_eq!(p1, answers[0] as usize);
|
||||
assert_eq!(p2, answers[1] as usize);
|
||||
|
||||
Ok((p1, p2))
|
||||
}
|
||||
@@ -4,6 +4,7 @@ mod day01;
|
||||
mod day02;
|
||||
mod day03;
|
||||
mod day04;
|
||||
mod day05;
|
||||
mod utils;
|
||||
|
||||
// TODO
|
||||
@@ -18,5 +19,6 @@ fn main() -> Result<()> {
|
||||
println!("Day 02: {:?}", day02::run()?);
|
||||
println!("Day 03: {:?}", day03::run()?);
|
||||
println!("Day 04: {:?}", day04::run()?);
|
||||
println!("Day 05: {:?}", day05::run()?);
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user