Commit 1dff3d65 authored by Kai Brandes's avatar Kai Brandes

add tests and solution for 3a

parent 3b8a65e7
......@@ -35,7 +35,7 @@ pub fn calc_day_2_a(input: String) -> u32 {
let lines: Vec<&str> = input.split_terminator("\n").collect();
return lines.iter()
.map(line_to_digits)
.fold(0, |old_sum, line: Vec<u32>| old_sum + min_max_line_diff(line));
.fold(0, |old_sum, digits: Vec<u32>| old_sum + min_max_line_diff(digits));
}
......@@ -44,7 +44,6 @@ pub fn calc_day_2_b(input: String) -> u32 {
return lines.iter()
.map(line_to_digits)
.fold(0, |old_sum, digits: Vec<u32>| old_sum + division_val_from_line(digits));
0
}
......
fn rel_position_and_circle(pos: &u32) -> (u32, u32) {
let mut c = pos.clone() as i32;
let mut circle_nr = 0;
loop {
let nr_elements_in_circle = if circle_nr == 0 { 1 } else { (circle_nr * 8) as i32 };
if c <= nr_elements_in_circle {
break;
} else {
c -= nr_elements_in_circle;
circle_nr += 1;
}
}
(c.abs() as u32 - 1, circle_nr)
}
pub fn calc_day_3_a(input: String) -> u32 {
let position = input.parse::<u32>().unwrap();
let (rel_pos, circle_nr) = rel_position_and_circle(&position);
let width = 1 + circle_nr * 2;
let center = (width / 2, width / 2);
let mut start_vector = (width - 1, width - 2);
let mut direction = 0;
for p in 0..rel_pos {
if ((p + 1) % (width - 1) == 0 && p > 0) {
direction += 1;
}
if direction == 0 {
start_vector = (start_vector.0, start_vector.1 - 1)
} else if direction == 1 {
start_vector = (start_vector.0 - 1, start_vector.1)
} else if (direction == 2) {
start_vector = (start_vector.0, start_vector.1 + 1)
} else if (direction == 3) {
start_vector = (start_vector.0 + 1, start_vector.1)
}
}
((start_vector.0 as i32 - center.0 as i32).abs() + (start_vector.1 as i32 - center.1 as i32).abs()) as u32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_calcs_some_distances() {
assert_eq!(2, calc_day_3_a(String::from("11")));
assert_eq!(3, calc_day_3_a(String::from("10")));
assert_eq!(6, calc_day_3_a(String::from("49")));
assert_eq!(6, calc_day_3_a(String::from("37")));
assert_eq!(6, calc_day_3_a(String::from("31")));
assert_eq!(2, calc_day_3_a(String::from("23")));
assert_eq!(430, calc_day_3_a(String::from("312051")));
}
#[test]
fn it_calcs_position_and_circle() {
assert_eq!((0, 0), rel_position_and_circle(&1));
assert_eq!((0, 1), rel_position_and_circle(&2));
assert_eq!((1, 1), rel_position_and_circle(&3));
assert_eq!((2, 1), rel_position_and_circle(&4));
assert_eq!((3, 1), rel_position_and_circle(&5));
assert_eq!((4, 1), rel_position_and_circle(&6));
assert_eq!((5, 1), rel_position_and_circle(&7));
assert_eq!((6, 1), rel_position_and_circle(&8));
assert_eq!((7, 1), rel_position_and_circle(&9));
assert_eq!((0, 2), rel_position_and_circle(&10));
assert_eq!((1, 2), rel_position_and_circle(&11));
}
}
\ No newline at end of file
pub mod day1;
pub mod day2;
pub mod day3;
pub mod results;
use day1;
use day2;
use day3;
use std::fs::File;
use std::io::prelude::*;
......@@ -26,4 +27,8 @@ pub fn day_2_a() -> u32 {
pub fn day_2_b() -> u32 {
return day2::calc_day_2_b(parse_input_from_file(String::from("src/day2/b.txt")));
}
pub fn day_3_a() -> u32 {
return day3::calc_day_3_a(String::from("312051"));
}
\ No newline at end of file
extern crate adventofcode_2017;
#[test]
fn it_shoul_assert_result_of_day3_a() {
assert_eq!(430, adventofcode_2017::results::day_3_a());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment