Last active
December 9, 2017 07:27
-
-
Save kevincharm/82cc45b5ff9eaf5bcbff27a489cd3ff9 to your computer and use it in GitHub Desktop.
aoc2017
This file contains hidden or 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
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => main()) | |
const evil = eval | |
class DepthCounter { | |
constructor() { | |
this.depth = 0 | |
this.total = 0 | |
} | |
feed(char) { | |
if (char === '{') { | |
this.depth++ | |
} else if (char === '}') { | |
this.total += this.depth-- | |
} | |
} | |
parse(str) { | |
str | |
.split('') | |
.forEach(c => this.feed(c)) | |
} | |
} | |
class Cleaner { | |
constructor() { | |
this.state = 'idle' | |
this.cancel = false | |
this.output = '' | |
this.garbage = '' | |
} | |
feed(char) { | |
if (this.cancel) { | |
this.cancel = false | |
return | |
} | |
if (char === '!') { | |
this.cancel = true | |
return | |
} | |
switch (this.state) { | |
case 'idle': | |
if (char === '<') { | |
this.state = 'garbage' | |
} else { | |
this.output += char | |
} | |
break | |
case 'garbage': | |
if (char === '>') { | |
this.state = 'idle' | |
} else { | |
this.garbage += char | |
} | |
break | |
default: | |
} | |
} | |
parse(str) { | |
str | |
.split('') | |
.forEach(c => this.feed(c)) | |
} | |
} | |
function main() { | |
const cleaner = new Cleaner() | |
cleaner.parse(input) | |
const depthCounter = new DepthCounter() | |
depthCounter.parse(cleaner.output) | |
console.log(`total: ${depthCounter.total}`) | |
console.log(`garbage_n: ${cleaner.garbage.length}`) | |
} |
This file contains hidden or 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
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => main()) | |
const evil = eval | |
const sumRecurse = (arr, n = 1) => | |
!arr.length ? n : | |
n + arr | |
.map(c => sumRecurse(c, n + 1)) | |
.reduce((p, c) => p + c, 0) | |
function main() { | |
let nGarbage = 0 | |
const cancel = input | |
.replace(/!./g, '') | |
const ecma = cancel | |
.replace(/<.*?>/g, match => { | |
nGarbage += match.length - 2 | |
return '' | |
}) | |
.replace(/{,{/g, '{{') | |
.replace(/},}/g, '}}') | |
.replace(/{/g, '[') | |
.replace(/}/g, ']') | |
console.log(ecma) | |
const depth = sumRecurse(evil(ecma)) | |
console.log(`total: ${depth}`) | |
console.log(`garbage_n: ${nGarbage}`) | |
} |
This file contains hidden or 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
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => main()) | |
const evil = eval | |
const sumRecurse = (arr, n) => { | |
if (!arr.length) { | |
return n | |
} | |
return n + arr | |
.map(c => sumRecurse(c, n + 1)) | |
.reduce((p, c) => p + c, 0) | |
} | |
class Cleaner { | |
constructor() { | |
this.state = 'idle' | |
this.cancel = false | |
this.output = '' | |
this.garbage = '' | |
} | |
feed(char) { | |
if (this.cancel) { | |
this.cancel = false | |
return | |
} | |
if (char === '!') { | |
this.cancel = true | |
return | |
} | |
switch (this.state) { | |
case 'idle': | |
if (char === '<') { | |
this.state = 'garbage' | |
} else { | |
this.output += char | |
} | |
break | |
case 'garbage': | |
if (char === '>') { | |
this.state = 'idle' | |
} else { | |
this.garbage += char | |
} | |
break | |
default: | |
} | |
} | |
parse(str) { | |
str | |
.split('') | |
.forEach(c => this.feed(c)) | |
} | |
total() { | |
const ecma = this.output | |
.replace(/{/g, '[') | |
.replace(/}/g, ']') | |
const groups = evil(ecma) | |
return sumRecurse(groups, 1) | |
} | |
} | |
function main() { | |
const cleaner = new Cleaner() | |
cleaner.parse(input) | |
console.log(`total: ${cleaner.total()}`) | |
console.log(`garbage_n: ${cleaner.garbage.length}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment