Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Dec 2, 2024
1 parent d54989a commit 6f98054
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 127 deletions.
3 changes: 3 additions & 0 deletions 2022/21/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const readMonkeys = (file: string): Monkeys => {
return Object.fromEntries(monkeys)
}

// @ts-expect-error
const getRootMonkeyValue = (file: string): number => {
const monkeys = readMonkeys(file)
const monkeyOperationCache = new Map<string, number>()
Expand All @@ -106,6 +107,8 @@ 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!)

return 0
Expand Down
6 changes: 3 additions & 3 deletions 2024/d01/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs'
import { counting, sum } from 'radash'

import { solutionRuns, testRuns } from '../util/aoc'
import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'

const part1 = (data: ReadonlyArray<[number, number]>): number => {
const left = data.map(item => item[0]).toSorted((a, b) => a - b)
Expand Down Expand Up @@ -31,5 +31,5 @@ const reader = (file: string): ReadonlyArray<[number, number]> => {
})
}

testRuns('01', reader, part1, part2)
solutionRuns('01', reader, part1, part2)
runExamples(2024, '01', reader, part1, part2)
runSolution(2024, '01', reader, part1, part2)
8 changes: 4 additions & 4 deletions 2024/d02/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs'

import { adjacentDiff } from '@magiczne/advent-of-code-ts-core'
import { solutionRuns, testRuns } from '../util/aoc'
import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import { adjacentDiff } from '@magiczne/advent-of-code-ts-core/array'

const isLineSafe = (line: ReadonlyArray<number>): boolean => {
const sign = Math.sign(line[0])
Expand Down Expand Up @@ -44,5 +44,5 @@ const reader = (file: string): ReadonlyArray<ReadonlyArray<number>> => {
})
}

testRuns('02', reader, part1, part2)
solutionRuns('02', reader, part1, part2)
runExamples(2024, '02', reader, part1, part2)
runSolution(2024, '02', reader, part1, part2)
73 changes: 0 additions & 73 deletions 2024/util/aoc.ts

This file was deleted.

2 changes: 2 additions & 0 deletions ts-core/aoc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './run-examples'
export * from './run-solution'
34 changes: 34 additions & 0 deletions ts-core/aoc/run-examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { existsSync, readdirSync } from 'node:fs'
import { join } from 'node:path'
import { __dirname, examplePart1, examplePart2 } from './util'

const runExamples = <TInput, TResult>(
year: number,
day: string,
reader: (file: string) => TInput,
part1: (data: TInput) => TResult,
part2: (data: TInput) => TResult,
): void => {
const dirPath = join(__dirname, `../../${year}/d${day}/test-runs`)

if (existsSync(dirPath)) {
const testFiles = readdirSync(dirPath)

testFiles.forEach(file => {
const testData = reader(join(dirPath, file))

const start1 = Date.now()
const valuePart1 = part1(testData)
const duration1 = Date.now() - start1

const start2 = Date.now()
const valuePart2 = part2(testData)
const duration2 = Date.now() - start2

examplePart1(valuePart1, file, duration1)
examplePart2(valuePart2, file, duration2)
})
}
}

export { runExamples }
26 changes: 26 additions & 0 deletions ts-core/aoc/run-solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { join } from 'node:path'
import { __dirname, solutionPart1, solutionPart2 } from './util'

const runSolution = <TInput, TResult>(
year: number,
day: string,
reader: (file: string) => TInput,
part1: (data: TInput) => TResult,
part2: (data: TInput) => TResult,
): void => {
const filePath = join(__dirname, `../../${year}/d${day}/input.txt`)
const data = reader(filePath)

const start1 = Date.now()
const valuePart1 = part1(data)
const duration1 = Date.now() - start1

const start2 = Date.now()
const valuePart2 = part2(data)
const duration2 = Date.now() - start2

solutionPart1(valuePart1, duration1)
solutionPart2(valuePart2, duration2)
}

export { runSolution }
29 changes: 29 additions & 0 deletions ts-core/aoc/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import chalk from 'chalk'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))

const examplePart1 = <TResult>(result: TResult, testFileName: string, durationInMs: number): void => {
console.log(chalk.yellow(`[EXM] (${testFileName})\t\tPart 1: ${result}\t\t${durationInMs}ms`))
}

const examplePart2 = <TResult>(result: TResult, testFileName: string, durationInMs: number): void => {
console.log(chalk.red(`[EXM] (${testFileName})\t\tPart 2: ${result}\t\t${durationInMs}ms`))
}

const solutionPart1 = <TResult>(result: TResult, durationInMs: number): void => {
console.log(chalk.green(`[SLN]\t\t\tPart 1: ${result}\t\t${durationInMs}ms`))
}

const solutionPart2 = <TResult>(result: TResult, durationInMs: number): void => {
console.log(chalk.blue(`[SLN]\t\t\tPart 2: ${result}\t\t${durationInMs}ms`))
}

export {
__dirname,
examplePart1,
examplePart2,
solutionPart1,
solutionPart2,
}
1 change: 0 additions & 1 deletion ts-core/index.ts

This file was deleted.

44 changes: 1 addition & 43 deletions turbo/generators/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
{
type: 'add',
path: '{{ turbo.paths.root }}/{{ year }}/d{{ day }}/main.ts',
templateFile: 'templates/typescript/main.ts',
templateFile: 'templates/typescript/main.ts.hbs',
},
{
type: 'add',
Expand All @@ -52,46 +52,4 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
}
]
})

// plop.setGenerator("example", {
// description:
// "An example Turborepo generator - creates a new file at the root of the project",
// prompts: [
// {
// type: "input",
// name: "file",
// message: "What is the name of the new file to create?",
// validate: (input: string) => {
// if (input.includes(".")) {
// return "file name cannot include an extension";
// }
// if (input.includes(" ")) {
// return "file name cannot include spaces";
// }
// if (!input) {
// return "file name is required";
// }
// return true;
// },
// },
// {
// type: "list",
// name: "type",
// message: "What type of file should be created?",
// choices: [".md", ".txt"],
// },
// {
// type: "input",
// name: "title",
// message: "What should be the title of the new file?",
// },
// ],
// actions: [
// {
// type: "add",
// path: "{{ turbo.paths.root }}/{{ dashCase file }}{{ type }}",
// templateFile: "templates/turborepo-generators.hbs",
// },
// ],
// });
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs'

import { testRuns, solutionRuns } from '../util/aoc'
import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'

const part1 = (data: string): number => {
return 0
Expand All @@ -14,5 +14,5 @@ const reader = (file: string): string => {
return readFileSync(file, 'utf-8').trim()
}

testRuns('{{ day }}', reader, part1, part2)
solutionRuns('{{ day }}', reader, part1, part2)
runExamples({{ year }}, '{{ day }}', reader, part1, part2)
runSolution({{ year }}, '{{ day }}', reader, part1, part2)

0 comments on commit 6f98054

Please sign in to comment.