From aa93c6860691e1433e22eb93fca2fa8e5a94288c Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Sat, 5 Dec 2020 10:36:30 +0100 Subject: [PATCH] 2020: day05: part 1 --- aoc2020/benches/bench.rs | 2 + aoc2020/input/day05.txt | 839 +++++++++++++++++++++++++++++++++++++++ aoc2020/src/day05.rs | 108 +++++ aoc2020/src/lib.rs | 1 + aoc2020/src/main.rs | 3 +- 5 files changed, 952 insertions(+), 1 deletion(-) create mode 100644 aoc2020/input/day05.txt create mode 100644 aoc2020/src/day05.rs diff --git a/aoc2020/benches/bench.rs b/aoc2020/benches/bench.rs index f2ae1e3..98dd746 100644 --- a/aoc2020/benches/bench.rs +++ b/aoc2020/benches/bench.rs @@ -4,12 +4,14 @@ use aoc2020::day01; use aoc2020::day02; use aoc2020::day03; use aoc2020::day04; +use aoc2020::day05; fn aoc2020_all(c: &mut Criterion) { c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day02", |b| b.iter(|| day02::run().unwrap())); c.bench_function("day03", |b| b.iter(|| day03::run().unwrap())); c.bench_function("day04", |b| b.iter(|| day04::run().unwrap())); + c.bench_function("day05", |b| b.iter(|| day05::run().unwrap())); } criterion_group! { diff --git a/aoc2020/input/day05.txt b/aoc2020/input/day05.txt new file mode 100644 index 0000000..6cd6f33 --- /dev/null +++ b/aoc2020/input/day05.txt @@ -0,0 +1,839 @@ +BFFBFBFLRL +BFBFBBBLRR +BFBFBFBLRR +BFBFFFFRLR +BBFFBBFRRR +FBBBFFFRLL +FFBFBFFLLL +BBFBFFFRLL +FBBBFFBLRR +FFFFBFFRRL +BFBFBFBRLL +FFBFFBBLLL +BFFBBFFRLR +FBBFFFFLRR +FBFBFFBRRR +FFBFFBBRRR +FBBBFFFRRL +FFBBBBFRRL +BFFBBBBLLL +BFBBFBBLLR +FBBFBFFLLR +FBBFBFFLRR +BFFFFBFLLL +FBBBBBBRRR +FBFFBBFLLR +BBFBFFBRLR +FFFFFBBLLR +FBBF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BFBBFFRRR +BFFBBBFRLL +BFFFFBFRLR +FFBFBFBLRL +FFFFBBFRRL +FFFFFFBRRL +FFFBFFBLRL +BFFBBBBRLL +FBFBBFBLRL +BBFFFBBRLL +FBFFBFBRLL +FFBBBBBRLL +FFBFBBFLRL +FFFFFBBRLR +FFBBFBFLLL +FFFFFFBLRR +BBFFFBFRRR +FBBFFFBRLL +FFFFBBBLRL +BFFFFBFLRR +BBFBFBFLLR +BFFFFBFRRL +BFBBFFFLLL +FBFFBFBRRR +BFFFBBFRLL +BFFFFBBLRL +FFFBBBFRRL +FBFFFBFRRR +FFBBBFFRLL +BFBFFBBLLL +BFBFBBBRRL +BFFFFBFLRL +FBBFBBBRRR +FBBFBBFLRL +FBBBFBBLRR +FBBBFFBRRR +BBFFBFBLRR +FFBBBFFLLR +BFBFBFFLLL +FFBBFFBLLR +FBFBBFFRLL +FBBFFBFLRL +FFBFBFBLRR +FFFBBFBLLR +FFBBFFBLRL +BFBBBBBRRR +BFBFBBFRLL +FFBBFFFLLR +FFBBFFBRLL +FFFBBBFRLR +BFFFBFFLRR +BFFBFBBRLR +BBFFBBBLLL +FBFBFFBLLR +FFFBBFFRRR +BBFBFFFLLR +FBBFBFFRLL +FFBBFBBRLL +FBBBFBFRLL +BFBBFBBRRL +BFBFFFFRLL +BFFFBBBLRR +FFBBBBBLRR +BBFFFBBRLR +BFFBBBFRRL +BBFFBFBLRL +BFFBBBBRRL diff --git a/aoc2020/src/day05.rs b/aoc2020/src/day05.rs new file mode 100644 index 0000000..93ccbb9 --- /dev/null +++ b/aoc2020/src/day05.rs @@ -0,0 +1,108 @@ +use std::fmt::Write; +use std::str::FromStr; + +use aoc::err; + +const INPUT: &str = include_str!("../input/day05.txt"); + +pub fn run() -> aoc::Result { + let mut res = String::with_capacity(128); + + writeln!(res, "part 1: {}", part1(INPUT)?)?; + + Ok(res) +} + +fn part1(input: &str) -> aoc::Result { + let seats = input + .lines() + .map(|line| line.parse()) + .collect::>>() + .map_err(|e| err!("{}", e))?; + + seats + .iter() + .map(|seat| seat.id()) + .max() + .ok_or_else(|| err!("0 seats processed")) +} + +#[derive(Debug, PartialEq, Eq)] +struct Seat { + row: usize, + column: usize, +} + +impl Seat { + fn id(&self) -> usize { + self.row * 8 + self.column + } +} + +impl FromStr for Seat { + type Err = aoc::Error; + + fn from_str(s: &str) -> aoc::Result { + if s.len() != 10 { + return Err(err!("Seat encoding must be 10 chars long: {}", s)); + } + + let (mut row_begin, mut row_end) = (0, 128); + let (mut col_begin, mut col_end) = (0, 8); + for c in s.chars() { + let row_range = (row_end - row_begin) / 2; + let col_range = (col_end - col_begin) / 2; + match c { + 'B' => { + row_begin += row_range; + } + 'F' => { + row_end -= row_range; + } + 'L' => { + col_end -= col_range; + } + 'R' => { + col_begin += col_range; + } + _ => return Err(err!("Wrong char while decoding seat: `{}`", c)), + } + } + + Ok(Self { + row: row_begin, + column: col_begin, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_provided() { + let seat: Seat = "BFFFBBFRRR".parse().unwrap(); + assert_eq!(seat, Seat { row: 70, column: 7 }); + assert_eq!(seat.id(), 567); + + let seat: Seat = "FFFBBBFRRR".parse().unwrap(); + assert_eq!(seat, Seat { row: 14, column: 7 }); + assert_eq!(seat.id(), 119); + + let seat: Seat = "BBFFBBFRLL".parse().unwrap(); + assert_eq!( + seat, + Seat { + row: 102, + column: 4 + } + ); + assert_eq!(seat.id(), 820); + } + + #[test] + fn part1_real() { + assert_eq!(part1(INPUT).unwrap(), 850); + } +} diff --git a/aoc2020/src/lib.rs b/aoc2020/src/lib.rs index e051fbd..f290640 100644 --- a/aoc2020/src/lib.rs +++ b/aoc2020/src/lib.rs @@ -2,3 +2,4 @@ pub mod day01; pub mod day02; pub mod day03; pub mod day04; +pub mod day05; diff --git a/aoc2020/src/main.rs b/aoc2020/src/main.rs index f49c643..b93351f 100644 --- a/aoc2020/src/main.rs +++ b/aoc2020/src/main.rs @@ -5,9 +5,10 @@ use aoc2020::day01; use aoc2020::day02; use aoc2020::day03; use aoc2020::day04; +use aoc2020::day05; fn main() -> Result<()> { - let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run]; + let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run, day05::run]; aoc::run(days) }