pre y2024d15

This commit is contained in:
2024-12-15 20:33:09 +01:00
parent f56780d1cf
commit 86c52a1a59
4 changed files with 276 additions and 0 deletions

27
y2024/src/bin/d15.rs Normal file
View File

@@ -0,0 +1,27 @@
use std::{fs, time::Instant};
use utils::time::get_elapsed_string;
use y2024::days::d15;
fn main() {
let now = Instant::now();
println!("Part 1:");
part1();
println!("Ran in {}", get_elapsed_string(now.elapsed()));
let now = Instant::now();
println!("Part 2:");
part2();
println!("Ran in {}", get_elapsed_string(now.elapsed()));
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/15_input.txt")).unwrap();
println!("{}", d15::process_part1(&content));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/15_input.txt")).unwrap();
println!("{}", d15::process_part2(&content));
}

176
y2024/src/days/d15.rs Normal file
View File

@@ -0,0 +1,176 @@
use std::error::Error;
use itertools::Itertools;
pub fn process_part1(input: &str) -> u32 {
let (grid, movements) = input.split_once("\n").unwrap();
let grid = parse_grid(grid);
let movements = parse_movements(movements);
for movement in movements {}
}
pub fn process_part2(input: &str) -> u32 {
0
}
fn parse_grid(input: &str) -> Vec<Vec<GridTile>> {
let mut grid_entities = Vec::new();
let mut robot = GridTile {
entity: GridTileType::Robot,
..Default::default()
};
input.lines().enumerate().for_each(|(yidx, line)| {
let mut row_entities = Vec::new();
line.chars().enumerate().for_each(|(xidx, chara)| {
if let Ok(entity) = GridTileType::from_character(chara) {
match entity {}
}
});
grid_entities.push(row_entities);
});
grid_entities
}
fn parse_movements(input: &str) -> Vec<Movement> {
input
.lines()
.map(|line| {
line.chars()
.map(|chara| Movement::try_from(chara).unwrap())
.collect_vec()
})
.concat()
}
fn move_up(robot: &mut GridTile, grid: &mut Vec<Vec<GridTile>>) {}
fn move_down(robot: &mut GridTile, grid: &mut Vec<Vec<GridTile>>) {}
fn move_left(robot: &mut GridTile, grid: &mut Vec<Vec<GridTile>>) {}
fn move_right(robot: &mut GridTile, grid: &mut Vec<Vec<GridTile>>) {}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum GridTileType {
Robot,
Crate,
Wall,
#[default]
Nothing,
}
impl GridTileType {
fn from_character(value: char) -> Result<GridTileType, Box<dyn Error>> {
match value {
'#' => Ok(Self::Wall),
'O' => Ok(Self::Crate),
'@' => Ok(Self::Robot),
'.' => Ok(Self::Nothing),
_ => Err(Box::from(format!("{value} is not a valid entity"))),
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct GridTile {
entity: GridTileType,
coords: (u32, u32),
}
impl GridTile {
fn get_gps(&self) -> u32 {
if self.entity == GridTileType::Crate {
self.coords.0 + self.coords.1 * 100
} else {
0
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Movement {
Up,
Down,
Left,
Right,
}
impl TryFrom<char> for Movement {
type Error = Box<dyn Error>;
fn try_from(value: char) -> std::result::Result<Movement, Box<dyn Error>> {
match value {
'^' => Ok(Self::Up),
'v' => Ok(Self::Down),
'<' => Ok(Self::Left),
'>' => Ok(Self::Right),
_ => Err(Box::from(format!("{value} is not a valid direction"))),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT_SMALL: &str = "#######
#...O..
#......";
const INPUT_MEDIUM: &str = "########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########
<^^>>>vv<v>>v<<";
const INPUT_LARGE: &str = "##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^";
#[test]
fn part1_small() {
let result = process_part1(INPUT_SMALL);
assert_eq!(result, 104);
}
#[test]
fn part1_medium() {
let result = process_part1(INPUT_MEDIUM);
assert_eq!(result, 2028);
}
#[test]
fn part1_large() {
let result = process_part1(INPUT_LARGE);
assert_eq!(result, 10092);
}
#[test]
fn part2() {
let result = process_part2(INPUT_SMALL);
assert_eq!(result, 0);
}
}

View File

@@ -17,3 +17,5 @@ pub mod d12;
pub mod d13;
pub mod d14;
pub mod d15;