From 109f3d47c80b962f6f6b1a232f7a59216bd3917f Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 3 Dec 2022 18:52:43 -0500 Subject: [PATCH] smol improvements --- day_01/solution.ts | 6 +++--- day_02/solution.ts | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/day_01/solution.ts b/day_01/solution.ts index 93e6388..098fa9f 100644 --- a/day_01/solution.ts +++ b/day_01/solution.ts @@ -45,10 +45,10 @@ Find the Elf carrying the most Calories. How many total Calories is that Elf car import { group, sum } from "../_utils"; -const groupSnacksByElf = (lines: string[]) => +const groupSnacksByElf = (lines: string[]): string[][] => group(lines, (_, line) => line === "", false); -const parseCaloriesToInts = (snacks: string[]) => +const parseCaloriesToInts = (snacks: string[]): number[] => snacks.map((calorie) => parseInt(calorie)); const sumCalories = (snacks: number[]): number => snacks.reduce(sum, 0); @@ -74,7 +74,7 @@ In the example above, the top three Elves are the fourth Elf (with 24000 Calorie Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total? */ -const findTopThreeElves = (leaders: number[], calorieSum: number) => { +const findTopThreeElves = (leaders: number[], calorieSum: number): number[] => { const [first, second, third] = leaders; if (calorieSum > first) { diff --git a/day_02/solution.ts b/day_02/solution.ts index 70e96ab..4b45f90 100644 --- a/day_02/solution.ts +++ b/day_02/solution.ts @@ -30,7 +30,7 @@ In this example, if you were to follow the strategy guide, you would get a total What would your total score be if everything goes exactly according to your strategy guide? */ -import { emptyLines } from "../_utils"; +import { emptyLines, sum } from "../_utils"; export function part1_solver(lines: string[]): number { const score_map = { @@ -63,12 +63,12 @@ export function part1_solver(lines: string[]): number { return score; }; - return lines.filter(emptyLines).reduce((totalScore, match) => { - const [opponent, player] = match.split(" "); - const roundScore = scoreRound(opponent, player); + const toRoundScore = (line: string): number => { + const [opponent, player] = line.split(" "); + return scoreRound(opponent, player); + }; - return (totalScore += roundScore); - }, 0); + return lines.filter(emptyLines).map(toRoundScore).reduce(sum, 0); } /* @@ -118,10 +118,10 @@ export function part2_solver(lines: string[]): number { return score; }; - return lines.filter(emptyLines).reduce((totalScore, match) => { - const [opponent, outcome] = match.split(" "); - const roundScore = scoreRound(opponent, outcome); + const toRoundScore = (line: string): number => { + const [opponent, outcome] = line.split(" "); + return scoreRound(opponent, outcome); + }; - return (totalScore += roundScore); - }, 0); + return lines.filter(emptyLines).map(toRoundScore).reduce(sum, 0); }