Last active
December 9, 2020 08:37
-
-
Save clemos/d6d799bfd86a89eb5f2ec150439a6a9e to your computer and use it in GitHub Desktop.
AoC 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/1/input") | |
.then(res => res.text()) | |
.then(text => text.split("\n").map(v => parseInt(v))) | |
.then(values => { | |
for(let i=0; i<values.length; i++) { | |
const v1 = values[i] | |
for(let j=i; j<values.length; j++) { | |
const v2 = values[j] | |
if( v1+v2 === 2020) { | |
return v1*v2 | |
} | |
} | |
} | |
}).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parse = (input, base) => | |
parseInt(base.split('').reduce((str, ch, index) => str.replaceAll(ch,index), input), base.length) | |
fetch("https://adventofcode.com/2020/day/5/input").then(res => res.text()).then(text => | |
text.split("\n") | |
.map(line => parse(line.substr(0,7), "FB") * 8 + parse(line.substr(7),"LR")) | |
.find((value, index, values) => !values.includes(value+1) && values.includes(value+2)) | |
+ 1 | |
).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/6/input").then(res => res.text()).then(input => | |
input.split("\n\n").map(group => group.split("\n").reduce((responses, value) => value.split("").forEach((letter) => responses.add(letter)) || responses, new Set()).size).reduce((a,b) => a+b, 0) | |
).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/1/input").then(res => res.text()).then(text => text.split("\n").map(v => parseInt(v))).then(values => { | |
for(let i=0; i<values.length; i++) { | |
const v1 = values[i] | |
for(let j=i+1; j<values.length; j++) { | |
const v2 = values[j] | |
for(let l=j+1; l<values.length; l++) { | |
const v3 = values[l] | |
if( v1+v2+v3 === 2020) { | |
return v1*v2*v3 | |
} | |
} | |
} | |
} | |
}).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/2/input").then(res => res.text()).then(text => text.split("\n")).then(data => | |
data.filter(line => { | |
try { | |
const columns = line.split(" ") | |
const range = columns.shift().split('-').map(v => parseInt(v)) | |
const letter = columns.shift().substr(0,1) | |
const password = columns.join(" ") | |
const n = password.split(letter).length-1 | |
console.log({line, n, range}) | |
return (n >= range[0]) && (n<= range[1]) | |
} catch(e) { | |
console.error(`Error for line "${line}"`, e) | |
return false | |
} | |
}).length | |
).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/2/input").then(res => res.text()).then(text => text.split("\n")).then(data => | |
data.filter(line => { | |
try { | |
const columns = line.split(" ") | |
const range = columns.shift().split('-').map(v => parseInt(v)) | |
const letter = columns.shift().substr(0,1) | |
const password = columns.join(" ") | |
const hasLetterAt = range.map((i) => password.charAt(i-1) === letter) | |
return (hasLetterAt[0] && !hasLetterAt[1]) || (hasLetterAt[1] && !hasLetterAt[0]) | |
} catch(e) { | |
console.error(`Error for line "${line}"`, e) | |
return false | |
} | |
}).length | |
).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/3/input").then(res => res.text()).then(text => text.split("\n")).then(lines => lines.map((line, index) => line.charAt(3*index % line.length) === '#').reduce((acc, tree) => acc + (tree ? 1 : 0), 0)).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/3/input").then(res => res.text()).then(text => text.split("\n")).then(lines => | |
lines | |
.map((line, index) => | |
[[1,1],[3,1],[5,1],[7,1],[1,2]] | |
.map(([right,down]) => (index % down === 0) && line.charAt((right * (index / down)) % line.length) === '#') | |
) | |
.reduce((acc, line) => line.map((tree, col) => (acc[col] || 0) + (tree?1:0)), []) | |
.reduce((acc, v) => acc * v, 1) | |
) | |
.then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://adventofcode.com/2020/day/4/input").then(res => res.text()).then(input => | |
input.split("\n\n") | |
.map(line => line.split(/[ \n]/).map(entry => entry.split(":")) | |
.reduce( | |
(missing,[key, value]) => value && missing.delete(key) && missing || missing, | |
new Set(["byr","iyr","eyr","hgt","hcl","ecl","pid"])) | |
) | |
.filter(missing => missing.size === 0) | |
.length | |
).then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isBetween = (min, max) => v => Math.max(min, Math.min(max, parseInt(v))) === parseInt(v) | |
const rules = { | |
"byr": isBetween(1920, 2002), | |
"iyr": isBetween(2010, 2020), | |
"eyr": isBetween(2020, 2030), | |
"hgt": v => { | |
switch(v.substr(-2)) { | |
case 'cm': return isBetween(150, 193)(v) | |
case 'in': return isBetween(59, 76)(v) | |
} | |
return false | |
}, | |
"hcl": v => /^#[0-9a-f]{6}$/.test(v), | |
"ecl": v => ["amb","blu","brn","gry","grn","hzl","oth"].includes(v), | |
"pid": v => /^[0-9]{9}$/.test(v) | |
} | |
fetch("https://adventofcode.com/2020/day/4/input").then(res => res.text()).then(input => | |
input.split("\n\n") | |
.map(line => line.split(/[ \n]/) | |
.map(entry => entry.split(":") | |
) | |
.reduce( | |
(missing,[key, value]) => value && rules[key] && rules[key](value) && missing.delete(key) && missing || missing, | |
new Set(Object.keys(rules))) | |
) | |
.filter(missing => missing.size === 0) | |
.length | |
) | |
.then(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parse = (input, base) => | |
parseInt(base.split('').reduce((str, ch, index) => str.replaceAll(ch,index), input), base.length) | |
fetch("https://adventofcode.com/2020/day/5/input").then(res => res.text()).then(text => | |
text.split("\n") | |
.map(line => parse(line.substr(0,7), "FB") * 8 + parse(line.substr(7),"LR")) | |
.reduce((current, id) => (id > current) ? id : current, 0) | |
).then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment