I did a basic dynamic programming solution
fn last_spoken(input: &Vec<usize>, last: usize) -> usize { let mut turns_spoken: HashMap<usize, usize> = input .iter() .take(input.len() - 1) .enumerate() .map(|(i, x)| (*x, i)) .collect(); let mut last_spoken = *input.last().unwrap(); for i in input.len()..last { let newly_spoken = match turns_spoken.get(&last_spoken) { Some(last_time) => i - *last_time - 1, None => 0, }; turns_spoken.insert(last_spoken, i - 1); last_spoken = newly_spoken as usize; } return last_spoken; } #[aoc(day15, part1)] fn number_spoken(input: &Vec<usize>) -> usize { last_spoken(input, 2020) } #[aoc(day15, part2)] fn number_spoken_big(input: &Vec<usize>) -> usize { last_spoken(input, 30000000) }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I did a basic dynamic programming solution