This commit is contained in:
Andrew Glaze 2023-12-29 11:30:39 -05:00
parent a0c9ce9882
commit f9d58356cd
2 changed files with 61 additions and 0 deletions

60
src/day15.rs Normal file
View 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);
}
}

View File

@ -1,3 +1,4 @@
mod day15;
mod day14;
mod day13;
mod day12;