day15
This commit is contained in:
parent
a0c9ce9882
commit
f9d58356cd
60
src/day15.rs
Normal file
60
src/day15.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use itertools::Itertools;
|
||||
|
||||
|
||||
#[aoc_generator(day15)]
|
||||
fn parse(input: &str) -> Vec<u64> {
|
||||
input.split(',').map(str::parse::<u64>).map(Result::unwrap).collect_vec()
|
||||
}
|
||||
|
||||
#[aoc(day15, part1)]
|
||||
fn part1(input: &Vec<u64>) -> u64 {
|
||||
let mut list = input.iter().take(input.len() - 1).cloned().enumerate().map(|(i,v)| (v,i)).collect::<HashMap<_, _>>();
|
||||
let mut cur = *input.last().unwrap();
|
||||
|
||||
for turn in input.len() - 1..2020 - 1 {
|
||||
let mut next = 0;
|
||||
if let Some(last_turn) = list.get_mut(&cur) {
|
||||
next = (turn - *last_turn) as u64;
|
||||
}
|
||||
list.insert(cur, turn);
|
||||
cur = next;
|
||||
}
|
||||
cur
|
||||
}
|
||||
|
||||
#[aoc(day15, part2)]
|
||||
fn part2(input: &Vec<u64>) -> u64 {
|
||||
let mut list = input.iter().take(input.len() - 1).cloned().enumerate().map(|(i,v)| (v,i)).collect::<HashMap<_, _>>();
|
||||
let mut cur = *input.last().unwrap();
|
||||
|
||||
for turn in input.len() - 1..30000000 - 1 {
|
||||
let mut next = 0;
|
||||
if let Some(last_turn) = list.get_mut(&cur) {
|
||||
next = (turn - *last_turn) as u64;
|
||||
}
|
||||
list.insert(cur, turn);
|
||||
cur = next;
|
||||
}
|
||||
cur
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const EX: &str = r"0,3,6";
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(part1(&parse(EX)), 436);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(part2(&parse(EX)), 175594);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
mod day15;
|
||||
mod day14;
|
||||
mod day13;
|
||||
mod day12;
|
||||
|
Loading…
Reference in New Issue
Block a user