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
65
66
67
68
69
70
71
72
73
74
75
use crate::input::Input;
type SeatId = u16;
fn parse_seat_specifier(specifier: &str) -> SeatId {
specifier
.chars()
.map(|c| SeatId::from(matches!(c, 'B' | 'R')))
.enumerate()
.map(|(bit_index, bit_flag)| bit_flag << (9 - bit_index))
.sum()
}
pub fn solve(input: &mut Input) -> Result<SeatId, String> {
if let Some(invalid_line_idx) = input.text.lines().enumerate().find_map(|(line_idx, line)| {
if line.len() != 10
|| !line[0..7].chars().all(|c| matches!(c, 'F' | 'B'))
|| !line[7..10].chars().all(|c| matches!(c, 'L' | 'R'))
{
Some(line_idx)
} else {
None
}
}) {
return Err(format!(
"Line {}: Not expected format (7 'F' or 'B' characters followed by 3 'L' or 'R' ones)",
invalid_line_idx
));
}
let seat_ids = input.text.lines().map(parse_seat_specifier);
if input.is_part_one() {
seat_ids
.max()
.ok_or_else(|| "No seats in input".to_string())
} else {
let mut seats = [0_u8; 127];
for seat_id in seat_ids {
let (row, col) = (seat_id / 8, seat_id % 8);
seats[row as usize] |= 1 << col;
}
for this_seat_id in 0..SeatId::MAX {
let (row, col) = (this_seat_id / 8, this_seat_id % 8);
let this_seat_occupied = seats[row as usize] & (1 << col) > 0;
if this_seat_occupied {
let next_seat_id = this_seat_id + 1;
let (row, col) = (next_seat_id / 8, next_seat_id % 8);
let next_seat_occupied = seats[row as usize] & (1 << col) == 0;
if next_seat_occupied {
return Ok(next_seat_id);
}
}
}
Err("No gap found".to_string())
}
}
#[test]
pub fn tests() {
use crate::input::{test_part_one, test_part_two};
let real_input = include_str!("day05_input.txt");
test_part_one!(real_input => 828);
test_part_two!(real_input => 565);
}