Advent of Code solutions

Day 10 & 11

bwc9876.dev 1e6757ea 443eb038

verified
+187 -12
+69 -6
years/2024/src/day_10.rs
··· 1 + use std::collections::HashSet; 1 2 2 - use advent_core::{Day, day_stuff, ex_for_day}; 3 + use advent_core::{day_stuff, ex_for_day, Day}; 4 + use utils::{dir::CARDINALS, pos::Position}; 3 5 4 6 pub struct Day10; 5 7 8 + #[derive(Clone, Debug)] 9 + pub struct Tile(usize); 10 + 11 + impl From<char> for Tile { 12 + fn from(value: char) -> Self { 13 + Self(value.to_string().parse().unwrap()) 14 + } 15 + } 16 + 17 + pub type Grid = utils::grid::Grid<Tile>; 18 + 19 + fn get_asc(g: &Grid, pos: Position, num: usize) -> impl Iterator<Item = Position> + use<'_> { 20 + g.relatives(pos, &CARDINALS) 21 + .filter_map(move |(_, r_pos, t)| if t.0 == num + 1 { Some(r_pos) } else { None }) 22 + } 23 + 6 24 impl Day for Day10 { 25 + day_stuff!(10, "36", "81", Grid); 7 26 8 - day_stuff!(10, "", ""); 27 + fn part_1(input: Self::Input) -> Option<String> { 28 + let starts = input 29 + .iter() 30 + .filter_map(|(p, t)| if t.0 == 0 { Some(p) } else { None }); 31 + 32 + let mut tot = 0; 33 + 34 + for start in starts { 35 + let mut num = 0; 36 + let mut nxt = vec![start]; 37 + while num != 9 { 38 + nxt = nxt 39 + .into_iter() 40 + .flat_map(|o| get_asc(&input, o, num)) 41 + .collect::<Vec<_>>(); 42 + num += 1; 43 + } 44 + 45 + let hsh = nxt.into_iter().collect::<HashSet<_>>(); 46 + 47 + tot += hsh.len(); 48 + } 49 + 50 + Some(tot.to_string()) 51 + } 52 + 53 + fn part_2(input: Self::Input) -> Option<String> { 54 + let starts = input 55 + .iter() 56 + .filter_map(|(p, t)| if t.0 == 0 { Some(p) } else { None }); 57 + 58 + let mut tot = 0; 59 + 60 + for start in starts { 61 + let mut num = 0; 62 + let mut nxt = vec![start]; 63 + while num != 9 { 64 + nxt = nxt 65 + .into_iter() 66 + .flat_map(|o| get_asc(&input, o, num)) 67 + .collect::<Vec<_>>(); 68 + num += 1; 69 + } 9 70 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 71 + tot += nxt.len(); 72 + } 73 + 74 + Some(tot.to_string()) 12 75 } 13 76 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 77 + fn parse_input(input: &str) -> Self::Input { 78 + Grid::parse(input.trim()) 16 79 } 17 80 }
+100 -6
years/2024/src/day_11.rs
··· 1 + use std::collections::HashMap; 1 2 2 - use advent_core::{Day, day_stuff, ex_for_day}; 3 + use advent_core::{day_stuff, ex_for_day, Day}; 3 4 4 5 pub struct Day11; 5 6 6 7 impl Day for Day11 { 8 + day_stuff!(11, "55312", "65601038650482", Vec<u128>); 7 9 8 - day_stuff!(11, "", ""); 10 + fn part_1(input: Self::Input) -> Option<String> { 11 + let l = input.len(); 12 + let mut stone_map = input 13 + .into_iter() 14 + .fold(HashMap::with_capacity(l), |mut acc, stone| { 15 + acc.entry(stone).and_modify(|c| *c += 1).or_insert(1); 16 + acc 17 + }); 18 + 19 + for _i in 0..25 { 20 + let mut new_map = HashMap::with_capacity(stone_map.len()); 21 + 22 + for (num, count) in stone_map { 23 + if num == 0 { 24 + new_map 25 + .entry(1) 26 + .and_modify(|c| *c += count) 27 + .or_insert(count); 28 + } else { 29 + let ss = num.to_string(); 30 + let l = ss.len(); 31 + if l % 2 == 0 { 32 + let (num1, num2) = 33 + (ss[..l / 2].parse().unwrap(), ss[l / 2..].parse().unwrap()); 34 + new_map 35 + .entry(num1) 36 + .and_modify(|c| *c += count) 37 + .or_insert(count); 38 + new_map 39 + .entry(num2) 40 + .and_modify(|c| *c += count) 41 + .or_insert(count); 42 + } else { 43 + new_map 44 + .entry(num * 2024) 45 + .and_modify(|c| *c += count) 46 + .or_insert(count); 47 + } 48 + } 49 + } 9 50 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 51 + stone_map = new_map; 52 + } 53 + 54 + Some(stone_map.values().sum::<u128>().to_string()) 12 55 } 13 56 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 57 + fn part_2(input: Self::Input) -> Option<String> { 58 + let l = input.len(); 59 + let mut stone_map = input 60 + .into_iter() 61 + .fold(HashMap::with_capacity(l), |mut acc, stone| { 62 + acc.entry(stone).and_modify(|c| *c += 1).or_insert(1); 63 + acc 64 + }); 65 + 66 + for _i in 0..75 { 67 + let mut new_map = HashMap::with_capacity(stone_map.len()); 68 + 69 + for (num, count) in stone_map { 70 + if num == 0 { 71 + new_map 72 + .entry(1) 73 + .and_modify(|c| *c += count) 74 + .or_insert(count); 75 + } else { 76 + let ss = num.to_string(); 77 + let l = ss.len(); 78 + if l % 2 == 0 { 79 + let (num1, num2) = 80 + (ss[..l / 2].parse().unwrap(), ss[l / 2..].parse().unwrap()); 81 + new_map 82 + .entry(num1) 83 + .and_modify(|c| *c += count) 84 + .or_insert(count); 85 + new_map 86 + .entry(num2) 87 + .and_modify(|c| *c += count) 88 + .or_insert(count); 89 + } else { 90 + new_map 91 + .entry(num * 2024) 92 + .and_modify(|c| *c += count) 93 + .or_insert(count); 94 + } 95 + } 96 + } 97 + 98 + stone_map = new_map; 99 + } 100 + 101 + Some(stone_map.values().sum::<u128>().to_string()) 102 + } 103 + 104 + fn parse_input(input: &str) -> Self::Input { 105 + input 106 + .trim() 107 + .split(" ") 108 + .map(|n| n.parse::<u128>().unwrap()) 109 + .collect() 16 110 } 17 111 }
+8
years/2024/src/examples/day_10/1.txt
··· 1 + 89010123 2 + 78121874 3 + 87430965 4 + 96549874 5 + 45678903 6 + 32019012 7 + 01329801 8 + 10456732
+8
years/2024/src/examples/day_10/2.txt
··· 1 + 89010123 2 + 78121874 3 + 87430965 4 + 96549874 5 + 45678903 6 + 32019012 7 + 01329801 8 + 10456732
+1
years/2024/src/examples/day_11/1.txt
··· 1 + 125 17
+1
years/2024/src/examples/day_11/2.txt
··· 1 + 125 17