Programming Update: December 2022

AI-generated image of elves trudging through a mine

December was wholly dedicated to solving the Advent of Code 2022 problem set. Our job was to help the elves trek through the jungle to get magical starfruit. It was a lot of fun to do it live once again! This year I also had experience from the previous years (both live and on my own pace) that gave me the skills to solve some of the puzzles, including some puzzles that were variations on themes I’d seen before. In the end, I collected 36 stars, near my average for number of stars collected during AoC. Between work, family commitments, my programming and problem solving skill levels, that’s about the most I can usually do during the live period in December.

This year I made less use of unit tests and more use of a debug variable to run either the real input or the test input. I only used unit tests where I needed to find corner cases.

In a reversal of what I mentioned above, some of the problems I solved this year taught me how to solve some problems I’d left undone in previous years and, when I had time, I went back and worked on some of those.

After 25 Dec, I wanted to get to 200 stars before the end of the year, so I did some of the early 2017 problems as well in order to get there. 

I’ll leave you with my solutions for Day 1 in Go, Python, and Ruby:

"""Solution to Advent of Code 2022 Day 1: Counting Calories."""


def process_elves(calorie_file: str) -> list:
    """Go through our input to create a list of calorie sums."""
    elf_sum = 0
    calorie_list = []
    with open(calorie_file) as input_file:
        for this_line in input_file:
            if this_line != "\n":
                elf_sum += int(this_line)
            else:
                calorie_list.append(elf_sum)
                elf_sum = 0
    # account for the final elf
    calorie_list.append(elf_sum)
    return calorie_list


def find_top_3_calorie_sum(elf_cals: list) -> int:
    """Take in a list of elf calories, sort, and sum the top 3."""
    elf_cals.sort(reverse=True)
    return elf_cals[0] + elf_cals[1] + elf_cals[2]


if __name__ == "__main__":
    elf_calories = process_elves("../input.txt")
    max_calorie_elf = max(elf_calories)
    print(f"The elf with the most calories is carrying {max_calorie_elf} calories.")
    top_three_cals = find_top_3_calorie_sum(elf_calories)
    print(f"The top 3 elves are carrying {top_three_cals} calories.")

The Go and Ruby solutions benefit from the fact that I’ve now already seen parts 1 and 2 of the problem and can code with that in mind.

// Solution to Advent of Code 2022 Day 01 - Calorie Counting

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// ParseElves returns the input file if it contains multiple lines of text
func ParseElves(fileName string) ([]int, error) {
	inputSlice := make([]int, 0)
	file, err := os.Open(fileName)
	if err != nil {
		return inputSlice, err
	}
	scanner := bufio.NewScanner(file)
	calorieSum := 0
	for scanner.Scan() {
		thisNumber, _ := strconv.Atoi(scanner.Text())
		// fmt.Printf("Number in loop: %d\n", thisNumber)
		if thisNumber != 0 {
			// fmt.Printf("not a 0!")
			calorieSum += thisNumber
			// fmt.Printf("calorieSum %d\n", calorieSum)
		} else {
			inputSlice = append(inputSlice, calorieSum)
			calorieSum = 0
		}
	}
	err = file.Close()
	if err != nil {
		return inputSlice, err
	}
	return inputSlice, nil
}

func main() {
	elfCalories, err := ParseElves("../input.txt")
	if err != nil {
		// fmt.Printf("Error%v\n", err)
	}
	// fmt.Printf("Elf Calories: %v\n", elfCalories)
	maxCalories := [3]int{0, 0, 0}
	for _, elfCalorieTotal := range elfCalories {
		if elfCalorieTotal > maxCalories[0] {
			temp := maxCalories[0]
			maxCalories[0] = elfCalorieTotal
			tempTwo := maxCalories[1]
			maxCalories[1] = temp
			maxCalories[2] = tempTwo
		} else if elfCalorieTotal > maxCalories[1] {
			temp := maxCalories[1]
			maxCalories[1] = elfCalorieTotal
			maxCalories[2] = temp
		} else if elfCalorieTotal > maxCalories[2] {
			maxCalories[2] = elfCalorieTotal
		}
	}
	fmt.Printf("The elf with the most calories has food worth %d calories\n", maxCalories[0])
	partTwoTotal := maxCalories[0] + maxCalories[1] + maxCalories[2]
	fmt.Printf("The elves with the top 3 most calories have food worth %d calories", partTwoTotal)

}
# Solution to 2022 Day 01 - Calorie Counting

def input_per_line(file)
  File.readlines(file, chomp:true)
end

elf_calorie_list = input_per_line("../input.txt")
calorie_per_elf = elf_calorie_list.map(&:to_i).slice_when {|i, j| j==0}.to_a
elf_sum = []
calorie_per_elf.each do |item|
  elf_sum.append(item.sum)
end
elf_sum.sort!.reverse!

puts "The elf with the highest amount of calories is carrying #{elf_sum[0]} calories. "

puts "The top 3 elves with the highest amount of calories are carrying #{elf_sum[0]+elf_sum[1]+elf_sum[2]} calories. "