-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (68 loc) · 2.27 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
const fs = require('fs/promises');
async function load_input() {
try {
const data = await fs.readFile('day01_input.txt', { encoding: 'utf8' });
return data
} catch (err) {
console.log(err);
}
}
var elf_count = 0;
var elf_current = 0;
var elf_cals = [0];
load_input().then(allFileContents => {
allFileContents.split(/\r?\n/).forEach(line => {
var calories = 0;
if (line) {
elf_current += Number(line)
elf_cals[elf_count] = elf_current
}
else {
elf_count++
elf_current = 0
}
});
console.log(`elf_count: ${elf_count}`);
// console.log(`before max elf_cals: ${elf_cals}`);
const max = Math.max(...elf_cals);
console.log(`max elf_cals: ${max}`);
// console.log(`before sorted elf_cals: ${elf_cals}`);
elf_cals.sort(function(a, b){return b - a})
console.log(`elf_cals1: ${elf_cals[0]}`);
console.log(`elf_cals2: ${elf_cals[1]}`);
console.log(`elf_cals3: ${elf_cals[2]}`);
const top3_cals = elf_cals[0] + elf_cals[1] + elf_cals[2]
console.log(`top3 cals total: ${top3_cals}`);
});
// const yargs = require("yargs");
// const axios = require("axios");
// const options = yargs
// .usage("Usage: -n <name>")
// .option("n", { alias: "name", describe: "Your name", type: "string", demandOption: true })
// .option("s", { alias: "search", describe: "Search term", type: "string" })
// .argv;
// const greeting = `Hello, ${options.name}!`;
// console.log(greeting);
// if (options.search) {
// console.log(`Searching for jokes about ${options.search}...`)
// } else {
// console.log("Here's a random joke for you:");
// }
// // The url depends on searching or not
// const url = options.search ? `https://icanhazdadjoke.com/search?term=${escape(options.search)}` : "https://icanhazdadjoke.com/";
// axios.get(url, { headers: { Accept: "application/json", "User-Agent": "axios 0.21.1" } })
// .then(res => {
// if (options.search) {
// // if searching for jokes, loop over the results
// res.data.results.forEach( j => {
// console.log("\n" + j.joke);
// });
// if (res.data.results.length === 0) {
// console.log("no jokes found :'(");
// }
// } else {
// console.log(res.data.joke);
// }
// });
// .catch(() => {e => console.log(e)});