DEV Community

Cover image for Advent of code - Day 14
Quentin Ménoret
Quentin Ménoret

Posted on

Advent of code - Day 14

Are you participating in the Advent of code this year?

If you don't know what the advent of code is, it's a website where you'll find a daily challenge (every day it gets harder). It's a really fun event, you should participate!

I try to solve the exercises using either JavaScript or TypeScript and will share my solutions daily (with one day delay so no one can cheat!). I only share the solution for the second part.

Damn, this one got me a little crazy. I couldn't manage to realise that the numbers you receive as inputs are 36 bits long. So bitwise operation would fail! (the first 4 bits would be discarded). This is why you can see the orMask and andMask are unused in my solution. I ended up rewriting entirely the bitwise operations using strings to try to understand what was happening... Thanks JavaScript...

With proper bitwise operation, the solution would be way simpler. For each line, you would just compute memory[address] = ((value | orMask) & andMask) >>> 0.

Note that it would be possible to use bitwise in JavaScript, you would just need to extract the first 4 bits into a different number to apply the mask there, then recombine the number together... But I didn't have the motivation to redo it!

Here is my (string-based) solution for day #14:

const sections = 
  .split('mask = ')
  .filter(Boolean)
  .map((str) => str.split('\n'))
  .map(([mask, ...actions]) => {
    return {
      mask,
      orMask: parseInt(mask.replace(/X/g, '0'), 2),
      andMask: parseInt(mask.replace(/X/g, '1'), 2),
      actions: actions.filter(Boolean).map((action) => {
        const [addressPart, valuePart] = action.split(' = ')
        return {
          address: parseInt(/\d+/.exec(addressPart), 10).toString(2).padStart(mask.length, '0'),
          value: parseInt(valuePart, 10),
        }
      }),
    }
  })

function getAddresses(address, mask) {
  if (!mask.length) return ['']
  const addresses = getAddresses(address.slice(1), mask.slice(1))
  if (mask[0] === '0') return addresses.map((a) => [address[0], ...a])
  if (mask[0] === '1') return addresses.map((a) => ['1', ...a])
  return [...addresses.map((a) => ['0', ...a]), ...addresses.map((a) => ['1', ...a])]
}

const memory = sections.reduce((acc, section) => {
  section.actions.forEach((action) => {
    const addresses = getAddresses(action.address, section.mask).map((a) => a.join(''))
    addresses.forEach((a) => {
      acc[a] = action.value
    })
  })
  return acc
}, {})

console.log(Object.keys(memory).reduce((acc, key) => acc + parseInt(memory[key], 10), 0))
Enter fullscreen mode Exit fullscreen mode

Feel free to share your solution in the comments!


Photo by Markus Spiske on Unsplash

Top comments (0)