repl.it
@Kognise/

AoC2019 1

Nodejs

Advent of Code is an Advent calendar of programming puzzles. https://adventofcode.com/2019/day/1

fork
loading
Files
  • index.js
  • input.txt
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const getFuel = (mass) => Math.floor(mass / 3) - 2
const text = require('fs').readFileSync('input.txt').toString()

let sum1 = 0
for (let line of text.split('\n')) {
  const parsed = parseInt(line)
  if (!parsed) continue

  sum1 += getFuel(parsed)
}
console.log('Part one:', sum1)

let sum2 = 0
for (let line of text.split('\n')) {
  const parsed = parseInt(line)
  if (!parsed) continue

  let fuel = parsed
  while (true) {
    fuel = getFuel(fuel)
    if (fuel <= 0) break
    sum2 += fuel
  }
}
console.log('Part two:', sum2)