-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.rs
64 lines (57 loc) · 1.42 KB
/
05.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
fn main() {
let input = include_str!("../../input/05.txt");
println!("Part 1: {}", run(input, false));
println!("Part 2: {}", run(input, true));
}
fn parse_space_partition(input: &str) -> i64 {
i64::from(input.bytes().fold(0, |seat_id, byte| {
u16::from(b"BR".contains(&byte)) + (seat_id << 1)
}))
}
fn run(input: &str, part_two: bool) -> i64 {
if part_two {
part_2(input)
} else {
part_1(input)
}
}
fn part_1(input: &str) -> i64 {
input
.lines()
.map(parse_space_partition)
.max()
.expect("Missing seat")
}
fn part_2(input: &str) -> i64 {
let mut occupied = [false; 1024];
let mut max_occupied = 0;
for line in input.lines() {
let seat_id = parse_space_partition(line);
occupied[seat_id as usize] = true;
if seat_id > max_occupied {
max_occupied = seat_id;
}
}
(0..max_occupied)
.rev()
.find(|&seat_id| !occupied[seat_id as usize])
.unwrap()
}
#[cfg(test)]
mod day_5_tests {
use super::*;
static INPUT: &str = "FBFBBFFRLR
BFFFBBFRRR
FFFBBBFRRR
BBFFBBFRLL";
#[test]
fn test_part_1() {
assert!(part_1(INPUT) == 820);
assert!(part_1(include_str!("../../input/05.txt")) == 901);
}
#[test]
fn test_part_2() {
assert!(part_2(INPUT) == 819);
assert!(part_2(include_str!("../../input/05.txt")) == 661);
}
}