Skip to content

Commit f81535c

Browse files
committed
Remove all arrays from part 1
1 parent e419b85 commit f81535c

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

src/day9.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,56 @@ pub fn part1(s: &str) -> u64 {
1616
}
1717
}
1818

19+
#[inline(always)]
20+
fn get_checksum(block_id: usize, position: u32, size: u32) -> u64 {
21+
size as u64 * block_id as u64 * (2 * position as u64 + size as u64 - 1)
22+
}
23+
1924
fn part1_inner(s: &str) -> u64 {
20-
let input_map = s
21-
.as_bytes()
22-
.strip_suffix(&[b'\n'])
23-
.unwrap_or_else(|| s.as_bytes());
25+
let s = s.as_bytes();
2426

25-
// All blocks with their id, and empty space
26-
let mut full_input_blocks: [u32; MAX_BLOCKS_SIZE] = [0; MAX_BLOCKS_SIZE];
27-
// Like full but without empty space
28-
let mut input_blocks: [u32; MAX_BLOCKS_SIZE / 2] = [0; MAX_BLOCKS_SIZE / 2];
27+
let mut front_pointer = 0;
28+
let mut back_pointer = INPUT_SIZE - 1;
2929

30-
let mut block_id = 0;
31-
let mut full_input_blocks_i: usize = 0;
32-
let mut input_blocks_i: usize = 0;
30+
let mut sum = 0;
3331

34-
for c in input_map.chunks(2) {
35-
let block_size = (c[0] - b'0') as usize;
32+
let mut position = 0;
3633

37-
full_input_blocks[full_input_blocks_i..full_input_blocks_i + block_size].fill(block_id);
38-
input_blocks[input_blocks_i..input_blocks_i + block_size].fill(block_id);
34+
let mut to_move_size = 0;
35+
let mut to_move_id = 0;
3936

40-
full_input_blocks_i += block_size;
41-
input_blocks_i += block_size;
37+
while front_pointer <= back_pointer {
38+
let block_size = (s[front_pointer] - b'0') as u32;
39+
front_pointer += 1;
4240

43-
if c.len() == 2 {
44-
let empty_size = (c[1] - b'0') as usize;
45-
full_input_blocks_i += empty_size;
46-
}
41+
sum += get_checksum(front_pointer / 2, position, block_size);
42+
position += block_size;
4743

48-
block_id += 1;
49-
}
44+
let mut empty_size = (s[front_pointer] - b'0') as u32;
45+
front_pointer += 1;
5046

51-
let full_size = full_input_blocks_i;
52-
let empty_space = full_size - input_blocks_i;
53-
let mut full_input_blocks_i: usize = (input_map[0] - b'0') as usize;
47+
while empty_size > 0 {
48+
if to_move_size == 0 {
49+
to_move_size = (s[back_pointer] - b'0') as u32;
50+
to_move_id = back_pointer / 2;
51+
back_pointer -= 2;
52+
}
53+
54+
let move_size = to_move_size.min(empty_size);
55+
sum += get_checksum(to_move_id, position, move_size);
56+
position += move_size;
5457

55-
for i in (input_blocks_i - empty_space - 1..input_blocks_i).rev() {
56-
full_input_blocks[full_input_blocks_i] = input_blocks[i];
57-
while full_input_blocks[full_input_blocks_i] != 0 {
58-
full_input_blocks_i += 1;
58+
to_move_size -= move_size;
59+
empty_size -= move_size;
5960
}
6061
}
62+
sum += get_checksum(to_move_id, position, to_move_size);
6163

62-
full_input_blocks[..full_size - empty_space]
63-
.iter()
64-
.enumerate()
65-
.map(|(i, id)| i as u64 * *id as u64)
66-
.sum()
64+
println!("");
65+
dbg!(to_move_id, to_move_size);
66+
dbg!(front_pointer, back_pointer);
67+
68+
sum / 2
6769
}
6870

6971
#[aoc(day9, part2)]

0 commit comments

Comments
 (0)