Commit 4096cb9d authored by Kai Brandes's avatar Kai Brandes

extract method

parent f90e6c67
...@@ -11,7 +11,7 @@ fn calc_next(v: Vec<u32>) -> (usize, u32) { ...@@ -11,7 +11,7 @@ fn calc_next(v: Vec<u32>) -> (usize, u32) {
Some((a, b)) => if b < it.1 { Some((it.0, it.1)) } else { Some((a, b)) } Some((a, b)) => if b < it.1 { Some((it.0, it.1)) } else { Some((a, b)) }
}, },
).unwrap(); ).unwrap();
(result.0,result.1.clone()) (result.0, result.1.clone())
} }
fn as_string(v: &Vec<u32>) -> String { fn as_string(v: &Vec<u32>) -> String {
...@@ -21,10 +21,8 @@ fn as_string(v: &Vec<u32>) -> String { ...@@ -21,10 +21,8 @@ fn as_string(v: &Vec<u32>) -> String {
.join(",") .join(",")
} }
pub fn calc_day_6_a(input: String) -> u32 { fn detect_loop_for(start: &Vec<u32>) -> (u32, u32) {
let mut current_state = input.split_whitespace() let mut current_state = start.clone();
.map(|a| a.parse::<u32>().unwrap())
.collect::<Vec<_>>();
let mut all_seen_states = HashSet::new(); let mut all_seen_states = HashSet::new();
let mut iteration = 0; let mut iteration = 0;
...@@ -34,7 +32,7 @@ pub fn calc_day_6_a(input: String) -> u32 { ...@@ -34,7 +32,7 @@ pub fn calc_day_6_a(input: String) -> u32 {
let (i, val) = calc_next(current_state.clone()); let (i, val) = calc_next(current_state.clone());
current_state[i] = 0; current_state[i] = 0;
for pos in (i+1)..(i + val as usize +1) { for pos in (i + 1)..(i + val as usize + 1) {
let current_index = pos % current_state.len(); let current_index = pos % current_state.len();
current_state[current_index] = current_state[current_index] + 1; current_state[current_index] = current_state[current_index] + 1;
} }
...@@ -42,7 +40,15 @@ pub fn calc_day_6_a(input: String) -> u32 { ...@@ -42,7 +40,15 @@ pub fn calc_day_6_a(input: String) -> u32 {
iteration += 1; iteration += 1;
cs = as_string(&current_state); cs = as_string(&current_state);
} }
return iteration; return (iteration, 0);
}
pub fn calc_day_6_a(input: String) -> u32 {
let current_state = input.split_whitespace()
.map(|a| a.parse::<u32>().unwrap())
.collect::<Vec<_>>();
return detect_loop_for(&current_state).0;
} }
...@@ -72,6 +78,4 @@ mod tests { ...@@ -72,6 +78,4 @@ mod tests {
fn it_should_calc_a() { fn it_should_calc_a() {
assert_eq!(5, calc_day_6_a(String::from("0 2 7 0"))) assert_eq!(5, calc_day_6_a(String::from("0 2 7 0")))
} }
} }
\ No newline at end of file
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