From 43fc4da6b2ccad433442af12e482c59646ea3d74 Mon Sep 17 00:00:00 2001 From: Carlos de la Orden Date: Thu, 28 Dec 2023 15:54:33 +0100 Subject: [PATCH] day 9 part 2 --- src/days/9/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/days/9/index.ts b/src/days/9/index.ts index c58aa7a..c53cdff 100644 --- a/src/days/9/index.ts +++ b/src/days/9/index.ts @@ -1,6 +1,6 @@ import { explode, splitLines, sum } from '@/lib/utils' import assert from 'assert' -import { createPipe, equals, last, map, pipe } from 'remeda' +import { createPipe, equals, first, last, map, pipe } from 'remeda' export const makeHistory = (line: string) => explode(line, ' ').map(Number) @@ -45,6 +45,13 @@ export const calculateNext = (history: number[], debug = false) => { return next + (last(history) ?? 0) } +export const calculatePrev = (history: number[]) => { + assert(history.length, 'empty history') + const histories = reduceHistories(history) + const prev = histories.reduceRight((acc, h) => (first(h) ?? 0) - acc, 0) + return (first(history) ?? 0) - prev +} + export function day9PartOne(sample: string, input: string) { assert(sample && input, 'Bad input data') const solution = pipe(input, parseInput, map(calculateNext), sum) @@ -53,4 +60,6 @@ export function day9PartOne(sample: string, input: string) { export function day9PartTwo(sample: string, input: string) { assert(sample && input, 'Bad input data') + const solution = pipe(input, parseInput, map(calculatePrev), sum) + console.log('Part Two', solution) }