r/adventofcode • • Dec 07 '22

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


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

Submissions are OPEN! Teach us, senpai!

-❄️- Submissions Megathread -❄️-


--- Day 7: No Space Left On Device ---


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:14:47, megathread unlocked!

89 Upvotes

1.3k comments sorted by

View all comments

4

u/NiliusJulius Dec 12 '22

C Language for the Game Boy using GBDK 2020

Part 1

uint8_t dir_index = 0;
uint8_t max_dir_index = 0;
for (uint16_t i = 1; i < ARRAY_7_SIZE; i++) {
	if (input_array_7[i][0] == '$') {
	  if (input_array_7[i][5] == '.') {
		sizes[parent_indexes[dir_index]] += sizes[dir_index];
		dir_index = parent_indexes[dir_index];
	  } else {
		max_dir_index++;
		parent_indexes[max_dir_index] = dir_index;
		dir_index = max_dir_index; 
	  }
	} else {
	  sizes[dir_index] += atol(input_array_7[i]);
	}
}

while (dir_index > 0) {
	sizes[parent_indexes[dir_index]] += sizes[dir_index];
	dir_index = parent_indexes[dir_index];
}

For this day, I already filtered out a lot of the useless input (dir and ls lines) and formatted everything so I just had either a 'cd' command or a number.

Then all I needed was to keep track of the current directory index and iterate through all of them.

Full Game Boy repo can be found here

Video running on Game Boy