# 2022 Day 8

Part 1 Problem From a grid of trees (with heights), count the trees that are visible from outside the grid. General solution First we need to design our data structures. A grid is perhaps most easily represented as a 2 dimensional array, both from a conceptual perspective and also it is likely to be efficient because requests will have locality of reference (we will often be looking at values near to each other).…

# 2022 Day 7

Part 1 Problem Read in a list of lines which represent one of the following: Command: Starts with a \$. Directory: Starts with dir and is followed by the name. Regular File: Starts with a number (the file size) and is followed by the name. The size of a directory is the sum of the regular files in the directory, plus any directories underneath it. Find the sum of the sizes of all directories with a size less than or equal to 100000.…

# 2022 Day 6

Part 1 Problem Read in a stream of characters and identify the first point at which 4 consecutive characters are unique. General solution This is trivial - we simply read it one line of the file, get the first 4 characters into a queue, and then check whether they are unique. If not, move the start point along by one and check the next four characters. Solution: Go Although we could read in a file character by character, this would potentially cause a lot of read requests.…

# 2022 Day 5

Part 1 Problem The file input includes a number of stacks, their contents (crates), and a series of instructions for moving crates betwene stacks. Process all the instructions and then find the identifier of each crate at the top of each stack. Some stacks may be empty at the beginning or end of the process. General solution This is more difficult than previous days, as our input file now has three different line types:…

# 2022 Day 4

Part 1 Problem Each line in the file represents a pair of assignments. Each assignment is separated by a comma, and the lower and upper bounds of the assignment are separated by a hyphen. Count the pairs where one assignment completely overlaps the other. General solution As all pairs are independent, we can process them as we read the file instead of converting them into a data structure. A pair of assignments completely overlap if and only if:…

# 2022 Day 3

Part 1 Problem Each line in the file represents a rucksack, and each character of the line represents an item. Rucksacks have two compartments, with the items split evenly between them (first n / 2 items go in the first compartment, the rest in the second compartment). General solution Create a nested data structure which comprises of: Rucksacks. Rucksack compartments. Contents of compartments. We also need a list of priorities for each letter a.…

# 2022 Day 2

Part 1 Problem Each line in the file represents a round of Rock Paper Scissors, with the two hands separated by whitespace. Calculate the total score based on the result of each round and the response by your player. General solution Create a nested data structure which is a list of rounds and populate this from the file, calculating the score for each round at the same time. Sum all the scores to find the total.…

# 2022 Day 1

Part 1 Problem Each line in the file contains an inventory item with the number of calories. Blank lines separate each group of items. Find the inventory group with the highest number of calories and print that number. General solution Create a nested data structure which is an array of arrays, where the first level is the list of inventory groups and the second level is the list of items within that group.…