Created
December 5, 2021 10:53
-
-
Save tluyben/15e05dd7eb465c00bcf938496ce1a038 to your computer and use it in GitHub Desktop.
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
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