Browse Source

first two levels work

master
Michael Preisach 8 years ago
commit
c51433804e
  1. 1
      day1.txt
  2. 1000
      day2.txt
  3. 44
      src/day1.rs
  4. 79
      src/day2.rs
  5. 112
      src/tests.rs

1
day1.txt

File diff suppressed because one or more lines are too long

1000
day2.txt

File diff suppressed because it is too large

44
src/day1.rs

@ -0,0 +1,44 @@
pub struct Data {
floors : Vec<i32>,
}
impl Data {
pub fn new() -> Data {
Data {
floors : Vec::new(),
}
}
pub fn parse(&mut self, input: String) {
let mut floor = 0;
for c in input.chars() {
match c {
'(' => {
floor += 1;
self.floors.push(floor);
},
')' => {
floor -= 1;
self.floors.push(floor);
},
_ => (),
}
}
}
pub fn floor(&mut self) -> i32 {
match self.floors.last() {
None => 0,
Some(val) => *val,
}
}
pub fn basement(&mut self) -> i32 {
for (i,_val) in self.floors.clone().into_iter().enumerate().filter(|&(_,_val)| _val == -1) {
return 1 + i as i32;
}
0
}
}

79
src/day2.rs

@ -0,0 +1,79 @@
pub struct Data {
boxes: Vec<Box>,
}
impl Data {
pub fn new() -> Data {
Data {
boxes : Vec::new(),
}
}
pub fn parse(&mut self, input: String) {
for s in input.split_whitespace() {
let data: Vec<&str> = s.split('x').collect();
if data.len() == 3 {
self.boxes.push(Box {
l: match data[0].parse::<i32>() {
Ok(val) => val,
Err(msg) => {
println!("{}", msg);
0
}
},
w: match data[1].parse::<i32>() {
Ok(val) => val,
Err(msg) => {
println!("{}", msg);
0
}
},
h: match data[2].parse::<i32>() {
Ok(val) => val,
Err(msg) => {
println!("{}", msg);
0
}
},
});
}
}
}
pub fn paper(&mut self) -> i32 {
let mut sum = 0;
for b in &self.boxes {
let mut squares = [b.l*b.w, b.w*b.h, b.h*b.l];
squares.sort();
sum += 3*squares[0] + 2*squares[1] + 2*squares[2];
}
sum
}
pub fn ribbon(&mut self) -> i32 {
let mut sum = 0;
for b in &self.boxes {
let mut squares = [b.l+b.w, b.w+b.h, b.h+b.l];
squares.sort();
sum += 2*squares[0] + b.w*b.l*b.h;
}
sum
}
}
struct Box {
l: i32,
w: i32,
h: i32,
}
impl Box {
pub fn new() -> Box {
Box {
l: 0,
w: 0,
h: 0,
}
}
}

112
src/tests.rs

@ -0,0 +1,112 @@
use super::*;
#[test]
fn day1_1() {
let mut input = String::from("(())");
let mut day1 = day1::Data::new();
day1.parse(input);
let mut res = day1.floor();
assert_eq!(res, 0);
input = String::from("()()");
day1 = day1::Data::new();
day1.parse(input);
res = day1.floor();
assert_eq!(res, 0);
}
#[test]
fn day1_2() {
let mut input = String::from("(((");
let mut day1 = day1::Data::new();
day1.parse(input);
let mut res = day1.floor();
assert_eq!(res, 3);
input = String::from("(()(()(");
day1 = day1::Data::new();
day1.parse(input);
res = day1.floor();
assert_eq!(res, 3);
}
#[test]
fn day1_3() {
let mut input = String::from("))(((((");
let mut day1 = day1::Data::new();
day1.parse(input);
let res = day1.floor();
assert_eq!(res, 3);
}
#[test]
fn day1_4() {
let mut input = String::from("())");
let mut day1 = day1::Data::new();
day1.parse(input);
let mut res = day1.floor();
assert_eq!(res, -1);
input = String::from("))(");
day1 = day1::Data::new();
day1.parse(input);
res = day1.floor();
assert_eq!(res, -1);
}
#[test]
fn day1_5() {
let mut input = String::from(")))");
let mut day1 = day1::Data::new();
day1.parse(input);
let mut res = day1.floor();
assert_eq!(res, -3);
input = String::from(")())())");
day1 = day1::Data::new();
day1.parse(input);
res = day1.floor();
assert_eq!(res, -3);
}
#[test]
fn day1_6() {
let mut input = String::from(")");
let mut day1 = day1::Data::new();
day1.parse(input);
let res = day1.basement();
assert_eq!(res, 1);
}
#[test]
fn day1_7() {
let mut input = String::from("()())");
let mut day1 = day1::Data::new();
day1.parse(input);
let res = day1.basement();
assert_eq!(res, 5);
}
#[test]
fn day2_1() {
let mut input = String::from("2x3x4");
let mut day2 = day2::Data::new();
day2.parse(input);
let mut res = day2.paper();
assert_eq!(res, 58);
res = day2.ribbon();
assert_eq!(res, 34);
}
#[test]
fn day2_2() {
let mut input = String::from("1x1x10");
let mut day2 = day2::Data::new();
day2.parse(input);
let mut res = day2.paper();
assert_eq!(res, 43);
res = day2.ribbon();
assert_eq!(res, 14);
}
Loading…
Cancel
Save