Skip to content
Snippets Groups Projects
Commit a2cd8739 authored by Mark Jeeninga's avatar Mark Jeeninga
Browse files

Day 5 Mark

parent 49ea0219
Branches
No related tags found
No related merge requests found
swap = (x,i,j) => {y = x[i]; x[i] = x[j]; x[j] = y; return false}
range = x => [...Array(x).keys()]
rules = input.split("\n\n")[0].split("\n")
lines = input.split("\n\n")[1].split("\n").map(x => x.split(","))
satisfiesRules = (x,y) => !rules.includes(y+"|"+x)
isCorrect = (x,i,j) => satisfiesRules(x[i], x[j]) ? true : swap(x,i,j)
sort = x => {while(!range(x.length).every(i => range(x.length-i).every(j => isCorrect(x,i,j+i)))){}; return x}
sortedLines = lines.map(sort)
lines = input.split("\n\n")[1].split("\n").map(x => x.split(","))
output1 = sortedLines.filter((x,i) => x.toString() == lines[i].toString()).map(x => Number(x[(x.length-1)/2])).reduce((x,y) => x+y,0)
output2 = sortedLines.filter((x,i) => x.toString() != lines[i].toString()).map(x => Number(x[(x.length-1)/2])).reduce((x,y) => x+y,0)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment