2022 Day 10


Part 1 Problem General solution Solution: Go package main import ( "bufio" "fmt" "os" "strconv" "strings" ) type Instruction struct { name string argument int } func main() { instructions := []Instruction{} scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if len(line) > 0 { fields := strings.Fields(line) instruction := Instruction{} instruction.name = fields[0] if instruction.name == "addx" { instruction.argument, _ = strconv.Atoi(fields[1]) } instructions = append(instructions, instruction) } } cycles := []int{} cycles = append(cycles, 1) for i := range instructions { switch instructions[i].…
Read more ⟶

2022 Day 9


Part 1 Problem General solution Solution: Go Our first attempt: for mi := range moveInstructions { // Move the head head.x += moveInstructions[mi].DeltaX head.y += moveInstructions[mi].DeltaY // If the distance between the head and the tail is more than 1 // across X or Y, move the tail 1 point in the opposite direction headTailDistanceX := head.x - tail.x headTailDistanceY := head.y - tail.y if headTailDistanceX < -1 { tail.x -= 1 } else if headTailDistanceX > 1 { tail.…
Read more ⟶

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).…
Read more ⟶

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.…
Read more ⟶

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.…
Read more ⟶

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:…
Read more ⟶

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:…
Read more ⟶

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.…
Read more ⟶

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.…
Read more ⟶

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.…
Read more ⟶