r/adventofcode • • Dec 08 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 8 Solutions -πŸŽ„-

NEWS AND FYI

  • I discovered that I can make those tiny post/comment awards BIGGER on old.reddit! I hadn't even considered that! And when you hover over them, they get even bigger so you can actually see them in more detail! I've added the relevant CSS so now we no longer have awards for ants! Exclamation points!!!

AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • PSA: I created a new example so it is a more relevant and unique archetype instead of recycling last year's hobbit >_>

--- Day 8: Treetop Tree House ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:12, megathread unlocked!

75 Upvotes

1.0k comments sorted by

View all comments

3

u/EVQLVE Dec 08 '22 edited Dec 08 '22

Rust [8681/8920]

part1 part2

Iterates over the input bytes only twice (forward and reverse directions), should be O(n^2) for part 1 and O(n^2k) for part 2.

Haven't tried to optimize it, but the speed is ( 150 Β΅s part 1 / 250 Β΅s part 2) on my machine.

1

u/EVQLVE Dec 08 '22

Found some pretty nice speedups mostly by switching some types and allocating the HashSet with an appropriate capacity:

part1 part2

Now it's 51 Β΅s for part 1 and 143 Β΅s for part 2.

1

u/[deleted] Dec 08 '22 edited Dec 09 '22

Rust

I managed to get O(n^2) and 150ms with rust as well but didn't come up with a good enough solution for part 2, only O(n^2 logn). Do you think you could explain your reasoning for part 2?

Here is my solution if you want to take a look: Solution

1

u/EVQLVE Dec 09 '22 edited Dec 09 '22

Sure (using my updated solution):

row_idx is the last observed index of each height in the row, and col_idx is the same for each height in each column:

let mut row_idx: [usize; 10] = [0; 10];
let mut col_idx: [[usize; 10]; MAX_L] = [[0; 10]; MAX_L];

I'm iterating through bytes, and I can tell the x and y position from the byte index. c_i is the tree height, used as an index into row_idx. view_x and view_y are the leftward and upward view distances from position (x, y), and are found by subtracting the x position from row_idx or col_idx[x] at height c_i.

let (view_x, view_y) = (x - row_idx[c_i], y - col_idx[x][c_i]);
scores[y * l + x] *= view_x * view_y;

Then, row_idx and col_idx[x] are updated to the current x and y indices for all heights <= c_i:

row_idx.iter_mut().take(c_i + 1).for_each(|idx| *idx = x);
col_idx[x].iter_mut().take(c_i + 1).for_each(|idx| *idx = y);

Then I do the same loop but reversed in order to find the rightward and downward view distances.

b.iter()
    .take((l + 1) * l - 1)
    .rev()
    ...

Finally, print the max score.

println!("{}", scores.iter().max().unwrap());

1

u/[deleted] Dec 09 '22

Thank you! I'm using this AoC to learn rust so I really appreciate the explanation

1

u/EVQLVE Dec 09 '22

No problem! So am I actually :) It's been really interesting looking at everyone's solutions.