Skip to content

Commit

Permalink
Add utilities for reading puzzle input
Browse files Browse the repository at this point in the history
  • Loading branch information
alexg-axis committed Nov 24, 2021
1 parent cea351b commit 0f0e3b1
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
node_modules
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false,
"deno.suggest.imports.hosts": {
"https://deno.land": true
},
"python.pythonPath": "/usr/local/opt/[email protected]/bin/python3"
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/alexg-axis/advent-of-code

go 1.17
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "advent-of-code",
"repository": "[email protected]:alexg-axis/advent-of-code.git"
}
7 changes: 7 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extraPaths": ["utils/python"],
"exclude": [
"**/__pycache__",
"node_modules"
],
}
24 changes: 24 additions & 0 deletions utils/deno/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from "https://deno.land/std/path/mod.ts";

const directory = path.dirname(path.fromFileUrl(Deno.mainModule));

const raw = await Deno.readTextFile(path.resolve(directory, "input.txt"))

/** Input represents a puzzle's input. */
class Input {
constructor(public readonly raw: string) {}

/** The lines of the input. Trims trailing whitespace. */
get lines() {
return this.raw.trimEnd().split("\n")
}

/** The numbers of the input, one number per line. */
get numbers() {
return this.lines.map(x => Number(x))
}
}

/** The puzzle's input */
const input = new Input(raw)
export default input
52 changes: 52 additions & 0 deletions utils/go/parsing/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package parsing

import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strconv"
"strings"
)

// Input represents a puzzle's input.
type Input string

// ReadInput reads the input for a puzzle. Must be called from a file in the same
// directory as the `input.txt` file.
func ReadInput() (Input, error) {
// Assume that the caller is from a file in the same directory as the input
_, filename, _, ok := runtime.Caller(1)
if !ok {
return "", fmt.Errorf("unknown caller")
}

path := filepath.Dir(filename)
data, err := ioutil.ReadFile(filepath.Join(path, "input.txt"))
if err != nil {
return "", err
}

return Input(data), nil
}

// Lines returns the lines of the input. Trims trailing newlines.
func (input Input) Lines() []string {
return strings.Split(strings.TrimSuffix(string(input), "\n"), "\n")
}

// Ints returns the integers found in the input - one integer per line.
func (input Input) Ints() ([]int, error) {
lines := input.Lines()
values := make([]int, len(lines))

for i, line := range lines {
value, err := strconv.ParseInt(line, 10, 32)
if err != nil {
return nil, fmt.Errorf("unable to parse input as ints - %w", err)
}
values[i] = int(value)
}

return values, nil
}
5 changes: 5 additions & 0 deletions utils/python/aoc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Python utilities for Advent of Code."""

__package__ = "aoc"

from aoc.input import input as input
29 changes: 29 additions & 0 deletions utils/python/aoc/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
from os import path

class Input:
"""Puzzle input."""

def __init__(self, raw: str) -> None:
self.__raw = raw

@property
def raw(self) -> str:
"""Raw input."""
return self.__raw

@property
def lines(self) -> list[str]:
"""Lines of the input. Strips whitespace."""
return self.__raw.strip().splitlines()

@property
def ints(self) -> list[int]:
"""Ints found in the input, one int per line."""
return [int(x) for x in self.lines]

input = Input("")

directory = path.dirname(path.abspath(sys.modules["__main__"].__file__ or ""))
with open(path.join(directory, "input.txt"), "r") as file:
input = Input(file.read())

0 comments on commit 0f0e3b1

Please sign in to comment.