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 (min, max, xor) = input.lines().fold((u32::MAX, u32::MIN, 0), |(min, max, xor), line| {
17 let id = line.bytes().fold(0, |acc, b| (acc << 1) | (b == b'B' || b == b'R') as u32);
18 (min.min(id), max.max(id), xor ^ id)
19 });
20
21 Input { min, max, xor }
22}
23
24pub fn part1(input: &Input) -> u32 {
25 input.max
26}
27
28pub fn part2(input: &Input) -> u32 {
29 let rows = (input.min..=input.max).fold(0, |acc, b| acc ^ b);
30 rows ^ input.xor
31}