Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created December 5, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/15e05dd7eb465c00bcf938496ce1a038 to your computer and use it in GitHub Desktop.
Save tluyben/15e05dd7eb465c00bcf938496ce1a038 to your computer and use it in GitHub Desktop.
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