-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.rs
37 lines (30 loc) · 877 Bytes
/
day05.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::fs;
fn parse(input: &str) -> u32 {
input
.chars()
.map(|c| match c {
'F' | 'L' => 0,
'B' | 'R' => 1,
_ => panic!("Invalid input!"),
})
.fold(0, |a, b| a << 1 | b)
}
struct Solution;
impl Solution {
fn part1(seat_ids: &[u32]) -> u32 {
*seat_ids.iter().max().unwrap()
}
fn part2(seat_ids: &mut Vec<u32>) -> Option<u32> {
seat_ids.sort_unstable();
seat_ids.windows(2).find(|w| w[0] + 1 != w[1]).map(|w| w[0] + 1)
}
}
fn main() {
let input = fs::read_to_string("./input/day05.txt").expect("File not found!");
let mut seat_ids: Vec<u32> = input.trim().lines().map(parse).collect();
println!("p1: {}", Solution::part1(&seat_ids));
println!(
"p2: {}",
Solution::part2(&mut seat_ids).expect("Empty seat not found!")
);
}