From 8cb64e29451a837b5f931ff5cf2843d5d6f2ad44 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Sat, 14 Dec 2024 18:02:04 +0200 Subject: [PATCH] feat: day14 --- input/day14/example | 12 +++++ src/day14/mod.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 8 +++ 3 files changed, 141 insertions(+) create mode 100644 input/day14/example create mode 100644 src/day14/mod.rs diff --git a/input/day14/example b/input/day14/example new file mode 100644 index 0000000..2455da4 --- /dev/null +++ b/input/day14/example @@ -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 diff --git a/src/day14/mod.rs b/src/day14/mod.rs new file mode 100644 index 0000000..b8bddbb --- /dev/null +++ b/src/day14/mod.rs @@ -0,0 +1,121 @@ +use std::{ + fs::File, + io::{BufRead, BufReader}, + path::Path, +}; + +pub fn solve(input: &Path) -> anyhow::Result<()> { + println!("part one: {}", part_one(input)?); + //println!("part two: {}", part_two(input)?); + + Ok(()) +} + +#[derive(Debug)] +struct R { + p: P, + v: P, +} + +impl R { + fn from_str(line: &str) -> Self { + let mut split = line.split_whitespace(); + let mut p_split = split.next().unwrap()[2..].split(","); + let mut v_split = split.next().unwrap()[2..].split(","); + Self { + p: P { + x: p_split.next().unwrap().parse().unwrap(), + y: p_split.next().unwrap().parse().unwrap(), + }, + v: P { + x: v_split.next().unwrap().parse().unwrap(), + y: v_split.next().unwrap().parse().unwrap(), + }, + } + } +} + +#[derive(Debug)] +struct P { + x: T, + y: T, +} + +fn _print_robots_on_map(robots: &[R], w: usize, h: usize, q: bool) { + for y in 0..h { + if q && y == h / 2 { + println!(); + continue; + } + for x in 0..w { + if q && x == w / 2 { + print!(" "); + continue; + } + let robots_on_tile = robots.iter().filter(|r| r.p.x == x && r.p.y == y).count(); + if robots_on_tile > 0 { + print!("{}", robots_on_tile); + } else { + //print!("."); + print!(" "); + } + } + println!(); + } +} + +fn part_one(input: &Path) -> anyhow::Result { + let reader = BufReader::new(File::open(input)?); + + let mut robots: Vec = vec![]; + for line in reader.lines() { + robots.push(R::from_str(&line?)); + } + + //let (w, h) = (11, 7); + let (w, h) = (101, 103); + + //print_robots_on_map(&robots, w, h, false); + for _ in 0..100 { + //for i in 0..10403 { + for robot in &mut robots { + let mut nx: i32 = robot.p.x as i32 + robot.v.x; + if nx >= w as i32 { + nx = (nx - w as i32).abs(); + } else if nx < 0 { + nx = (nx + w as i32).abs(); + } + let mut ny: i32 = robot.p.y as i32 + robot.v.y; + if ny >= h as i32 { + ny = (ny - h as i32).abs(); + } else if ny < 0 { + ny = (ny + h as i32).abs(); + } + robot.p.x = nx as usize; + robot.p.y = ny as usize; + } + //println!(); + //print_robots_on_map(&robots, w, h, true); + } + //println!(); + //print_robots_on_map(&robots, w, h, true); + + let q1 = robots + .iter() + .filter(|r| r.p.x < w / 2 && r.p.y < h / 2) + .count(); + let q2 = robots + .iter() + .filter(|r| r.p.x > w / 2 && r.p.y < h / 2) + .count(); + let q3 = robots + .iter() + .filter(|r| r.p.x > w / 2 && r.p.y > h / 2) + .count(); + let q4 = robots + .iter() + .filter(|r| r.p.x < w / 2 && r.p.y > h / 2) + .count(); + + Ok(q1 * q2 * q3 * q4) +} diff --git a/src/main.rs b/src/main.rs index 99c445c..27a691b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod day10; mod day11; mod day12; mod day13; +mod day14; mod day2; mod day3; mod day4; @@ -76,6 +77,10 @@ enum DayArgs { #[arg(short)] input: PathBuf, }, + Day14 { + #[arg(short)] + input: PathBuf, + }, } fn main() -> anyhow::Result<()> { @@ -121,6 +126,9 @@ fn main() -> anyhow::Result<()> { DayArgs::Day13 { input } => { day13::solve(&input)?; } + DayArgs::Day14 { input } => { + day14::solve(&input)?; + } } Ok(()) -- 2.44.1