Commit b3a70ef8 authored by Kai Brandes's avatar Kai Brandes

add solution for 6b

parent 0062edd2
4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3
\ No newline at end of file
......@@ -24,11 +24,11 @@ fn as_string(v: &Vec<u32>) -> String {
fn detect_loop_for(start: &Vec<u32>) -> (u32, u32) {
let mut current_state = start.clone();
let mut all_seen_states = HashSet::new();
let mut all_seen_states = HashMap::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());
while all_seen_states.get(&cs).is_none() {
all_seen_states.insert(cs.clone(), iteration);
let (i, val) = calc_next(current_state.clone());
current_state[i] = 0;
......@@ -40,7 +40,8 @@ fn detect_loop_for(start: &Vec<u32>) -> (u32, u32) {
iteration += 1;
cs = as_string(&current_state);
}
return (iteration, 0);
let cycle_size = iteration - *all_seen_states.get(&cs).unwrap();
return (iteration, cycle_size);
}
fn as_numbers(input: &String) -> Vec<u32> {
......@@ -57,7 +58,8 @@ pub fn calc_day_6_a(input: String) -> u32 {
#[wasm_bindgen]
pub fn calc_day_6_b(input: String) -> u32 {
0
let input = as_numbers(&input);
return detect_loop_for(&input).1;
}
#[cfg(test)]
......@@ -81,4 +83,9 @@ mod tests {
fn it_should_calc_a() {
assert_eq!(5, calc_day_6_a(String::from("0 2 7 0")))
}
#[test]
fn it_should_calc_b() {
assert_eq!(4, calc_day_6_b(String::from("0 2 7 0")))
}
}
\ No newline at end of file
......@@ -8,6 +8,6 @@ fn it_shoul_assert_result_of_day6_a() {
#[test]
fn it_shoul_assert_result_of_day6_b() {
assert_eq!(0, adventofcode_2017::results::day_6_b());
assert_eq!(2392, 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