let convertedData = data
// concat all group of rows separated by an empty string ''
.reduce((acc,d) => {
if (d === ''){
return [...acc,d]
} else {
let clone = [...acc]
let last = clone.pop()
return [...clone,last.concat(d).concat(' ')]
}
},[''])
// separe all pair entry/value separated by a ' '
.map(d => d.split(' ').slice(0,-1))
// convert all strings to arrays
.map(d => d.map(answers => answers.split('')))
let result1 = convertedData
// concat answers and use Set to remove duplicate, it gets all answers for each group
.map(d => d
.reduce((acc,answers) => [...new Set([...acc, ...answers])] , []))
// convert list of answers to number of answers
.map(d => d.length)
// add all those numbers to get final result
.reduce((acc,d) => acc+d,0)
let result2 = convertedData
// intersect arrays using a combination of filter and includes
.map(d => d
.reduce((acc,answers, i, arr) => i===0 ? arr[0] : acc.filter(accElem => answers.includes(accElem)), []))
// convert list of answers to number of answers
.map(d => d.length)
// add all those numbers to get final result
.reduce((acc,d) => acc+d,0)
console.log(result1)
console.log(result2)
def day7_1(data,tag,taboo):
bags = 0
exploration = []
for d in data:
if d['bag'] not in taboo and tag in [b[1] for b in d['contain']]:
bags+=1
taboo.append(d['bag'])
exploration.append(d['bag'])
for b in exploration:
bags += day7_1(data, b, taboo)
return bags
def day7_2(data,tag):
cpt = 0
for b in [d["contain"] for d in data if d["bag"]==tag][0]:
print(b)
cpt += b[0] * (1 + day7_2(data,b[1]))
return cpt
if __name__ == "__main__":
fichier = open('input.txt', 'r')
linesDeMonFichier = fichier.readlines()
data = [ {'bag':d.strip().split("contain")[0].split(" bags")[0],'contain':[ [int(b[0]),b.split(" bag")[0][2:]] for b in d.strip().split("contain ")[1].split(", ") if d.strip().split("contain ")[1] != "no other bags."]} for d in linesDeMonFichier ]
print(day7_1(data,'shiny gold',[]))
print(day7_2(data,'shiny gold'))
Bon jâai commence aujourdâhui en GoLang (je suis un vrai). Je code a la semi-crade. Pour lâinstant jâai fait 2 jours part1/2, donc jâai 4 etoiles. Je suis tropfort!!!
Day1
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
strs := strings.Split(exerciseInput, "\n")
numbers := make([]int, len(strs))
for i := range numbers {
numbers[i], _ = strconv.Atoi(strs[i])
}
fmt.Println(numbers)
for i, v := range numbers {
for j := i + 1; j < len(numbers); j++ {
for k := j; k < len(numbers); k++ {
if k == j || k == i {
continue
}
if v+numbers[j]+numbers[k] == 2020 {
fmt.Println("Found number that add to 2020", v, numbers[j], numbers[k])
fmt.Println("Multiplied: ", v*numbers[j]*numbers[k])
}
}
}
}
}
Jour 7 en JS (vaguement) en fonctionnel, en essayant de commenter mon code pour ceux qui veulent relire.
Jour 7
// Get the direct parents of the given bag according to the list of rules
let bagsContaining = (listRules) => (bagInside) => {
return listRules
// get all rules having child equal to the given bag
.filter(rule => rule.vals.filter(val => val.id === bagInside).length > 0)
// get all the parents of those rules
.map(rule => rule.id)
}
// Get the all ascendants of the given bag according to the list of rules
let bagContainingRec = (listRules) => (bagInside) => {
// get the parents of the tested bag and return empty list if no parents
let firstLevel = bagsContaining(listRules)(bagInside);
if (firstLevel === []) {
return firstLevel
} else {
let parents = firstLevel
// get all the parents of the direct parents, so we get a list of lists of ansestors
.map(bag => bagContainingRec(listRules)(bag))
// remove all the empty lists: there are no ancestors for those parents
.filter(bags => bags.length >= 0)
// merge the ancestors alltoghether, merge this to the first level of parent
.reduce((acc,bags) => [...new Set([...acc, ...bags])] , firstLevel)
return parents
}
}
// get the number of bags contained in given bag according to the rules
let numberBagContainedIn = (listRules) => (bag) => {
let allBagsInside = listRules.filter(rule => rule.id === bag)[0].vals
// if no bag found inside, return 0
if (allBagsInside === []){
return 0
} else {
// if bags found inside, do a sum of all the bags multiplied by their quantities
// plus all the bags inside those bags recursively
return allBagsInside.reduce((acc,bagInside) => (
acc +
bagInside.qty +
(bagInside.qty * numberBagContainedIn(listRules)(bagInside.id))), 0)
}
}
let formatedData = data
// split the text to get an array: in index 1 the bag and in index 2 what it contains
.map(d => d.match(/^([a-z]+ [a-z]+) bags contain (.*)\.$/))
.map(d => ({
id:d[1],
vals:(d[2].split(', ')
.map(val => val.match(/^([0-9]+) ([a-z]+ [a-z]+)/))
.filter(val => val != null)
.map(val => ({id:val[2], qty:parseInt(val[1], 10)})))
}))
let result = bagContainingRec(formatedData)('shiny gold').length
console.log(result)
let result2 = numberBagContainedIn(formatedData)('shiny gold')
console.log(result2)