diff --git a/day 5/day_5_mark.js b/day 5/day_5_mark.js new file mode 100644 index 0000000000000000000000000000000000000000..64cdce7eb51a4469d019caa1195fa5542763264e --- /dev/null +++ b/day 5/day_5_mark.js @@ -0,0 +1,13 @@ +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