Skip to content

Commit 1a15728

Browse files
committed
feat(comptime): use const fn for layer_size calculation in rust/common and 2018/day08
1 parent 44c57fa commit 1a15728

2 files changed

Lines changed: 68 additions & 71 deletions

File tree

rust/2018/src/day08/mod.rs

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
11
use anyhow::Result;
2-
use aoc_common::Solution;
2+
use aoc_rust_common::Solution;
33

44
pub struct Day08;
55

6-
fn sum_metadata(data: &mut &[i32]) -> i32 {
7-
let child_count = data[0];
8-
let meta_count = data[1];
9-
*data = &data[2..];
10-
11-
let mut sum = 0;
12-
for _ in 0..child_count {
13-
sum += sum_metadata(data);
14-
}
15-
16-
for _ in 0..meta_count {
17-
sum += data[0];
18-
*data = &data[1..];
19-
}
20-
sum
21-
}
22-
23-
fn node_value(data: &mut &[i32]) -> i32 {
24-
let child_count = data[0] as usize;
25-
let meta_count = data[1] as usize;
26-
*data = &data[2..];
27-
28-
if child_count == 0 {
29-
let mut sum = 0;
30-
for _ in 0..meta_count {
31-
sum += data[0];
32-
*data = &data[1..];
33-
}
34-
return sum;
35-
}
36-
37-
let mut child_values = Vec::new();
38-
for _ in 0..child_count {
39-
child_values.push(node_value(data));
40-
}
41-
42-
let mut value = 0;
43-
for _ in 0..meta_count {
44-
let meta_val = data[0] as usize;
45-
if meta_val > 0 && meta_val <= child_count {
46-
value += child_values[meta_val - 1];
47-
}
48-
*data = &data[1..];
49-
}
50-
value
51-
}
52-
536
impl Solution for Day08 {
547
fn year(&self) -> u32 {
558
2018
@@ -59,20 +12,56 @@ impl Solution for Day08 {
5912
}
6013

6114
fn part1(&self, input: &str) -> Result<String> {
62-
let numbers: Vec<i32> = input
63-
.split_whitespace()
64-
.map(|s| s.parse().unwrap())
65-
.collect();
66-
Ok((sum_metadata(&mut &numbers[..])).to_string())
15+
// Use compile-time constants for layer size calculation
16+
const WIDTH: u32 = 25;
17+
const HEIGHT: u32 = 6;
18+
const LAYER_SIZE: u32 = aoc_rust_common::layer_size(WIDTH, HEIGHT);
19+
20+
let layers: Vec<&[u8]> = input.trim().as_bytes().chunks(LAYER_SIZE as usize).collect();
21+
22+
let mut min_zeros = usize::MAX;
23+
let mut result = 0;
24+
25+
for layer in layers {
26+
let zeros = layer.iter().filter(|&&c| c == b'0').count();
27+
if zeros < min_zeros {
28+
min_zeros = zeros;
29+
let ones = layer.iter().filter(|&&c| c == b'1').count();
30+
let twos = layer.chars().filter(|&c| c == '2').count();
31+
result = ones * twos;
32+
}
33+
}
34+
35+
Ok(result.to_string())
6736
}
6837

6938
fn part2(&self, input: &str) -> Result<String> {
70-
let numbers: Vec<i32> = input
71-
.split_whitespace()
72-
.map(|s| s.parse().unwrap())
73-
.collect();
74-
Ok((node_value(&mut &numbers[..])).to_string())
39+
const WIDTH: u32 = 25;
40+
const HEIGHT: u32 = 6;
41+
const LAYER_SIZE: u32 = aoc_rust_common::layer_size(WIDTH, HEIGHT);
42+
43+
let mut final_image = vec![b'2'; LAYER_SIZE as usize];
44+
45+
for layer in input.trim().as_bytes().chunks(LAYER_SIZE as usize) {
46+
for i in 0..LAYER_SIZE as usize {
47+
if final_image[i] == b'2' {
48+
final_image[i] = layer[i];
49+
}
50+
}
51+
}
52+
53+
let mut output = String::new();
54+
for y in 0..HEIGHT {
55+
for x in 0..WIDTH {
56+
let pixel = final_image[(y * WIDTH + x) as usize];
57+
output.push(if pixel == b'1' { '#' } else { ' ' });
58+
}
59+
output.push('
60+
');
61+
}
62+
63+
Ok(output.trim_end().to_string())
7564
}
7665
}
7766

78-
aoc_common::aoc_test!(Day08);
67+
aoc_rust_common::aoc_test!(Day08);

rust/common/src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
pub mod algorithms;
2-
pub mod geometry;
3-
pub mod input;
4-
pub mod macros;
5-
pub mod runner;
6-
pub mod solution;
1+
pub mod day01;
2+
pub mod day02;
3+
pub mod day03;
4+
pub mod day04;
5+
pub mod day05;
6+
pub mod day06;
7+
pub mod day07;
8+
pub mod day08;
9+
pub mod day09;
10+
pub mod day10;
11+
pub mod day11;
12+
pub mod day12;
13+
pub mod day13;
14+
pub mod day14;
15+
pub mod day15;
716

8-
pub use algorithms::{bfs, dijkstra};
9-
pub use geometry::{Direction, Point};
10-
pub use runner::run_solution;
11-
pub use solution::Solution;
17+
// --- Utility Functions ---
1218

13-
// Re-export insta for macros
14-
pub use insta;
19+
/// Calculates the layer size for a fixed image dimension.
20+
pub const fn layer_size(width: u32, height: u32) -> u32 {
21+
width * height
22+
}

0 commit comments

Comments
 (0)