1
0

day 14 make faster

cleanup

cleanup
This commit is contained in:
2024-12-14 17:44:47 +01:00
parent 0fd7b96791
commit 37874ebba9
3 changed files with 35 additions and 21 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,16 +2,17 @@ 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;
use crate::utils::grid::Grid;
pub fn day_main() { pub fn day_main() {
let input = read_to_string("input/day14.txt").unwrap(); let input = read_to_string("input/day14.txt").unwrap();
let input = input.trim(); let input = input.trim();
println!(" part1: {}", part1(input)); println!(" part1: {}", part1(input));
// println!(" part2: {}", part2(input)); println!(" part2: {}", part2(input));
} }
type RiddleResult = usize; type RiddleResult = usize;
@@ -77,22 +78,27 @@ fn part2(input: &str) -> RiddleResult {
if seen.contains(&robots) { if seen.contains(&robots) {
panic!("Loop after {second} rounds, but no christmas tree!"); panic!("Loop after {second} rounds, but no christmas tree!");
} }
let mut grid: Grid<u8> = Grid::from_default(101, 103);
seen.insert(robots.clone()); seen.insert(robots.clone());
robots.iter_mut().for_each(|((px, py), (vx, vy))| { robots.iter_mut().for_each(|((px, py), (vx, vy))| {
*px = (*px + width + *vx) % width; *px = (*px + width + *vx) % width;
*py = (*py + height + *vy) % height; *py = (*py + height + *vy) % height;
grid[(*px, *py)] += 1;
if let Some(v) = grid.get_mut((*px + 1, *py)) {
*v += 1
}
if let Some(v) = grid.get_mut((*px - 1, *py)) {
*v += 1
}
if let Some(v) = grid.get_mut((*px, *py + 1)) {
*v += 1
}
if let Some(v) = grid.get_mut((*px, *py - 1)) {
*v += 1
}
}); });
if robots if robots.iter().filter(|(s, _)| grid[*s] > 1).count() > robots.len() * 70 / 100 {
.iter()
.filter(|(s, _)| {
robots
.iter()
.any(|(t, _)| t != s && (t.0.sub(s.0).abs() + t.1.sub(s.1).abs()) <= 2)
})
.count()
> robots.len() * 70 / 100
{
printr(&robots, width, height); printr(&robots, width, height);
println!("after {} seconds. Press enter to continue or type 'merry christmas' if you can spot a tree!", second + 1); //+1 because we look at it after they have changed println!("after {} seconds. Press enter to continue or type 'merry christmas' if you can spot a tree!", second + 1); //+1 because we look at it after they have changed
let stdin = io::stdin(); let stdin = io::stdin();
@@ -128,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
@@ -148,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);
}
} }

View File

@@ -99,6 +99,21 @@ impl<T> Grid<T> {
} }
} }
impl<T> Grid<T>
where
T: Default,
{
pub fn from_default(width: i64, height: i64) -> Grid<T> {
let mut content: Vec<T> = Vec::with_capacity((width * height) as usize);
content.resize_with((width * height) as usize, Default::default);
Grid {
content_width: width,
content_height: height,
content,
}
}
}
impl Grid<char> { impl Grid<char> {
pub fn parse(input: &str) -> Grid<char> { pub fn parse(input: &str) -> Grid<char> {
let content_width = input.lines().next().unwrap().len(); let content_width = input.lines().next().unwrap().len();