# Advent of Code 2022 - Day 1: Calorie Counting Solution

Straight-forward data manipulation

## Part 1

Calculate the total calories carried by each Elf

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}const fs = require("fs")2
5const SEPARATOR = ""6
7const aggregatedData = data.reduce(8  (acc, el) => {9    if (el === SEPARATOR) {10      acc.totalCarries.push(acc.sum)11      acc.sum = 012    } else {13      acc.sum += Number(el)14    }15
16    return acc17  },18  { totalCarries: [], sum: 0 }19)20
21const res = Math.max(...aggregatedData.totalCarries)22
23console.log(res)```

## Part 2

Same as Part 1, with additional steps

• take
aggregatedData.totalCarries
• sort descending
• take the first 3 value
• calculate sum

## Implementation

```1const fs = require("fs")2
5const SEPARATOR = ""6
7const aggregatedData = data.reduce(8  (acc, el) => {9    if (el === SEPARATOR) {10      acc.totalCarries.push(acc.sum)11      acc.sum = 012    } else {13      acc.sum += Number(el)14    }15
16    return acc17  },18  { totalCarries: [], sum: 0 }19)20
21const res = aggregatedData.totalCarries22  .sort((a, b) => b - a)23  .slice(0, 3)24  .reduce((acc, el) => acc + el, 0)25
26console.log(res)```

Original problem

## Next Post

Advent of Code 2022 - Day 4: Camp Cleanup

Dec 4, 2022

Dealing with start, end

Search Posts