aoc/year2020/
day05.rs

1//! # Binary Boarding
2//!
3//! The entire part one description is an obfuscated way to describe that each seat id is a 10 bit
4//! binary number, where `B` and `R` mean a 1 bit in that position and `F` and `L` mean a 0 bit.
5//!
6//! To solve part two we can have a little fun. Since we know that only a single seat is missing
7//! if we [XOR](https://en.wikipedia.org/wiki/XOR_gate) together all the seat ids from
8//! `min` to `max` then XOR with the actual seat ids, the result will be our missing seat id.
9pub struct Input {
10    min: u32,
11    max: u32,
12    xor: u32,
13}
14
15pub fn parse(input: &str) -> Input {
16    let mut min = u32::MAX;
17    let mut max = u32::MIN;
18    let mut xor = 0;
19
20    for line in input.lines() {
21        let id = line.bytes().fold(0, |acc, b| (acc << 1) | (b == b'B' || b == b'R') as u32);
22        min = min.min(id);
23        max = max.max(id);
24        xor ^= id;
25    }
26
27    Input { min, max, xor }
28}
29
30pub fn part1(input: &Input) -> u32 {
31    input.max
32}
33
34pub fn part2(input: &Input) -> u32 {
35    let rows = (input.min..=input.max).fold(0, |acc, b| acc ^ b);
36    rows ^ input.xor
37}