Skip to content

Instantly share code, notes, and snippets.

@mplewis
Created December 6, 2019 21:09
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 mplewis/1b8240e82900aa10673b3b18a3b1cebd to your computer and use it in GitHub Desktop.
Save mplewis/1b8240e82900aa10673b3b18a3b1cebd to your computer and use it in GitHub Desktop.
Advent of Code stuff
const input = `
106404
140515
142745
120767
79665
54235
127391
72207
70799
79485
103994
129583
132791
95135
121194
129425
64861
123233
132805
87916
111395
126625
113045
61704
65413
145820
75988
74717
115137
85331
86833
86063
85464
139738
103372
101942
52741
77660
112745
103109
106301
141714
74546
55474
106747
140234
60426
145867
144810
94179
101606
77763
139291
104246
148513
126828
64624
139058
85839
86636
62198
137358
76711
87848
141711
114079
71639
95896
104522
61929
72199
142790
137736
123437
91872
127661
111179
51548
83452
91196
117798
84484
75517
83820
97407
89181
71428
72758
73076
109957
50601
74571
65556
129765
80626
126995
73480
71360
103288
85670
`.trim().split('\n').map(n => parseInt(n))
// Day 1 Part 1
function sum(ns: number[]): number {
return ns.reduce((total, n) => n + total, 0)
}
function toFuel(mass: number): number {
return Math.floor(mass / 3) - 2
}
const baseFuels = input.map(mass => toFuel(mass))
const baseFuel = sum(baseFuels)
// Day 1 Part 2
function auxFuelCost(baseFuel: number) {
let curr = baseFuel
let sum = 0
do {
curr = toFuel(curr)
if (curr > 0) sum += curr
} while (curr > 0)
return sum
}
const auxFuelCosts = input.map(mass => auxFuelCost(mass))
const totalFuel = sum(auxFuelCosts)
console.log(totalFuel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment