Skip to content

Commit

Permalink
feat: revamp code generation and input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
amorriscode committed Dec 1, 2022
1 parent 5664ae7 commit 894c751
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 51 deletions.
10 changes: 9 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"presets": ["@babel/env"]
"presets": ["@babel/env"],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ This repo is used to help maintain my [JavaScript solutions](https://github.com/
YEAR=20XX DAY=0X yarn new
```

## Copy your inputs

Inside the `/inputs` directory, you'll see have an `input.txt` and `example.txt` for each day you are solving. The `input.txt` is your unique Advent of Code input. The `example.txt` is for the example input given in the text of the question.
In the future, `input.txt` will be automatically downloaded for you.
## Run tests while you're solve the problem

```bash
Expand Down
2 changes: 1 addition & 1 deletion _template/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Day _______
# Advent of Code {{YEAR}} Day {{DAY}}
15 changes: 3 additions & 12 deletions _template/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { example, data } from './input'
export const parseInput = (input) => input.split('\n')

export const inputParser = (input) => input.split('\n')

const parsedData = inputParser(input)

export const part1 = (input = parsedData) => {
export const part1 = (input) => {
return
}

export const part2 = (input = parsedData) => {
export const part2 = (input) => {
return
}

export default {
part1,
part2,
}
20 changes: 10 additions & 10 deletions _template/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const { inputParser, part1, part2 } = require('./index.js')
const { example, data } = require('./input.js')
const { parseInput, part1, part2 } = require('./index.js')
const { getExampleInput, getInput } = require('./input.js')

test('part 1 example', () => {
expect(part1(inputParser(example))).toBe(0)
test('part 1 example', async () => {
expect(part1(parseInput(await getExampleInput()))).toBe(0)
})

test('part 1 data', () => {
expect(part1(inputParser(data))).toBe(0)
test('part 1 data', async () => {
expect(part1(parseInput(await getInput()))).toBe(0)
})

test('part 2 example', () => {
expect(part2(inputParser(example))).toBe(0)
test('part 2 example', async () => {
expect(part2(parseInput(await getExampleInput()))).toBe(0)
})

test('part 2 data', () => {
expect(part2(inputParser(data))).toBe(0)
test('part 2 data', async () => {
expect(part2(parseInput(await getInput()))).toBe(0)
})
17 changes: 15 additions & 2 deletions _template/input.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export const example = ``
import fs from 'fs'
import path from 'path'

export const data = ``
export const getExampleInput = async () =>
fs
.readFileSync(
path.resolve(process.env.NODE_PATH, 'inputs/{{YEAR}}/{{DAY}}/example.txt')
)
.toString()

export const getInput = async () =>
fs
.readFileSync(
path.resolve(process.env.NODE_PATH, 'inputs/{{YEAR}}/{{DAY}}/input.txt')
)
.toString()
22 changes: 22 additions & 0 deletions bin/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

# Make solution directory
mkdir -p ./solutions/$YEAR/$DAY

# Make input files
mkdir -p ./inputs/$YEAR/$DAY
touch ./inputs/$YEAR/$DAY/input.txt
touch ./inputs/$YEAR/$DAY/example.txt

# Copy the template files over
cp -r ./_template/ ./solutions/$YEAR/$DAY/

# Replace the year template variable
sed -i '' "s/{{YEAR}}/$YEAR/g" ./solutions/$YEAR/$DAY/README.md
sed -i '' "s/{{YEAR}}/$YEAR/g" ./solutions/$YEAR/$DAY/input.js

# Replace the day template variable
sed -i '' "s/{{DAY}}/$DAY/g" ./solutions/$YEAR/$DAY/README.md
sed -i '' "s/{{DAY}}/$DAY/g" ./solutions/$YEAR/$DAY/input.js

echo "Generated code for Advent of Code $YEAR Day $DAY πŸŽ„"
4 changes: 3 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dayToSolve = process.argv[3]

const days = []
const solutions = {}
const inputs = {}

try {
const files = fs.readdirSync(`./solutions/${year}`)
Expand All @@ -19,6 +20,7 @@ try {

days.forEach(async (day) => {
solutions[day] = import(`./solutions/${year}/${day}/index.js`)
inputs[day] = import(`./solutions/${year}/${day}/input.js`)
})

solve(year, dayToSolve, solutions[dayToSolve])
solve(year, dayToSolve, solutions[dayToSolve], inputs[dayToSolve])
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"dependencies": {
"colors": "^1.4.0"
},
"name": "advent-of-code",
"version": "1.1.0",
"description": "A template for solving of Advent of Code in JavaScript",
Expand All @@ -10,16 +7,19 @@
"license": "MIT",
"private": false,
"scripts": {
"new": "mkdir -p ./solutions/$YEAR/$DAY && cp -r ./_template/ ./solutions/$YEAR/$DAY/",
"new": "bin/generate.sh",
"solve": "rollup -c ./rollup.config.js --silent && node ./dist/main.js $YEAR $DAY",
"test": "jest ./solutions/$YEAR/$DAY"
"test": "NODE_PATH=. jest ./solutions/$YEAR/$DAY"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/preset-env": "^7.12.11",
"@rollup/plugin-dynamic-import-vars": "^1.1.1",
"babel-preset-es2015": "^6.24.1",
"jest": "^26.6.3",
"rollup": "^1.27.8"
"rollup": "^1.27.8",
"rollup-plugin-inject-process-env": "^1.3.1",
"colors": "^1.4.0"
}
}
7 changes: 5 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars'
import injectProcessEnv from 'rollup-plugin-inject-process-env'
import path from 'path'

module.exports = {
input: 'main.js',
Expand All @@ -7,8 +9,9 @@ module.exports = {
format: 'cjs',
},
plugins: [
dynamicImportVars({
// options
dynamicImportVars({}),
injectProcessEnv({
NODE_PATH: path.resolve(__dirname),
}),
],
}
10 changes: 5 additions & 5 deletions solve.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import colors from 'colors'

export default async (year, day, solutionsPromise) => {
export default async (year, day, solutionsPromise, inputsPromise) => {
// @TODO: Fix this jank
const resolvedSolutions = await solutionsPromise
const solutions = resolvedSolutions.default
const { part1, part2 } = await solutionsPromise
const { getInput } = await inputsPromise

console.log(' '.bgRed)

Expand All @@ -16,10 +16,10 @@ export default async (year, day, solutionsPromise) => {
console.log('')

console.log(`πŸŽ„πŸŽ„ DAY ${day} PART ONE πŸŽ„πŸŽ„`.bold.green)
console.log(`${await solutions.part1()}\n`)
console.log(`${await part1(getInput())}\n`)

console.log(`πŸŽ„πŸŽ„ DAY ${day} PART TWO πŸŽ„πŸŽ„`.bold.green)
console.log(await solutions.part2())
console.log(await part2(getInput()))

console.log('')

Expand Down
Loading

0 comments on commit 894c751

Please sign in to comment.