Skip to content

Commit

Permalink
♻️ Implement pipe function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrennanfr committed Dec 12, 2024
1 parent 80baafb commit 76830b7
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default [
"id-length": [
"error",
{
exceptions: ["λ", "_", "$", "c", "r", "U"],
exceptions: ["λ", "_", "$", "c", "l", "r", "U"],
},
],
"no-restricted-syntax": [
Expand Down
4 changes: 2 additions & 2 deletions src/04/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const solve = (
/* --------------------------------- part01 --------------------------------- */

export const part01 = (input: string): number =>
solve(U.grid(input), (cell, position, grid) =>
solve(U.grid(input, ""), (cell, position, grid) =>
cell === "X" ?
U.count(
[U.east, U.southEast],
Expand All @@ -30,7 +30,7 @@ export const part01 = (input: string): number =>
/* --------------------------------- part02 --------------------------------- */

export const part02 = (input: string): number =>
solve(U.grid(input), (cell, position, grid) =>
solve(U.grid(input, ""), (cell, position, grid) =>
cell === "A" ?
Number(
[
Expand Down
2 changes: 1 addition & 1 deletion src/06/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const parse = (
guard: { facing: string; position: U.Position }
obstacles: Set<string>
} => {
const grid = U.map2D(U.grid(input), (cell, position) => ({
const grid = U.map2D(U.grid(input, ""), (cell, position) => ({
cell,
position,
}))
Expand Down
2 changes: 1 addition & 1 deletion src/08/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as U from "@/utils"
const parse = (
input: string,
): { antennas: Record<string, Set<string>>; grid: string[][] } => {
const grid = U.grid(input)
const grid = U.grid(input, "")
const positions = U.map2D(grid, U.index)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/10/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as U from "@/utils"

const parse = (input: string): number[][] => U.map2D(U.grid(input), Number)
const parse = (input: string): number[][] => U.map2D(U.grid(input, ""), Number)

const getStarts = (grid: number[][]): U.Position[] =>
U.map2D(grid, U.index)
Expand Down
2 changes: 1 addition & 1 deletion src/12/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as U from "@/utils"

const parse = (input: string): string[][] => U.grid(input)
const parse = (input: string): string[][] => U.grid(input, "")

const getPositionHash = (position: U.Position): string =>
U.join([U.r(position), U.c(position)], "_")
Expand Down
16 changes: 12 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export type Flip<T extends unknown[]> =
T extends [infer U, ...infer V] ? [...Flip<V>, U] : []

export type Pipe<T extends unknown[]> = {
l: <U>(function_: (...arguments_: T) => U) => Pipe<[U]>
return: () => T[0]
}

export type Position = Record<"c" | "r", number>

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -136,10 +141,8 @@ export const flatMap = <T, U>(
export const fromEntries = <T>(entries: [string, T][]): Record<string, T> =>
Object.fromEntries(entries)

export const grid = (
string: string,
separator: RegExp | string = "",
): string[][] => map(lines(string), λ(_(split), separator))
export const grid = (string: string, separator: RegExp | string): string[][] =>
map(lines(string), λ(_(split), separator))

export const guard = <T>(value: T): NonNullable<T> =>
isDefined(value) ? value : panic(`${String(value)} did not pass guard!`)
Expand Down Expand Up @@ -211,6 +214,11 @@ export const int = (value: unknown): number => Number(value) // eslint-disable-l
export const join = (array: (number | string)[], separator = ""): string =>
array.join(separator)

export const l = <T extends unknown[]>(...arguments_: T): Pipe<T> => ({
l: (function_) => l(function_(...arguments_)),
return: () => arguments_[0], // eslint-disable-line functional/functional-parameters
})

export const last = <T>(array: T[]): T => at(array, -1)

export const lastIndexOf = (string: string, value: string): number =>
Expand Down

0 comments on commit 76830b7

Please sign in to comment.