Skip to content

Commit

Permalink
extract utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Dec 2, 2024
1 parent 37dd8f1 commit 8efeb41
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 203 deletions.
7 changes: 4 additions & 3 deletions 2022/03/solution.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { intersection } from '@magiczne/advent-of-code-ts-core/array'
import { isUpperCase } from '@magiczne/advent-of-code-ts-core/string'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { cluster, unique } from 'radash'
import { intersection, solutionExample, solutionPart1, solutionPart2 } from '../util'
import { isUpperCase } from '@magiczne/advent-of-code-ts-core/string'
import { solutionExample, solutionPart1, solutionPart2 } from '../util'

type Rucksack = [Array<string>, Array<string>]

Expand All @@ -13,7 +14,7 @@ const getRucksacks = (file: string): Array<string> => {
return readFileSync(resolve(__dirname, file)).toString().trim().split('\n')
}

const calculatePriorities = (itemTypes: Array<Array<string>>): number => {
const calculatePriorities = (itemTypes: ReadonlyArray<ReadonlyArray<string>>): number => {
return itemTypes
.map((intersection, index) => {
const noDuplicates = unique(intersection)
Expand Down
2 changes: 1 addition & 1 deletion 2022/05/solution.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isUpperCaseLetter } from '@magiczne/advent-of-code-ts-core/string'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { cluster, last } from 'radash'
import { transpose } from 'ramda'
import { solutionExample, solutionPart1, solutionPart2 } from '../util'
import { isUpperCaseLetter } from '@magiczne/advent-of-code-ts-core/string'

type Stack = Array<Uppercase<string>>
type Stacks = Record<number, Stack>
Expand Down
3 changes: 2 additions & 1 deletion 2022/14/solution.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { windows } from '@magiczne/advent-of-code-ts-core/array'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { range, xprod } from 'ramda'
import { solutionExample, solutionPart1, solutionPart2, windows } from '../util'
import { solutionExample, solutionPart1, solutionPart2 } from '../util'

type Point = { x: number; y: number }
type Cell = '.' | '#' | 'o'
Expand Down
3 changes: 2 additions & 1 deletion 2022/20/solution.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CircularLinkedList, LinkedListNode } from '@magiczne/advent-of-code-ts-core/structures'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { CircularLinkedList, LinkedListNode, solutionExample, solutionPart1, solutionPart2 } from '../util'
import { solutionExample, solutionPart1, solutionPart2 } from '../util'

const __dirname = fileURLToPath(new URL('.', import.meta.url))
const part2DecryptionKey = 811589153
Expand Down
2 changes: 1 addition & 1 deletion 2022/21/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const getHumanValue = (file: string): number => {
delete monkeys['humn']

const dependent = Object.values(monkeys).find(monkey => monkey.dependsOn.includes('humn'))!

// @ts-expect-error
const operation = getInverseOperation(dependent.dependsOn, dependent.operator!)

Expand Down
19 changes: 0 additions & 19 deletions 2022/util/array.ts

This file was deleted.

4 changes: 1 addition & 3 deletions 2022/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './array'
export * from './linked-list'
export * from './output'
export * from './output'
2 changes: 1 addition & 1 deletion 2023/01/solution.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isNumeric } from '@magiczne/advent-of-code-ts-core/string'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { first, last } from 'radash'
import { solutionExample, solutionPart1, solutionPart2 } from '../util/index.ts'
import { isNumeric } from '@magiczne/advent-of-code-ts-core/string'

const __dirname = fileURLToPath(new URL('.', import.meta.url))

Expand Down
2 changes: 1 addition & 1 deletion 2023/03/schematic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { range } from 'radash'
import { isNumeric } from '@magiczne/advent-of-code-ts-core/string'
import { range } from 'radash'

interface Point {
x: number
Expand Down
2 changes: 1 addition & 1 deletion 2023/04/card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { intersection } from '@magiczne/advent-of-code-ts-core/array'
import { range } from 'ramda'
import { intersection } from '../util/index.ts'

class Card {
private readonly matchedNumbers: ReadonlyArray<number>
Expand Down
2 changes: 1 addition & 1 deletion 2023/05/almanac.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Range } from '@magiczne/advent-of-code-ts-core/structures'
import { first, last, unique } from 'radash'
import { Range } from '../util/range.ts'

const enum Category {
Seed = 'seed',
Expand Down
2 changes: 1 addition & 1 deletion 2023/05/solution.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Range } from '@magiczne/advent-of-code-ts-core/structures'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { solutionExample, solutionPart1, solutionPart2 } from '../util/index.ts'
import { Range } from '../util/range.ts'
import { Almanac, AlmanacMap, AlmanacMapRange, Category } from './almanac.ts'

const __dirname = fileURLToPath(new URL('.', import.meta.url))
Expand Down
4 changes: 2 additions & 2 deletions 2023/08/solution.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { leastCommonMultiple } from '@magiczne/advent-of-code-ts-core/number'
import { Tree, TreeNode } from '@magiczne/advent-of-code-ts-core/structures'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { solutionExample, solutionPart1, solutionPart2 } from '../util/index.ts'
import { leastCommonMultiple } from '../util/number.ts'
import { Tree, TreeNode } from '../util/tree.ts'

const __dirname = fileURLToPath(new URL('.', import.meta.url))

Expand Down
33 changes: 0 additions & 33 deletions 2023/util/array.ts

This file was deleted.

2 changes: 0 additions & 2 deletions 2023/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './array.ts'
export * from './linked-list.ts'
export * from './output.ts'
117 changes: 0 additions & 117 deletions 2023/util/linked-list.ts

This file was deleted.

11 changes: 0 additions & 11 deletions 2023/util/number.ts

This file was deleted.

4 changes: 2 additions & 2 deletions ts-core/array/adjacent-diff.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Use with Array.map to get difference between adjacent items
* Difference between adjacent array items
*
* e.g: [7, 6, 4, 2, 1].map(adjacentDiff)
* e.g: adjacentDiff([7, 6, 4, 2, 1])
* returns [1, 2, 2, 1]
*/
const adjacentDiff = (line: ReadonlyArray<number>): ReadonlyArray<number> => {
Expand Down
7 changes: 7 additions & 0 deletions ts-core/array/cycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function* cycle<T>(array: ReadonlyArray<T>): Generator<T, void, undefined> {
while (true) {
yield* array
}
}

export { cycle }
5 changes: 4 additions & 1 deletion ts-core/array/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './adjacent-diff'
export { adjacentDiff } from './adjacent-diff'
export { cycle } from './cycle'
export { intersection } from './intersection'
export { windows } from './windows'
7 changes: 7 additions & 0 deletions ts-core/array/intersection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const intersection = <T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>): ReadonlyArray<T> => {
return array1.filter(value => {
return array2.includes(value)
})
}

export { intersection }
13 changes: 13 additions & 0 deletions ts-core/array/windows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const windows = <T>(array: ReadonlyArray<T>, windowLength: number): ReadonlyArray<ReadonlyArray<T>> => {
return array
.map((_, index) => {
if (index <= array.length - windowLength) {
return array.slice(index, index + windowLength)
}

return []
})
.filter(window => window.length > 0)
}

export { windows }
5 changes: 5 additions & 0 deletions ts-core/number/greatest-common-denominator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const greatestCommonDenominator = (a: number, b: number): number => {
return a % b === 0 ? b : greatestCommonDenominator(b, a % b)
}

export { greatestCommonDenominator }
2 changes: 2 additions & 0 deletions ts-core/number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { greatestCommonDenominator } from './greatest-common-denominator'
export { leastCommonMultiple } from './least-common-multiple'
9 changes: 9 additions & 0 deletions ts-core/number/least-common-multiple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { greatestCommonDenominator } from './greatest-common-denominator'

const leastCommonMultiple = (numbers: ReadonlyArray<number>): number => {
return numbers.reduce((lhs, rhs) => {
return (lhs * rhs) / greatestCommonDenominator(lhs, rhs)
})
}

export { leastCommonMultiple }
File renamed without changes.
4 changes: 4 additions & 0 deletions ts-core/structures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { DirectedGraph } from './directed-graph'
export { CircularLinkedList, LinkedList, LinkedListNode } from './linked-list'
export { Range } from './range'
export { Tree, TreeNode } from './tree'
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion 2023/util/tree.ts → ts-core/structures/tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cycle } from './array.ts'
import { cycle } from '../array/cycle'

class TreeNode<T> {
constructor(
Expand Down

0 comments on commit 8efeb41

Please sign in to comment.