parent
27051203b9
commit
6901d71abb
@ -0,0 +1,25 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
use failure::*;
|
||||
|
||||
pub fn solve(input: &str) -> Result<String, Error> {
|
||||
let mut jumps: HashMap<isize, isize> = input
|
||||
.trim()
|
||||
.lines()
|
||||
.enumerate()
|
||||
.map(|(i,x)| x.parse().map(|x| (i as isize, x)))
|
||||
.collect::<Result<_,_>>()?;
|
||||
let mut pc = 0;
|
||||
let mut steps = 0;
|
||||
while let Entry::Occupied(mut entry) = jumps.entry(pc) {
|
||||
let offset = entry.get_mut();
|
||||
pc += *offset;
|
||||
if *offset >= 3 {
|
||||
*offset -= 1;
|
||||
} else {
|
||||
*offset += 1;
|
||||
}
|
||||
steps += 1;
|
||||
}
|
||||
Ok(steps.to_string())
|
||||
}
|
Loading…
Reference in new issue