Advent Of Code 2022 — Day 1

James Lingham
5 min readDec 6, 2022

--

I’m going to be working through the Advent Of Code 2022 challenges and writing up my workings and solutions. I recommend working through the challenges yourself though.

What is Advent Of Code

Advent Of Code is an advent calendar of small programming challenges that you can attempt in any language. I’ll be doing this in JavaScript.
You can take part by visiting this webpage.

Each day has two tasks related to each other, i’ll be solving both of the tasks within each article. It looks like the challenges are the same, but people are given different input files — so our process may be the same, but we will likely get different answers.

Day 1 - Task 1

You can find the full details for day 1 here

We’re given a list of numbers in the following format:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

This list represents snacks that elves are carrying. Each Elf is separated by a blank line. Each snack the elf is carrying is on its own line — the value of the line depicts the caloric value of the snack.

Our task is to find the highest total calories being carried by any single elf.

Formatting The Data

The first thing we need to do it transform the data from the current format, in to something we can easily work with. We need to break the values down by Elf to be able to work out their totals — So let’s start with that.

I’ve got the input for the file formatted as a template string like this in its own file, i’ll be importing this file to keep our problem solving logic tidy.

exports.example = `1000
2000
3000

4000

5000
6000

7000
8000
9000

10000`;

This is referenced in our main solution like this

const { example} = require("./input.js");

The specification says that each elf is separated in the input file by a blank space, which means we can easily split the list by elves using split() on our input string, Splitting on \n\n which is the character for a new line.

const elves = example.split("\n\n");

We’re then able to loop through our array and calculate the total value of the snacks each of those elves are carrying. We can apply similar logic to splitting the lines to get each snack — Although we’re only looking for one new line rather than 2.

elves.forEach((e) => {
const snacks = e.split("\n");
});

Finding Total Snack Value

Now that we have our data in an easy to use format, we just need to find a way to add the snacks up, and have a way to store the values.

To store the values, we can create an array outside of our “Elf Loop” where we can keep the values.

To get the total of the snack values we can use the reduce() method. Which is used to iterate through an array and perform a calculation — passing its result on to the next iteration, ultimately returning a single value.

const elvesTotalCalories = [];
elves.forEach((e) => {
const snacks = e.split("\n");
const totalCalories = snacks.reduce((total, calories) => {
return total + parseInt(calories);
}, 0);
elvesTotalCalories.push(totalCalories);
});

Remember that the input has everything as a string, so we need to use parseInt() to convert the string in to a number, otherwise we’ll just be concatenating the strings together, which isn’t what we want.

Finding the Highest Total

Great, we’ve got an array of our total values for each elf, the next step is to find the highest value within the array. To do this, we’ll use a simple sort() on the array, and grab the first value.

const sortedElves = elvesTotalCalories.sort((a, b) => b - a);
const result = sortedElves[0];

Final Code

const { example, input } = require("./input.js");
function getHighestElfCalories(data) {
const elves = data.split("\n\n");
const elvesTotalCalories = [];
elves.forEach((e) => {
const snacks = e.split("\n");
const totalCalories = snacks.reduce((total, calories) => {
return total + parseInt(calories);
}, 0);
elvesTotalCalories.push(totalCalories);
});
const sortedElves = elvesTotalCalories.sort((a, b) => b - a);
return sortedElves[0];
}
console.log(getHighestElfCalories(example));
console.log(getHighestElfCalories(input));

Day 1 - Task 2

The second part of the task is to find the 3 elves carrying the highest snack value, and then find the total of those values — similar to what we’ve already done, but for 3 elves instead.

What we can do here is allow an extra parameter count to be passed in to our current function, count being the number of elves we want to get the value for. Our requirement is only to have 3 — but its good to be flexible with our function if the requirement was the change at a later date.

Instead of taking the first item in the array, we’re now interested in the top X items, which we can get by using splice() on our sorted array

function getHighestElfCalories(data, count) {
/*
previous function code here
*/
const topElves = sortedElves.splice(0,count);
}

Now that we have the top X values — We need to add them together.
We’ve already added an array of numbers together in this task before, and the process is going to be exactly the same — we’ll be using reduce() again!

const totalOfXElves = topElves.reduce((total, calories) => {
return total + parseInt(calories);
}, 0);

We then need to make sure we update our function call to specify how many elves we want to be combining, in this case — 3.
Our final code would look something a bit like this

const { example, input } = require("./input.js");

function getHighestElfCalories(data, count) {
const elves = data.split("\n\n");
const elvesTotalCalories = [];
elves.forEach((e) => {
const snacks = e.split("\n");
const totalCalories = snacks.reduce((total, calories) => {
return total + parseInt(calories);
}, 0);
elvesTotalCalories.push(totalCalories);
});
const sortedElves = elvesTotalCalories.sort((a, b) => b - a);
const topElves = sortedElves.splice(0, count);
const totalOfXElves = topElves.reduce((total, calories) => {
return total + parseInt(calories);
}, 0);
return totalOfXElves;
}

console.log(getHighestElfCalories(example, 3));
console.log(getHighestElfCalories(input, 3));

Summary

A fairly straight forward challenge looking at grouping values together within Arrays and working with some of the ES6 functions some people will be less familiar with.

Key functions we used
- reduce()
- forEach()
- sort()

GitHub

You can find the full code and input at my GitHub repo here

--

--