From 8475909bd667cd69dafa91473c4f1b3f5b6104da Mon Sep 17 00:00:00 2001 From: Lucy Date: Mon, 16 Dec 2024 09:36:49 +0100 Subject: [PATCH] feat: day 14 (1) --- Cargo.toml | 2 ++ inputs/day14/example.txt | 12 +++++++ src/day14.rs | 78 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +++ 4 files changed, 96 insertions(+) create mode 100644 inputs/day14/example.txt create mode 100644 src/day14.rs diff --git a/Cargo.toml b/Cargo.toml index 9d6a5c0..1fc22b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ full = [ "day11", "day12", "day13", + "day14", ] default = ["full"] day1 = [] @@ -33,6 +34,7 @@ day10 = [] day11 = [] day12 = [] day13 = [] +day14 = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/inputs/day14/example.txt b/inputs/day14/example.txt new file mode 100644 index 0000000..72a324a --- /dev/null +++ b/inputs/day14/example.txt @@ -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 \ No newline at end of file diff --git a/src/day14.rs b/src/day14.rs new file mode 100644 index 0000000..bbf0390 --- /dev/null +++ b/src/day14.rs @@ -0,0 +1,78 @@ +#![allow( + clippy::cast_sign_loss, + clippy::cast_possible_wrap, + clippy::too_many_lines +)] + +use std::str::FromStr; + +use color_eyre::eyre::{self, OptionExt}; + +struct Robot { + x: isize, + y: isize, + vx: isize, + vy: isize, +} + +impl Robot { + fn move_robot(&mut self, dims: (isize, isize)) { + self.x += self.vx; + self.y += self.vy; + if self.x >= dims.0 { + self.x -= dims.0; + } + if self.y >= dims.1 { + self.y -= dims.1; + } + if self.x < 0 { + self.x += dims.0; + } + if self.y < 0 { + self.y += dims.1; + } + } +} +impl FromStr for Robot { + type Err = eyre::Error; + + fn from_str(s: &str) -> Result { + let (pos, vel) = s.split_once(' ').ok_or_eyre("line malformed")?; + let position = pos.split_once(',').ok_or_eyre("line malformed")?; + let velocity = vel.split_once(',').ok_or_eyre("line malformed")?; + Ok(Robot { + x: position + .0 + .split_once('=') + .ok_or_eyre("malformed position")? + .1 + .parse()?, + y: position.1.parse()?, + vx: velocity + .0 + .split_once('=') + .ok_or_eyre("malformed velocity")? + .1 + .parse()?, + vy: velocity.1.parse()?, + }) + } +} + +pub fn pt1(input: &str) -> usize { + let dims: (isize, isize) = (101, 103); + let mut robots = input.lines().flat_map(Robot::from_str).collect::>(); + for _ in 0..100 { + for robot in &mut robots { + robot.move_robot(dims); + } + } + robots.iter().filter(|r| r.x < 50 && r.y < 51).count() + * robots.iter().filter(|r| r.x > 50 && r.y < 51).count() + * robots.iter().filter(|r| r.x < 50 && r.y > 51).count() + * robots.iter().filter(|r| r.x > 50 && r.y > 51).count() +} + +pub fn pt2(_input: &str) -> String { + "lol no".into() +} diff --git a/src/main.rs b/src/main.rs index a2f12ba..3d51129 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,8 @@ mod day11; mod day12; #[cfg(feature = "day13")] mod day13; +#[cfg(feature = "day14")] +mod day14; fn main() -> eyre::Result<()> { color_eyre::install()?; @@ -61,6 +63,8 @@ fn main() -> eyre::Result<()> { solve_day(12, day12::pt1, day12::pt2); #[cfg(feature = "day13")] solve_day(13, day13::pt1, day13::pt2); + #[cfg(feature = "day14")] + solve_day(14, day14::pt1, day14::pt2); Ok(()) }