Last active
December 11, 2020 16:01
-
-
Save Yiin/f2bcd6feb77ebb3095594947363e0656 to your computer and use it in GitHub Desktop.
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
list = input.split("\n").sort((a, b) => a - b); | |
result = list.reduce( | |
(acc, number) => { | |
const diff = number - acc.last; | |
const key = ["one", "two", "three"][diff - 1]; | |
return { | |
...acc, | |
[key]: acc[key] + 1, | |
last: +number, | |
}; | |
}, | |
{ one: 0, two: 0, three: 1, last: 0 } | |
); | |
console.log(result.one * result.three); | |
// part 2 took forever im mad | |
fullList = [0, ...list, Math.max(...list) + 3].map(Number); | |
cache = {}; | |
calc = (val) => { | |
if (cache[val]) { | |
return cache[val]; | |
} | |
if (val === 0) { | |
return cache[val] = 1; | |
} | |
if (fullList.includes(val)) { | |
cache[val] = calc(val - 1) + calc(val - 2) + calc(val - 3); | |
return cache[val]; | |
} | |
return 0; | |
}; | |
console.log(calc(Math.max(...list) + 3)); |
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
x = input.split('\n'); | |
let count = 0; | |
for (const line of x) { | |
const [, from, to, letter, string] = /(\d+)-(\d+) ([a-z]): ([a-z]+)/g.exec(line); | |
if ([string[from - 1], string[to - 1]].filter(l => l === letter).length === 1) { | |
count++; | |
} | |
} | |
console.log(count); |
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
let rows = input.split('\n'); | |
result = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]].reduce((sum, [incX, incY]) => sum * (() => { | |
let count = 0; | |
for (let x = 0, y = 0; y < rows.length; y += incY, x += incX) { | |
while (x >= rows[y].length) { | |
x = x - rows[y].length; | |
} | |
if (rows[y][x] === '#') { | |
count++; | |
} | |
} | |
return count; | |
})(), 1) | |
console.log(result) |
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
passports = input.split(`\n\n`); | |
console.log(passports.filter(x => { | |
keys = (x.includes('cid:') ? x : x + ' cid:').split(/\s/); | |
return keys.map(key => key.split(':')) | |
.filter(([key, value]) => { | |
switch(key) { | |
case 'byr': | |
return value >= 1920 && value <= 2002; | |
case 'iyr': | |
return value >= 2010 && value <= 2020; | |
case 'eyr': | |
return value >= 2020 && value <= 2030; | |
case 'hgt': { | |
type = value.substr(-2); | |
hgt = value.slice(0, value.length - 2); | |
switch (type) { | |
case 'cm': | |
return hgt >= 150 && hgt <= 193; | |
case 'in': | |
return hgt >= 59 && hgt <= 76; | |
default: | |
return false; | |
} | |
} | |
case 'hcl': | |
return value.length === 7 && value[0] === '#'; | |
return /#[0-9a-f]{6}/.test(value); | |
case 'ecl': | |
return ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(value); | |
case 'pid': | |
return /^[0-9]{9}$/.test(value) | |
case 'cid': | |
return true; | |
default: | |
return false; | |
} | |
}).length === 8 | |
}).length) |
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 seats = input | |
.split('\n') | |
.map(curr => parseInt([...curr].map(x => +['B', 'R'].includes(x)).join(''), 2)) | |
.sort((a, b) => a - b) | |
.reduce((a, b) => ((b - a > 1) && console.log(a + 1), b)) | |
console.log(seats[seats.length-1]) |
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
results = input.split('\n\n').reduce((sums, group) => { | |
lines = group.split('\n').length; | |
part1 = new Set(group.replace(/\n/g, '').split('')).size; | |
const map = group.split('\n').reduce((map, person) => { | |
for (const question of person) { | |
map[question] = (map[question] || 0) + 1; | |
} | |
return map; | |
}, {}); | |
part2 = Object.values(map).reduce((sum, value) => sum + (value === lines), 0); | |
return [sums[0] + part1, sums[1] + part2]; | |
}, [0, 0]); | |
console.log(results); |
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
canContainShinyGoldBag = new Set(['shiny gold']); | |
map = {}; | |
rules = input.split('\n').map(line => { | |
match = line | |
.replace(/\sbags?/g, '') | |
.replace(/\./g, '') | |
.split(/contain|,/g) | |
.map(x => x.trim()) | |
let [parent, ...children] = match; | |
map[parent] = children | |
.filter(child => child !== 'no other') | |
.map(child => { | |
const [, amount, name] = /(\d+) (.*)/g.exec(child); | |
return { amount: +amount, name }; | |
}); | |
return { | |
parent, | |
children: map[parent] | |
}; | |
}); | |
while (true) { | |
const size = canContainShinyGoldBag.size; | |
for (const { parent, children } of rules) { | |
if (children.some(bag => canContainShinyGoldBag.has(bag.name))) { | |
canContainShinyGoldBag.add(parent); | |
} | |
} | |
if (canContainShinyGoldBag.size === size) { | |
break; | |
} | |
} | |
console.log(canContainShinyGoldBag.size - 1); | |
result = 0 | |
traverse = name => { | |
let count = 1 | |
for (child of map[name]) { | |
count += child.amount * traverse(child.name); | |
} | |
return count; | |
} | |
console.log(traverse('shiny gold') - 1); |
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
instructions = input.split('\n') | |
executed = new Set(); | |
acc = 0; | |
for (let line = 0; !executed.has(line);) { | |
const [instruction, value] = instructions[line].split(' '); | |
executed.add(line) | |
switch(instruction) { | |
case 'nop': | |
line++; | |
continue; | |
case 'acc': | |
acc += +value; | |
line++; | |
continue; | |
case 'jmp': | |
line += +value; | |
continue; | |
} | |
} | |
console.log(acc); | |
swapped = new Set(); | |
do { | |
acc = 0; | |
executed = new Set(); | |
swappedSize = swapped.size; | |
for (line = 0; !executed.has(line) && line < instructions.length;) { | |
const [instruction, value] = instructions[line].split(' '); | |
executed.add(line) | |
const actions = { | |
nop() { | |
line++; | |
}, | |
acc() { | |
acc += +value; | |
line++; | |
}, | |
jmp() { | |
line += +value; | |
}, | |
}; | |
switch(instruction) { | |
case 'nop': | |
if (!swapped.has(line) && swappedSize === swapped.size) { | |
swapped.add(line); | |
actions['jmp'](true); | |
continue; | |
} | |
actions['nop'](); | |
continue; | |
case 'acc': | |
actions['acc'](); | |
continue; | |
case 'jmp': | |
if (!swapped.has(line) && swappedSize === swapped.size) { | |
swapped.add(line); | |
actions['nop'](true); | |
continue; | |
} | |
actions['jmp'](); | |
continue; | |
} | |
} | |
} while (line < instructions.length); | |
console.log(acc); |
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
SIZE = 25; | |
arr = input.split('\n').map(Number); | |
result = arr.find((number, index) => { | |
if (index < SIZE) { | |
return false; | |
} | |
list = arr.slice(index - SIZE, index); | |
found = false; | |
main: for (let i = 0; i < list.length; ++i) { | |
for (let j = i; j < list.length; ++j) { | |
if (list[i] + list[j] === number) { | |
found = true; | |
break main; | |
} | |
} | |
} | |
if (!found) { | |
return true; | |
} | |
}); | |
console.log(result); | |
sum = numbers => numbers.reduce((sum, a) => sum + a); | |
list = [] | |
for (const number of arr) { | |
list.push(number); | |
while (sum(list) > result) { | |
list.shift(); | |
} | |
if (sum(list) === result) { | |
console.log(Math.min(...list) + Math.max(...list)); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment