Advent Of Code — Day 7 — Javascript

Vincent Yang
3 min readFeb 1, 2021

--

Today’s puzzle is quite confusing on how to complete. If we take a look at the sample data:

light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.

Given all the different colored bags, find which bags can contain a shiny gold bag. This count includes bags of all depths so we cannot just do a search for shiny gold bags in the second portion of each line. For example:

bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.

In these two lines, we see that bright white bags and muted yellow bags can contain shiny gold bags. However, if we go one level deeper:

bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags
-> bright white bags
-> shiny gold bags
-> muted yellow bags
-> shiny gold bags
little red bags
-> bright white bags
-> shiny gold bags
-> muted yellow bags
-> shiny gold bags

And so, in total, there are four possible color bags that contain shiny gold bags.

function day7(input){
let arr = input.split('\n')
}

First thing that we will need to do is divide each line into the bag that contains the bag that is being contained.

function day7(input){
let arr = input.split('\n')
for(let str of arr){
const [bag, bags] = str.split(' bags contain ')
}
}
//arr[0] or str = "light red bags contain 1 bright white bag, 2 muted yellow bags."
//bag = light red
//bags = 1 bright white bag, 2 muted yellow bags

Basically, what we will want to do with the data is create a structure that looks similar to this:

dark orange bags
bright white bags
shiny gold bags
muted yellow bags
little red bags
bright white bags
shiny gold bags
muted yellow bags
bright white bags
shiny gold bags
muted yellow bags
shiny gold bags

With a map, we can create a structure where a unique key, the containing bag, will point to all of the colors that it can hold. At the end, we will be able to iterate through all of the keys and find all of the possible colors that contain shiny gold bags.

function day7(input){
let arr = input.split('\n')
const map = new Map()
for(let str of arr){
const [bag, bags] = str.split(' bags contain ')
if(!map.has(bag)){
map.set(bag, [])
}
}
}

What this will do is create a key for each color that contains another bag pointing to an array of colors that it can hold inside of it.

function day7(input){
let arr = input.split('\n')
const map = new Map()
for(let str of arr){
const [bag, bags] = str.split(' bags contain ')
if(!map.has(bag)){
map.set(bag, [])
}
}
}
const colors = map.keys()
let total = 0
for(const color of colors){
if(hasGold(color) && color != 'shiny gold bags')
total++
}
function hasGold(color){
if(color === 'shiny gold bags') return true
if(!map.has(color)) return false
const bagsWithin = map.get(color)
for(const {color: bag} of bagsWithin){
if(hasGold(bag)
return true
}
return false
}
return total

--

--

No responses yet