master
mitchellhansen 4 years ago
parent 24b0056f35
commit d96448bbef

@ -5,11 +5,13 @@ use crate::problem1::lib::Problem1;
use crate::problem2::lib::Problem2; use crate::problem2::lib::Problem2;
use crate::problem3::lib::Problem3; use crate::problem3::lib::Problem3;
use crate::problem4::lib::Problem4; use crate::problem4::lib::Problem4;
use crate::problem5::lib::Problem5;
mod problem1; mod problem1;
mod problem2; mod problem2;
mod problem3; mod problem3;
mod problem4; mod problem4;
mod problem5;
mod util; mod util;
@ -35,7 +37,11 @@ fn main() {
// problem3.run_part1(); // problem3.run_part1();
// problem3.run_part2(); // problem3.run_part2();
let problem4 = Problem4::new(&util::get_problem(4)); // let problem4 = Problem4::new(&util::get_problem(4));
problem4.run_part1(); // problem4.run_part1();
problem4.run_part2(); // problem4.run_part2();
let problem5 = Problem5::new(&util::get_problem(5));
problem5.run_part1();
problem5.run_part2();
} }

@ -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<Vec<Side>>,
}
#[derive(Clone, Copy, Debug)]
enum Side {
Left,
Right,
Front,
Back
}
fn split(side: Side, lower: &mut i32, upper: &mut i32) -> Option<i32> {
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<Side> = s.chars().map(|c| {
match c {
'F' => {Side::Front},
'B' => {Side::Back},
'L' => {Side::Left},
'R' => {Side::Right},
_ => {Side::Front}
}
}).collect::<Vec<Side>>();
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
*/

@ -0,0 +1 @@
pub mod lib;
Loading…
Cancel
Save