From 6be2bc197968f2a338720769c1986e08b72fbe33 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 3 Mar 2021 01:20:25 +0800 Subject: [PATCH] day5b find within possible values --- day5b/src/main.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/day5b/src/main.rs b/day5b/src/main.rs index c513b01..fa53997 100644 --- a/day5b/src/main.rs +++ b/day5b/src/main.rs @@ -5,21 +5,22 @@ use std::io::Read; fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); - let passes: HashSet = input + let mut seats: Vec<_> = input .lines() - .map(|x| x.chars().rev().map(|y| match y { - 'F' => 0, - 'B' => 1, - 'L' => 0, - 'R' => 1, - _ => 0, - }).enumerate().fold(0, |acc, x| acc + (x.1 << x.0))) + .map(|l| { + l.bytes().fold(0, |a, b| { + a << 1 | if b == b'B' || b == b'R' { 1 } else { 0 } + }) + }) .collect(); - - for i in 0..(1 << 10) - 1 { - if passes.contains(&(i - 1)) && passes.contains(&(i + 1)) && !passes.contains(&i) { - println!("{}", i); - break; - } - } + seats.sort(); + + println!( + "{}", + seats + .windows(2) + .find(|s| s[1] - s[0] == 2) + .map(|s| s[0] + 1) + .unwrap() + ); }