Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created Dec 5, 2021
Embed
What would you like to do?
import { accessSync, promises as fs } from 'fs';
(async()=>{
const input = await fs.readFile("./interpretertests/aoc_21_2_input.txt", 'utf8')
const result = input.split('\n').reduce((acc, line) => {
let [cmd, i] = line.split(' ')
i = parseInt(i)
switch(cmd) {
case 'forward':
acc.horz += i
break
case 'down':
acc.depth += i
break
case 'up':
acc.depth -= i
break
}
return acc
}, {depth: 0, horz: 0})
console.log(result.depth*result.horz)
const result2 = input.split('\n').reduce((acc, line) => {
let [cmd, i] = line.split(' ')
i = parseInt(i)
switch(cmd) {
case 'forward':
acc.horz += i
acc.depth += acc.aim*i
break
case 'down':
acc.aim += i
break
case 'up':
acc.aim -= i
break
}
return acc
}, {depth: 0, horz: 0, aim: 0})
console.log(result2.depth*result2.horz)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment