-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utilities for reading puzzle input
- Loading branch information
1 parent
cea351b
commit 0f0e3b1
Showing
9 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__pycache__ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extraPaths": ["utils/python"], | ||
"exclude": [ | ||
"**/__pycache__", | ||
"node_modules" | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |