1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
24386b750b day 15 part 1 2024-12-15 11:26:29 +01:00
37874ebba9 day 14 make faster
cleanup

cleanup
2024-12-15 11:26:29 +01:00
6 changed files with 79 additions and 11 deletions

View File

@@ -64,7 +64,7 @@ fn part2(input: &str) -> RiddleResult {
} else { } else {
free.insert(head, l); free.insert(head, l);
} }
head += l as usize; head += l;
} }
for file in files.iter_mut().rev() { for file in files.iter_mut().rev() {
let (start_index, length, _file_id) = *file; let (start_index, length, _file_id) = *file;
@@ -73,7 +73,7 @@ fn part2(input: &str) -> RiddleResult {
.take_while(|f| *f.0 < start_index) .take_while(|f| *f.0 < start_index)
.find(|f| *f.1 >= length); .find(|f| *f.1 >= length);
if let Some((&free_start, &free_length)) = found.clone() { if let Some((&free_start, &free_length)) = found {
free.remove(&free_start); free.remove(&free_start);
free.insert(start_index, length); free.insert(start_index, length);
file.0 = free_start; file.0 = free_start;

View File

@@ -2,7 +2,6 @@ use std::{
collections::HashSet, collections::HashSet,
fs::read_to_string, fs::read_to_string,
io::{self, BufRead}, io::{self, BufRead},
ops::Sub,
}; };
use itertools::Itertools; use itertools::Itertools;
@@ -135,8 +134,6 @@ fn printr(robots: &[Robot], width: i64, height: i64) {
mod test { mod test {
use crate::day14::solve_part1; use crate::day14::solve_part1;
use super::part2;
const TEST_INPUT: &str = r"p=0,4 v=3,-3 const TEST_INPUT: &str = r"p=0,4 v=3,-3
p=6,3 v=-1,-3 p=6,3 v=-1,-3
p=10,3 v=-1,2 p=10,3 v=-1,2
@@ -155,9 +152,4 @@ p=9,5 v=-3,-3
fn test1() { fn test1() {
assert_eq!(solve_part1(TEST_INPUT, 100, 11, 7), 12); assert_eq!(solve_part1(TEST_INPUT, 100, 11, 7), 12);
} }
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 0);
}
} }

75
src/day15.rs Normal file
View File

@@ -0,0 +1,75 @@
use std::fs::read_to_string;
use crate::utils::grid::Grid;
pub fn day_main() {
let input = read_to_string("input/day15.txt").unwrap();
let input = input.trim();
println!(" part1: {}", part1(input));
println!(" part2: {}", part2(input));
}
type RiddleResult = i64;
fn part1(input: &str) -> RiddleResult {
let (grid, movements) = input.split_once("\n\n").unwrap();
let mut grid = Grid::parse(grid);
let mut robot = grid.entries().find(|(_r, c)| **c == '@').unwrap().0;
let directions = |d| match d {
'^' => (0, -1),
'v' => (0, 1),
'<' => (-1, 0),
'>' => (1, 0),
_ => panic!(),
};
for m in movements.chars().filter(|c| *c != '\n') {
let dir = directions(m);
let space = (1..)
.map(|i| (robot.0 + i * dir.0, robot.1 + i * dir.1))
.take_while(|p| grid.get(*p).is_some() && (grid[*p] == '.' || grid[*p] == 'O'))
.find(|p| grid[*p] == '.');
if let Some(p) = space {
if (p.0 - robot.0).abs() + (p.1 - robot.1).abs() > 1 {
// this means: the free spot is not a direct neighbor of the robot, i.e. there are boxes
grid[p] = 'O';
}
grid[robot] = '.';
robot = (robot.0 + dir.0, robot.1 + dir.1);
grid[robot] = '@';
}
}
grid.entries()
.filter(|(_, c)| **c == 'O')
.map(|((x, y), _)| y * 100 + x)
.sum()
}
fn part2(_input: &str) -> RiddleResult {
0
}
#[cfg(test)]
mod test {
use super::{part1, part2};
const TEST_INPUT: &str = r"########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########
<^^>>>vv<v>>v<<";
#[test]
fn test1() {
assert_eq!(part1(TEST_INPUT), 2028);
}
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 0);
}
}

View File

@@ -12,5 +12,6 @@ pub mod day11;
pub mod day12; pub mod day12;
pub mod day13; pub mod day13;
pub mod day14; pub mod day14;
pub mod day15;
// PLACEHOLDER // PLACEHOLDER
pub mod utils; pub mod utils;

View File

@@ -19,6 +19,7 @@ fn main() {
(12, day12::day_main), (12, day12::day_main),
(13, day13::day_main), (13, day13::day_main),
(14, day14::day_main), (14, day14::day_main),
(15, day15::day_main),
// PLACEHOLDER // PLACEHOLDER
]); ]);
let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok()); let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok());

View File

@@ -4,7 +4,6 @@ use std::{
}; };
use itertools::Itertools; use itertools::Itertools;
use regex::bytes::Replacer;
/// A grid structure, indexed by (x, y) tuples. The top-left coordinate is (0, 0). /// A grid structure, indexed by (x, y) tuples. The top-left coordinate is (0, 0).
#[derive(Eq, PartialEq, Debug, Clone)] #[derive(Eq, PartialEq, Debug, Clone)]