feat: day 14 (1)

This commit is contained in:
Lucy 2024-12-16 09:36:49 +01:00
parent 75d1327446
commit 8475909bd6
4 changed files with 96 additions and 0 deletions

View file

@ -18,6 +18,7 @@ full = [
"day11", "day11",
"day12", "day12",
"day13", "day13",
"day14",
] ]
default = ["full"] default = ["full"]
day1 = [] day1 = []
@ -33,6 +34,7 @@ day10 = []
day11 = [] day11 = []
day12 = [] day12 = []
day13 = [] day13 = []
day14 = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

12
inputs/day14/example.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

78
src/day14.rs Normal file
View file

@ -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<Self, Self::Err> {
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::<Vec<_>>();
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()
}

View file

@ -29,6 +29,8 @@ mod day11;
mod day12; mod day12;
#[cfg(feature = "day13")] #[cfg(feature = "day13")]
mod day13; mod day13;
#[cfg(feature = "day14")]
mod day14;
fn main() -> eyre::Result<()> { fn main() -> eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
@ -61,6 +63,8 @@ fn main() -> eyre::Result<()> {
solve_day(12, day12::pt1, day12::pt2); solve_day(12, day12::pt1, day12::pt2);
#[cfg(feature = "day13")] #[cfg(feature = "day13")]
solve_day(13, day13::pt1, day13::pt2); solve_day(13, day13::pt1, day13::pt2);
#[cfg(feature = "day14")]
solve_day(14, day14::pt1, day14::pt2);
Ok(()) Ok(())
} }