solution: day 14

This commit is contained in:
Matej Janezic 2024-12-15 00:11:58 +01:00
parent 0000017050
commit 000001801f
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
2 changed files with 119 additions and 0 deletions

12
data/examples/14.txt Normal file
View File

@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3

107
src/bin/14.rs Normal file
View File

@ -0,0 +1,107 @@
use hashbrown::HashMap;
use num_integer::Integer;
use regex::Regex;
fn parse_input(input: &str) -> Vec<((isize, isize), (isize, isize))> {
let r = Regex::new(r"p=(\d+),(\d+) v=(-?\d+),(-?\d+)").unwrap();
let mut parsed = Vec::new();
for line in input.lines() {
let c = r.captures(line).unwrap();
let j: isize = c.get(1).unwrap().as_str().parse().unwrap();
let i: isize = c.get(2).unwrap().as_str().parse().unwrap();
let y: isize = c.get(3).unwrap().as_str().parse().unwrap();
let x: isize = c.get(4).unwrap().as_str().parse().unwrap();
parsed.push(((i, j), (x, y)));
}
parsed
}
pub fn part_one(input: &str) -> Option<usize> {
let parsed = parse_input(input);
let mut r = [0; 4];
let h = 103;
let w = 101;
for ((pi, pj), (vi, vj)) in parsed {
let ni = (pi + vi * 100).rem_euclid(h);
let nj = (pj + vj * 100).rem_euclid(w);
if ni < h / 2 && nj > w / 2 {
r[0] += 1;
}
if ni < h / 2 && nj < w / 2 {
r[1] += 1;
}
if ni > h / 2 && nj < w / 2 {
r[2] += 1;
}
if ni > h / 2 && nj > w / 2 {
r[3] += 1;
}
}
r.into_iter().product::<usize>().into()
}
pub fn part_two(input: &str) -> Option<usize> {
let pv = parse_input(input);
let h = 103;
let w = 101;
let mut n = 1;
// hardcoded side lengths of the "easter egg"
let th = 33;
let tw = 31;
for ((pi, pj), (vi, vj)) in pv.iter().copied() {
let mut m = 1;
loop {
let ni = (pi + vi * m).rem_euclid(h);
let nj = (pj + vj * m).rem_euclid(w);
if (ni, nj) == (pi, pj) {
break;
}
m += 1;
}
n = n.lcm(&m);
}
let mut sh = HashMap::new();
let mut sw = HashMap::new();
for m in 0..n {
sh.clear();
sw.clear();
for ((pi, pj), (vi, vj)) in pv.iter().copied() {
let ni = (pi + vi * m).rem_euclid(h);
let nj = (pj + vj * m).rem_euclid(w);
*sw.entry(ni).or_default() += 1_usize;
*sh.entry(nj).or_default() += 1_usize;
}
let ch = sh.values().filter(|&&x: &&usize| x >= th).count();
let cw = sw.values().filter(|&&x: &&usize| x >= tw).count();
if ch >= 2 && cw >= 2 {
return Some(n as usize);
}
}
None
}
aoc::solution!(14);