Skip to content

refactor: organize aoc-related functions #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ It follows the directory structure:
> [!NOTE]
> 📂 dist<br>
> 📂 src<br>
> └─ 📂 utils<br>
> └─ 📂 aoc<br>
> └─ 📂 sample<br>
> └─ 📂 2024<br>
> └─── 📂 2024-12-01<br>
Expand Down Expand Up @@ -77,7 +77,7 @@ Each Advent of Code (AOC) event quiz has its folder under **`"/src/<YEAR>/<YYYY-

#### Other Items

- **/src/utils**: Folder containing generic utility helper functions
- **/src/aoc**: 🗃️ Folder containing generic utility and common AoC helper functions
- **/src/dist**: Folder containing the JavaScript files compiled from TypeScript (not committed to the repository)
- **/src/sample**: Miscellaneous random examples
- **/src/index.ts**: Exports all solutions to AOC quiz answer functions
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-01/lib/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { readFile, directory } from '@/utils/file.js'
import { readFile, directory } from '@/aoc/file/utils.js'

export type arrayLists = {
list1: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-01/lib/listTotalDistance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { arrangeArray, ARRAY_ORDERING } from '@/utils/arrays.js'
import { arrangeArray, ARRAY_ORDERING } from '@/aoc/array/utils.js'

/**
* Calculates the total distance between the smallest value-pairs from list `a` and list `b`
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-02/lib/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { readFile, directory } from '@/utils/file.js'
import { readFile, directory } from '@/aoc/file/utils.js'

/**
* Reads the quiz's input file into two (2) string arrays
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-03/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { directory, readFile } from '@/utils/file.js'
import { directory, readFile } from '@/aoc/file/utils.js'
import { extractMultiply, extractMultiplyCondition } from './lib/extractMultiply.js'

const dir = directory(import.meta.url)
Expand Down
29 changes: 9 additions & 20 deletions src/2024/2024-12-04/lib/wordCheckerUtils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
export type CharacterArray = string[][]

interface CoordinateBase {
x: number;
y: number;
}

interface Coordinate extends CoordinateBase {
direction: 1 | -1;
}

interface CoordinateData {
xDirection: 1 | -1;
yDirection: 1 | -1;
data: CharacterArray
}
import type {
Coordinate,
Point,
CoordinateData
} from '@/aoc/point/types.js'

/**
* Finds the `WORD` input parameter in the vertical directions (up/down) from an (x,y) coordinate
Expand All @@ -23,11 +12,11 @@ interface CoordinateData {
* @param coord.direction {number} Up or down letter-checking direction
* - `-1` for "up" going to (0,0)
* - `1` for "down" going to (N,N) from the `coord`
* @param fullData {CharacterArray} 2D array of strings consisting of letters per item
* @param fullData {string[][]} 2D array of strings consisting of letters per item
* @param WORD {string} Word to find
* @returns {boolean} Flag indicating the existence of the `WORD`
*/
export const checkVertical = (coord: Coordinate, fullData: CharacterArray, WORD: string): boolean => {
export const checkVertical = (coord: Coordinate, fullData: string[][], WORD: string): boolean => {
let yIndex = coord.y
let subWord = ''

Expand Down Expand Up @@ -79,11 +68,11 @@ export const checkHorizontal = (coord: Coordinate, rowData: string[], WORD: stri
* @typedef param {CoordinateData} Direction and data object
* @param param.xDirection {number} `-1` for "left" going to (0,0) or `1` for "right" going to (N,N) from the `coord`
* @param param.yDirection {number} `-1` for "up" going to (0,0) or `1` for "down" going to (N,N) from the `coord`
* @param param.data {CharacterArray} 2D string array containing the input text
* @param param.data {string[][]} 2D string array containing the input text
* @returns {boolean} Flag indicating the existence of the `WORD`
* @param WORD {string} Word to find
*/
export const checkDiagonal = (coord: CoordinateBase, param: CoordinateData, WORD: string): boolean => {
export const checkDiagonal = (coord: Point, param: CoordinateData, WORD: string): boolean => {
let xIndex = coord.x
let yIndex = coord.y
let subWord = ''
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-04/lib/wordCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { checkVertical, checkHorizontal, checkDiagonal } from './wordCheckerUtil

/**
* Counts the number of occurence of the `WORD` from a set of input
* @param data {CharacterArray} 2D string array containing the input text
* @param data {string[][]} 2D string array containing the input text
* @param WORD_TO_FIND {string} Word to find
* @returns {number} Number of `WORD`s
*/
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-04/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { directory, readFile } from '@/utils/file.js'
import { directory, readFile } from '@/aoc/file/utils.js'
import { wordCount } from './lib/wordCount.js'
import { countMASword } from './lib/xmasCount.js'

Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-05/lib/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { directory, readFile } from '@/utils/file.js'
import { uniformArrayElements } from '@/utils/arrays.js'
import { directory, readFile } from '@/aoc/file/utils.js'
import { uniformArrayElements } from '@/aoc/array/utils.js'

export type Rules = Record<number, number[]>

Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-05/lib/fixOrderingUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isOrderedReport } from './orderedUpdates.js'
import { arrayMiddleIndex, uniformArrayElements } from '@/utils/arrays.js'
import { arrayMiddleIndex, uniformArrayElements } from '@/aoc/array/utils.js'

import type { Rules } from './fileReader.js'
import type { QuizData } from './fileReader.js'
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-05/lib/orderedUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { QuizData, Rules } from './fileReader.js'
import { uniformArrayElements, arrayMiddleIndex } from '@/utils/arrays.js'
import { arrayMiddleIndex, uniformArrayElements } from '@/aoc/array/utils.js'

/**
* Checks if an "update" list is correct according to defined "rules"
Expand Down
28 changes: 17 additions & 11 deletions src/2024/2024-12-06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ Visit the Advent of Code website for more information on this puzzle at:

## Code

1. **grid.ts**
- `Grid` - class that has a 2D array object containing paths and obstacles in which a `Guard` runs.
### `grid.ts`

**`Grid`** class
- class that has a 2D array object containing paths and obstacles in which a `Guard` runs.
- Has functions and methods for managing a `Grid` object

2. **guard.ts**
- `Guard` - class representing an object that can walk in the `Grid` map.
### `guard.ts`

**`Guard`** class
- class representing an object that can walk in the `Grid` map.
- Have methods for moving around in the `Grid`.

3. **guardController.ts**
- `guardController()` - Runs the `Guard` on the `Grid`, counting distinct positions/steps.
### `guardController.ts`

- **`guardController()`** - Runs the `Guard` on the `Grid`, counting distinct positions/steps.

### `guardControllerLoop.ts`
- **`gridHasInfiniteLoop()`**
- Runs the `Guard` on a `Grid` with obstacles, checking the placement of paths and obstacles that will make the `Guard` walk in an infinite loop
- **`findObstructionPositions()`** - Counts the number of positions in the `Grid` in which inserting one (1) obstruction symbol # will cause the `Guard` to walk in an infinite loop.
- > _**WARNING:** This is a very slow, unoptimized program execution that takes about ~3 mins to complete using the actual AoC input text._

5. **guardControllerLoop.ts**
- `gridHasInfiniteLoop()`
- Runs the `Guard` on a `Grid` with obstacles, checking the placement of paths and obstacles that will make the `Guard` walk in an infinite loop
- `findObstructionPositions()` - Counts the number of positions in the `Grid` in which inserting one (1) obstruction symbol # will cause the `Guard` to walk in an infinite loop.
- > _**WARNING:** This is a very slow, unoptimized program execution that takes about ~3 mins to complete using the actual AoC input text._
1 change: 0 additions & 1 deletion src/2024/2024-12-06/lib/grid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GuardStatus, GuardDirection } from './guard.types.js'
import type { GuardState } from './guard.types.js'


/**
* @class Grid
* @description Object containing a 2D array of strings and other data in which a `Guard` object runs. Each item represents an open path or obstacle.
Expand Down
10 changes: 0 additions & 10 deletions src/2024/2024-12-06/lib/grid.types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/2024/2024-12-06/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { guardController } from './lib/guardController.js'
import { findObstructionPositions } from './lib/guardControllerLoop.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-06/sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { test, expect } from 'vitest'

import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { guardController } from './lib/guardController.js'
import { findObstructionPositions } from './lib/guardControllerLoop.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-07/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { totalCalibrationResult } from './lib/totalCalibration.js'
import { totalCalibrationConcat } from './lib/totalCalibrationConcat.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-07/sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { test, expect } from 'vitest'

import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { totalCalibrationResult } from './lib/totalCalibration.js'
import { totalCalibrationConcat } from './lib/totalCalibrationConcat.js'
Expand Down
8 changes: 4 additions & 4 deletions src/2024/2024-12-08/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Visit the Advent of Code website for more information on this puzzle at:

### `GridAntinodes.ts`

- `GridAntiNodes` class - Object that tracks and manages `Antennas` and `Antinodes` in a 2D grid array
- **`GridAntiNodes`** class - Object that tracks and manages `Antennas` and `Antinodes` in a 2D grid array

### `uniqueAntinodes.ts`

- `uniqueAntinodes()` - Counts the unique locations in the grid that contains an antinode
- **`countAntinodes()`** - Counts the unique locations in the grid that contains an antinode

### `allAntinodes.ts`

- `getAntinodesInPath()` - Finds all `Antinode` coordinates along a path within a 2D array (grid) given a `Point`, increment steps and a +/- direction
- **`getAntinodesInPath()`** - Finds all `Antinode` coordinates along a path within a 2D array (grid) given a `Point`, increment steps and a +/- direction

- `countAllAntinodes()` - Counts the unique locations in the grid that contains all locations of antinodes along a path
- **`countAllAntinodes()`** - Counts the unique locations in the grid that contains all locations of antinodes along a path
3 changes: 2 additions & 1 deletion src/2024/2024-12-08/lib/allAntinodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Antenna, Point } from './types.js'
import type { Antenna } from './types.js'
import type { Point } from '@/aoc/point/types.js'
import { GridAntiNodes } from './GridAntinodes.js'

/**
Expand Down
5 changes: 1 addition & 4 deletions src/2024/2024-12-08/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export interface Point {
x: number;
y: number;
}
import type { Point } from '@/aoc/point/types.js'

export interface Antenna extends Point {
frequency: string;
Expand Down
3 changes: 2 additions & 1 deletion src/2024/2024-12-08/lib/uniqueAntinodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Antenna, Point } from './types.js'
import type { Antenna } from './types.js'
import type { Point } from '@/aoc/point/types.js'
import { GridAntiNodes } from './GridAntinodes.js'

/**
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-08/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { countAntinodes } from './lib/uniqueAntinodes.js'
import { countAllAntinodes } from './lib/allAntinodes.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-08/sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { test, expect } from 'vitest'

import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { countAntinodes } from './lib/uniqueAntinodes.js'
import { countAllAntinodes } from './lib/allAntinodes.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-09/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'

import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { CompactDisk } from './lib/compact.js'
import { WholeDisk } from './lib/whole.js'
Expand Down
4 changes: 2 additions & 2 deletions src/2024/2024-12-09/sample.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { test, expect } from 'vitest'

import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
import { directory } from '@/utils/file.js'
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/aoc/file/aocfile.js'
import { directory } from '@/aoc/file/utils.js'

import { CompactDisk } from './lib/compact.js'
import { WholeDisk } from './lib/whole.js'
Expand Down
2 changes: 1 addition & 1 deletion src/2024/2024-12-10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ Visit the Advent of Code website for more information on this puzzle at:

### `scoresRatings.ts`

- `countTrailScores()`
- **`countTrailScores()`**
- Finds valid hiking trails (trailheads) from a point coordinate in a 2D array starting with `0` and ending in `9` symbol and calculates the **scores** for each trailhead.
- Calculates the trailhead **ratings** instead of the trailhead scores if provided with the optional `{ isRating: true }` parameter. Defaults to `false`
13 changes: 5 additions & 8 deletions src/2024/2024-12-10/lib/scoresRatings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { Point } from '../../2024-12-08/lib/types.js'
import type { InputOptions, PointSteps, PointDirection, TrailScores } from './types.js'

import {
findValidSteps,
findZeroCoordinatePositions,
getCoordinateSymbol
} from './utils.js'
import type { Point, PointDirection, PointSteps } from '@/aoc/point/types.js'
import type { InputOptions, TrailScores } from './types.js'

import { getCoordinateSymbol } from '@/aoc/grid/utils.js'
import { findValidSteps, findZeroCoordinatePositions } from './utils.js'

// List of trailhead scores
const scores: Record<string, string[]> = {}
Expand Down
41 changes: 0 additions & 41 deletions src/2024/2024-12-10/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,3 @@
/**
* Point (y,x) coordinate in a 2D array with direction
* @type {Object} PointSteps
* @property {number} x - x-coordinate of the point
* @property {number} y - y-coordinate of the point
* @property {Object} direction - Direction vector
* @property {number} direction.x - Left/right x-direction denoted by `+1` or `-1`
* @property {number} direction.y - Up/down y-direction denoted by `-1` or `+1`
*/
export type PointSteps = {
x: number;
y: number;
direction: {
x: number;
y: number;
}
}

/**
* Point (y,x) coordinate in a 2D array with a list of valid coordinates from its location.
* @type {Object} PointDirection
* @property {number} x - x-coordinate of the point
* @property {number} y - y-coordinate of the point
* @property {PointSteps[]} validSteps - List of valid up/down/left/right coordinates from the (y,x) position.
*/
export type PointDirection = {
x: number;
y: number;
validSteps: PointSteps[]
}

/**
* Represents a "(y,x)" coordinate in string and its value from a 2D array.
* @param {string} coordinate - String version of an "(y,x)" coordinate
* @param {string | number} symbol - Number or character in a 2D arary denoted by the (y,x) coordinate
*/
export type GridCoordinateSymbol = {
coordinate: string;
symbol: string | number;
}

/**
* Data returned by the trailhead scores counting function.
* @param {Record<string, string[]>} scores - Object list of trailhead scores per (y,x) coordinate that starts with a unique `0` and ends with a `9`
Expand Down
Loading
Loading