From d96448bbef00e89b7afc82e190ec9977bf119c57 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 5 Dec 2020 00:23:40 -0800 Subject: [PATCH] problem 5 --- src/main.rs | 12 ++- src/problem5/lib.rs | 228 ++++++++++++++++++++++++++++++++++++++++++++ src/problem5/mod.rs | 1 + 3 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 src/problem5/lib.rs create mode 100644 src/problem5/mod.rs diff --git a/src/main.rs b/src/main.rs index 8638ecd..520da7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,13 @@ use crate::problem1::lib::Problem1; use crate::problem2::lib::Problem2; use crate::problem3::lib::Problem3; use crate::problem4::lib::Problem4; +use crate::problem5::lib::Problem5; mod problem1; mod problem2; mod problem3; mod problem4; +mod problem5; mod util; @@ -35,7 +37,11 @@ fn main() { // problem3.run_part1(); // problem3.run_part2(); - let problem4 = Problem4::new(&util::get_problem(4)); - problem4.run_part1(); - problem4.run_part2(); + // let problem4 = Problem4::new(&util::get_problem(4)); + // problem4.run_part1(); + // problem4.run_part2(); + + let problem5 = Problem5::new(&util::get_problem(5)); + problem5.run_part1(); + problem5.run_part2(); } diff --git a/src/problem5/lib.rs b/src/problem5/lib.rs new file mode 100644 index 0000000..d018ef3 --- /dev/null +++ b/src/problem5/lib.rs @@ -0,0 +1,228 @@ +/* +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. +*/ + +use crate::Problem; + +pub struct Problem5 { + seats: Vec>, +} + +#[derive(Clone, Copy, Debug)] +enum Side { + Left, + Right, + Front, + Back +} + +fn split(side: Side, lower: &mut i32, upper: &mut i32) -> Option { + match side { +/* Side::Left => { + *upper = ((*upper - *lower + 1) / 2 + *lower); + //println!("{}, {}", *upper, *lower); + if (*upper - *lower).abs() == 1 { + Some(*lower) + } else { + None + } + } + Side::Right => { + *lower = ((*upper - *lower + 1) / 2 + *lower - 1); + //println!("{}, {}", *upper, *lower); + if (*upper - *lower).abs() == 1 { + Some(*upper - 1) + } else { + None + } + } + Side::Front => { + *upper = ((*upper - *lower + 1) / 2 + *lower); + // println!("{}, {}", *upper, *lower); + if (*upper - *lower).abs() == 1 { + Some(*lower) + } else { + None + } + } + Side::Back => { + *lower = ((*upper - *lower + 1) / 2 + *lower); + //println!("{}, {}", *upper, *lower); + if (*upper - *lower).abs() == 1 { + Some(*upper - 1) + } else { + None + } + }*/ + Side::Left => { + if (*upper - *lower).abs() == 1 { + Some(*lower) + } else { + *upper = ((*upper - *lower + 1) / 2 + *lower - 1); + println!("{}, {}", *upper, *lower); + None + } + } + Side::Right => { + if (*upper - *lower).abs() == 1 { + Some(*upper) + } else { + *lower = ((*upper - *lower + 1) / 2 + *lower); + println!("{}, {}", *upper, *lower); + None + } + } + Side::Front => { + if (*upper - *lower).abs() == 1 { + Some(*lower) + } else { + *upper = ((*upper - *lower + 1) / 2 + *lower - 1); + println!("{}, {}", *upper, *lower); + None + } + } + Side::Back => { + if (*upper - *lower).abs() == 1 { + Some(*upper) + } else { + *lower = ((*upper - *lower + 1) / 2 + *lower); + println!("{}, {}", *upper, *lower); + None + } + } + } +} + +impl Problem5 {} + +impl Problem for Problem5 { + fn new(input: &String) -> Self { + Problem5 { + seats: input + .split("\n") + .filter_map(|s| { + let s = s.trim(); + if !s.is_empty() { + let v : Vec = s.chars().map(|c| { + match c { + 'F' => {Side::Front}, + 'B' => {Side::Back}, + 'L' => {Side::Left}, + 'R' => {Side::Right}, + _ => {Side::Front} + } + }).collect::>(); + Some(v) + } else { + None + } + }).collect(), + } + } + + fn run_part1(&self) { + + let mut highest_id = 0; + + for s in &self.seats { + + let mut front = 0; + let mut back = 127; + let mut right = 7; + let mut left = 0; + + let mut row = 0; + let mut col = 0; + + for op in s { + match op { + Side::Left | Side::Right => { + match split(*op, &mut left, &mut right) { + None => {} + Some(v) => {col = v} + } + } + Side::Front | Side::Back => { + match split(*op, &mut front, &mut back) { + None => {} + Some(v) => {row = v} + } + } + } + } + + + println!("EndState: {:?}, {}, {}", s, front, back); + println!("Row, Col: {}, {}", row, col); + + let seat_id = row * 8 + col; + println!("SeatID : {}", seat_id); + + if seat_id > highest_id { + highest_id = seat_id; + } + } + + println!("{}", highest_id) + } + + fn run_part2(&self) { + + let mut seats = Vec::new(); + + for s in &self.seats { + + let mut front = 0; + let mut back = 127; + let mut right = 7; + let mut left = 0; + + let mut row = 0; + let mut col = 0; + + for op in s { + match op { + Side::Left | Side::Right => { + match split(*op, &mut left, &mut right) { + None => {} + Some(v) => {col = v} + } + } + Side::Front | Side::Back => { + match split(*op, &mut front, &mut back) { + None => {} + Some(v) => {row = v} + } + } + } + } + + let seat_id = row * 8 + col; + seats.push(seat_id); + } + seats.sort(); + println!("{:?}", seats); + let mut last = 0; + for i in seats { + if last == 0 { + last = i; + } + if i - last > 1 { + println!("{}", i); + } + last = i + } + } +} + +/* + +EndState: [Front, Front, Front, Back, Back, Front, Front, Left, Left, Left], 12, 13 +Row, Col: 12, 1 +SeatID : 97 + +EndState: [Front, Front, Front, Back, Back, Front, Front, Left, Left, Right], 12, 13 +Row, Col: 12, 1 +SeatID : 97 + + */ \ No newline at end of file diff --git a/src/problem5/mod.rs b/src/problem5/mod.rs new file mode 100644 index 0000000..965f28e --- /dev/null +++ b/src/problem5/mod.rs @@ -0,0 +1 @@ +pub mod lib;