Skip to content

Commit 26db76d

Browse files
2024: Optimize day 14
1 parent 41f91dc commit 26db76d

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

2024/day14/src/main.rs

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::fs;
22

33
#[allow(unused)]
4-
fn print_grid(grid: &[usize], w: usize, h: usize) {
4+
fn print_grid(grid: &[u16], w: usize, h: usize) {
55
for y in 0..h {
66
for x in 0..w {
7-
if grid[y * w + x] == 0 {
8-
print!(".");
9-
} else {
10-
print!("#");
7+
for i in (0..16).rev() {
8+
if grid[y * w + x] & 1 << i > 0 {
9+
print!("#");
10+
} else {
11+
print!(".");
12+
}
1113
}
1214
}
1315
println!();
@@ -31,8 +33,10 @@ fn main() {
3133
let w = 101;
3234
let h = 103;
3335
let mut grid = vec![0; w * h];
34-
let mut robots = Vec::new();
36+
let bw = (w + 15) / 16;
37+
let mut binary_grid = vec![0u16; bw * h];
3538

39+
let mut robots = Vec::new();
3640
for l in lines {
3741
let (p, v) = l.split_once(' ').unwrap();
3842
let (px, py) = p[2..]
@@ -45,32 +49,23 @@ fn main() {
4549
.unwrap();
4650
robots.push((px, py, vx, vy));
4751
grid[py as usize * w + px as usize] += 1;
52+
binary_grid[py as usize * bw + px as usize / 16] |= 1 << (15 - px % 16);
4853
}
4954

50-
let mut total1 = 0;
51-
let mut total2 = 0;
55+
let mut total1 = None;
56+
let mut total2 = None;
5257
let mut steps = 0;
5358
'outer: loop {
5459
for (px, py, vx, vy) in &mut robots {
55-
grid[*py as usize * w + *px as usize] -= 1;
56-
57-
*px += *vx;
58-
if *px >= w as i32 {
59-
*px -= w as i32;
60-
}
61-
if *px < 0 {
62-
*px += w as i32;
60+
let ni = *py as usize * w + *px as usize;
61+
grid[ni] -= 1;
62+
if grid[ni] == 0 {
63+
binary_grid[*py as usize * bw + *px as usize / 16] &= !(1 << (15 - *px % 16));
6364
}
64-
65-
*py += *vy;
66-
if *py >= h as i32 {
67-
*py -= h as i32;
68-
}
69-
if *py < 0 {
70-
*py += h as i32;
71-
}
72-
65+
*px = (*px + *vx).rem_euclid(w as i32);
66+
*py = (*py + *vy).rem_euclid(h as i32);
7367
grid[*py as usize * w + *px as usize] += 1;
68+
binary_grid[*py as usize * bw + *px as usize / 16] |= 1 << (15 - *px % 16);
7469
}
7570

7671
steps += 1;
@@ -80,36 +75,31 @@ fn main() {
8075
let t2 = count(&grid, w, 0, w / 2, (h + 1) / 2, h);
8176
let t3 = count(&grid, w, (w + 1) / 2, w, 0, h / 2);
8277
let t4 = count(&grid, w, (w + 1) / 2, w, (h + 1) / 2, h);
83-
total1 = t1 * t2 * t3 * t4;
84-
if total2 > 0 {
78+
total1 = Some(t1 * t2 * t3 * t4);
79+
if total2.is_some() {
8580
break;
8681
}
8782
}
8883

8984
// look for at least 16 robots in a row. this should be our christmas tree.
90-
let min_run = 16;
9185
for y in 0..h {
92-
for x in 0..w - min_run {
93-
let mut found = true;
94-
for i in 0..min_run {
95-
if grid[y * w + x + i] == 0 {
96-
found = false;
97-
break;
98-
}
99-
}
100-
if found {
86+
for x in 0..bw - 1 {
87+
if binary_grid[y * bw + x].trailing_ones()
88+
+ binary_grid[y * bw + x + 1].leading_ones()
89+
>= 16
90+
{
10191
// use print_grid to check if it's really a christmas tree
102-
// print_grid(&grid, w, h);
92+
// print_grid(&binary_grid, bw, h);
10393

104-
total2 = steps;
105-
if total1 > 0 {
94+
total2 = Some(steps);
95+
if total1.is_some() {
10696
break 'outer;
10797
}
10898
}
10999
}
110100
}
111101
}
112102

113-
println!("{}", total1);
114-
println!("{}", total2);
103+
println!("{}", total1.unwrap());
104+
println!("{}", total2.unwrap());
115105
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy