Ben Ashton
1 year ago
commit
ef4a9f7182
4 changed files with 4800 additions and 0 deletions
@ -0,0 +1,28 @@
|
||||
import { readFileSync } from 'node:fs'; |
||||
|
||||
const input = readFileSync('input', 'utf-8'); |
||||
|
||||
const elfCalories = input |
||||
.split(/\n{2,}/) |
||||
.filter(calories => calories) |
||||
.map(calories => |
||||
calories |
||||
.split('\n') |
||||
.map(count => parseInt(count)) |
||||
); |
||||
|
||||
const totalElfCalories = elfCalories |
||||
.map(calories => |
||||
calories.reduce((total, count) => total + count, 0) |
||||
); |
||||
|
||||
totalElfCalories.sort((a, b) => a - b); |
||||
|
||||
const mostCalories = totalElfCalories[totalElfCalories.length - 1]; |
||||
console.log(`Elf with most calories has: ${mostCalories}`); |
||||
|
||||
const topThreeCalories = totalElfCalories |
||||
.slice(-3) |
||||
.reduce((total, count) => total + count, 0); |
||||
console.log(`Top three calory count: ${topThreeCalories}`); |
||||
|
@ -0,0 +1,34 @@
|
||||
import { readFileSync } from 'node:fs'; |
||||
|
||||
const input = readFileSync('input', 'utf-8'); |
||||
|
||||
const data = input |
||||
.split('\n') |
||||
.filter(line => line) |
||||
.map(line => { |
||||
const play = line.split(' '); |
||||
return [ |
||||
['A', 'B', 'C'].indexOf(play[0]), |
||||
['X', 'Y', 'Z'].indexOf(play[1]) |
||||
] |
||||
}); |
||||
|
||||
const scoreGame = plays => |
||||
plays.map(([o, p]) => |
||||
(p + 1) + // shape score
|
||||
3 * (o == p) + // draw
|
||||
6 * ((o + 1) % 3 == p) // win
|
||||
); |
||||
|
||||
const wrongScores = scoreGame(data); |
||||
const totalScore = wrongScores.reduce((t, s) => t + s, 0) |
||||
console.log(`Total Score: ${totalScore}`); |
||||
|
||||
const actualPlays = data.map(([o, a]) => [ |
||||
o, |
||||
(o + [2, 0, 1][a]) % 3 |
||||
]); |
||||
const correctScores = scoreGame(actualPlays); |
||||
const correctTotalScore = correctScores.reduce((t, s) => t + s, 0) |
||||
console.log(`Correct Total Score: ${correctTotalScore}`); |
||||
|
Loading…
Reference in new issue