DEV Community

Cover image for Calorie Counting
Robert Mion
Robert Mion

Posted on

Calorie Counting

Advent of Code 2022 Day 1

Part 1

  1. Another great lesson in reduce
  2. My algorithm in pseudocode
  3. My algorithm in JavaScript

Another great lesson in reduce

  • From a collection of Elvish calorie counts
  • I want to derive an integer
  • Which represents an amount of calories
  • Associated with the Elf who eats the most calories

Why use reduce()?

  • reduce() is a built-in array method
  • that accumulates one item (can be of any data type)
  • updating that accumulating item after operating on each item in the array
  • tracking the item's value, index, and the source array
  • and returns the accumulated item

I'll take two reduce()ers, please!

  1. A reduce() to iterate through each Elf
  2. A reduce() to iterate through one Elf's itemized calories

My algorithm in pseudocode

Split the input at each double newline character to create an array of strings representing each Elf

For each Elf, accumulate an integer, starting at 0:
  Split the string at each newline character to create an array of strings representing each food's calorie count

  For each string, accumulate a number starting at 0:
    Coerce the string to an integer
    Increment the accumulating number by the integer
    Return the accumulated number

  If this Elf's calorie count is greater than the current largest count
    Update the accumulating integer to reflect this Elf's calorie count

  Return the accumulated integer
Enter fullscreen mode Exit fullscreen mode

My algorithm in JavaScript

return input
  .split('\n\n')
  .reduce((answer, elf) => {
    const calories = elf
      .split('\n')
      .map(Number)
      .reduce((sum, count) => sum + count)
    return answer < calories ? calories : answer
  }, 0)
Enter fullscreen mode Exit fullscreen mode

Part 2

  1. Make that three reduce()ers, please!
  2. My algorithm in pseudocode
  3. My algorithm in JavaScript

Make that three reduce()ers, please!

  1. A reduce() to iterate through each Elf
  2. A reduce() to iterate through one Elf's itemized calories
  3. A reduce() to sum up the three largest calories

My algorithm in pseudocode

Split the input at each double newline character to create an array of strings representing each Elf

For each Elf, accumulate an array that is initially empty:
  Split the string at each newline character to create an array of strings representing each food's calorie count

  For each string, accumulate a number starting at 0:
    Coerce the string to an integer
    Increment the accumulating number by the integer
    Return the accumulated number

  Add the current Elf's calorie count to the accumulating array

  Return the accumulated array

Sort the array's numbers in descending order
Extract only the first three numbers - the largest ones
For each number, accumulate a sum
Return the sum
Enter fullscreen mode Exit fullscreen mode

My algorithm in JavaScript

return input
  .split('\n\n')
  .reduce((winner, elf) => {
    const calories = elf
      .split('\n')
      .map(Number)
      .reduce((sum, count) => sum + count)
    winner.push(calories)
    return winner
  }, [])
  .sort((a, b) => b - a)
  .slice(0, 3)
  .reduce((sum, count) => sum + count)
Enter fullscreen mode Exit fullscreen mode

I did it!!

  • I solved both parts!
  • I misread the instructions for Part 1. I thought the answer was a particular Elf. But it was just the calorie count, so my code got even simpler!
  • I loved the twist of Part 2, requiring a post-reduce sort and slice!
  • I made a GIF to visualize how my algorithms work!

Never a dull challenge in AoC!

Even the relatively easier ones are great practice!

Top comments (0)