Commit 4a70c586 authored by Kai Brandes's avatar Kai Brandes

add solutuon for 6a

parent 5b67570f
......@@ -12,4 +12,6 @@ fn main() {
println!("Solution for day 4b is: {}", adventofcode_2017::results::day_4_b());
println!("Solution for day 5a is: {}", adventofcode_2017::results::day_5_a());
println!("Solution for day 5b is: {}", adventofcode_2017::results::day_5_b());
println!("Solution for day 6a is: {}", adventofcode_2017::results::day_6_a());
println!("Solution for day 6b is: {}", adventofcode_2017::results::day_6_b());
}
\ No newline at end of file
4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3
\ No newline at end of file
use wasm_bindgen::prelude::*;
use std::collections::HashMap;
use std::collections::HashSet;
fn calc_next(v: Vec<u32>) -> (usize, u32) {
let result = v.iter().enumerate()
.fold(None, |old, it: (usize, &u32)|
match old {
None => Some((it.0, it.1)),
Some((a, b)) => if (b < it.1) { Some((it.0, it.1)) } else { Some((a, b)) }
},
).unwrap();
(result.0,result.1.clone())
}
fn as_string(v: &Vec<u32>) -> String {
v.iter()
.map(|a| a.to_string())
.collect::<Vec<String>>()
.join(",")
}
pub fn calc_day_6_a(input: String) -> u32 {
let mut current_state = input.split_whitespace()
.map(|a| a.parse::<u32>().unwrap())
.collect::<Vec<_>>();
let mut all_seen_states = HashSet::new();
let mut iteration = 0;
let mut cs: String = as_string(&current_state);
while !all_seen_states.contains(&cs) {
all_seen_states.insert(cs.clone());
let (i, val) = calc_next(current_state.clone());
current_state[i] = 0;
for pos in (i+1)..(i + val as usize +1) {
let current_index = pos % current_state.len();
current_state[current_index] = current_state[current_index] + 1;
}
iteration += 1;
cs = as_string(&current_state);
}
return iteration;
}
#[wasm_bindgen]
pub fn calc_day_6_b(input: String) -> u32 {
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_build_as_string() {
assert_eq!(String::from("1,2,3"), as_string(&vec!(1, 2, 3)))
}
#[test]
fn it_calc_next() {
assert_eq!((2, 3), calc_next(vec!(1, 2, 3)));
assert_eq!((1, 7), calc_next(vec!(1, 7, 3)));
assert_eq!((0, 18), calc_next(vec!(18, 7, 3)));
}
#[test]
fn it_should_calc_a() {
assert_eq!(5, calc_day_6_a(String::from("0 2 7 0")))
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;
pub mod results;
mod utils;
......
......@@ -3,6 +3,7 @@ use day2;
use day3;
use day4;
use day5;
use day6;
use std::fs::File;
use std::io::prelude::*;
......@@ -54,3 +55,11 @@ pub fn day_5_a() -> u32 {
pub fn day_5_b() -> u32 {
return day5::calc_day_5_b(parse_input_from_file(String::from("src/day5/b.txt")));
}
pub fn day_6_a() -> u32 {
return day6::calc_day_6_a(parse_input_from_file(String::from("src/day6/a.txt")));
}
pub fn day_6_b() -> u32 {
return day6::calc_day_6_b(parse_input_from_file(String::from("src/day6/b.txt")));
}
extern crate adventofcode_2017;
extern crate time;
#[test]
fn it_shoul_assert_result_of_day5_a() {
......
extern crate adventofcode_2017;
extern crate time;
#[test]
fn it_shoul_assert_result_of_day6_a() {
assert_eq!(6681, adventofcode_2017::results::day_6_a());
}
#[test]
fn it_shoul_assert_result_of_day6_b() {
assert_eq!(0, adventofcode_2017::results::day_6_b());
}
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