Skip to content

Commit

Permalink
day 9 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cdelaorden committed Dec 28, 2023
1 parent b874f1f commit 43fc4da
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/days/9/index.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 43fc4da

Please sign in to comment.