DEV Community

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

Posted on

2

Advent of code - Day 21

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.


Thank you for the break after Day #20 horrors 🙏
This one was clear, easy to work with and just challenging enough for a Monday!

Part 2 was actually free if you anticipated a little bit while doing the first part, so after reading the subject, it only took me 30 seconds to adapt the return value (since I already knew which allergens and ingredients were linked).

Anyway, here is my solution for day #21:

const lines = input.split('\n').map((line) => {
    const [_ingredients, _allergens] = line.split(' (')
    return {
      ingredients: _ingredients.split(' '),
      allergens: _allergens.slice(9, -1).split(', '),
    }
  })

  const allAllergens = Array.from(new Set(lines.reduce((acc, line) => [...acc, ...line.allergens], [])))
  const allIngredients = Array.from(new Set(lines.reduce((acc, line) => [...acc, ...line.ingredients], [])))

  const struct = allAllergens.reduce((acc, allergen) => {
    acc[allergen] = [...allIngredients]
    return acc
  }, {})

  lines.map((line) => {
    allAllergens.map((allergen) => {
      if (line.allergens.includes(allergen)) {
        struct[allergen] = struct[allergen].filter((ingredient) => line.ingredients.includes(ingredient))
      }
    })
  })

  while (true) {
    let found = false

    allAllergens.map((foundAllergen) => {
      if (struct[foundAllergen].length === 1) {
        const foundIngredient = struct[foundAllergen][0]
        allAllergens.map((allergen) => {
          if (allergen === foundAllergen) return
          if (struct[allergen].includes(foundIngredient)) {
            found = true
            const index = struct[allergen].indexOf(foundIngredient)
            struct[allergen].splice(index, 1)
          }
        })
      }
    })

    if (!found) break
  }

  console.log(
    allAllergens
      .sort()
      .map((allergen) => struct[allergen][0])
      .join(),
  )
Enter fullscreen mode Exit fullscreen mode

Feel free to share your solution in the comments!


Photo by Markus Spiske on Unsplash

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay