Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.rs 1.95 KiB
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
struct Seat {
row: i64,
col: i64
}
impl Seat {
fn id(&self) -> i64 {
self.row * 8 + self.col
}
}
fn check_borading_passes(boarding_passes: &mut Vec<String>) -> Vec<Seat> {
boarding_passes
.iter()
.map(|boarding_pass| {
let (row_bits, col_bits) = boarding_pass.split_at(7);
let row_bits = &row_bits
.replace("F", "0")
.replace("B", "1");
let col_bits = &col_bits
.replace("L", "0")
.replace("R", "1");
let row = i64::from_str_radix(row_bits, 2).unwrap();
let col = i64::from_str_radix(col_bits, 2).unwrap();
Seat{
row: row,
col: col,
}
})
.collect()
}
fn main() -> io::Result<()> {
println!("Advent of Code 2020 – Day 5:");
let file = File::open("input.txt")?;
let reader = BufReader::new(file);
let mut input = reader
.lines()
.map(|l| {
String::from(l.unwrap())
})
.collect::<Vec<String>>();
let seats = check_borading_passes(&mut input);
let highest_seat_id = seats
.iter()
.map(|s| s.id())
.max()
.unwrap();
println!("Answer Part 1: {}", highest_seat_id);
let mut sorted_seat_ids = seats
.iter()
.map(|s| s.id())
.collect::<Vec<i64>>();
sorted_seat_ids.sort();
let mut last = sorted_seat_ids[0]-1;
let mut canidate = 0;
for id in sorted_seat_ids.iter() {
if last + 2 == *id {
canidate = last + 1;
break;
}
last = *id;
}
println!("Answer Part 2: {:?}", canidate);
Ok(())
}