Compare commits
6 Commits
613cc75237
...
db1d21df04
Author | SHA1 | Date |
---|---|---|
Daylin Morgan | db1d21df04 | |
Daylin Morgan | 193a6315b6 | |
Daylin Morgan | 520d30d90e | |
Daylin Morgan | 9d8b5e2d2e | |
Daylin Morgan | 19f37ece40 | |
Daylin Morgan | 3759966ea6 |
|
@ -0,0 +1,22 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Daylin Morgan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
29
README.md
29
README.md
|
@ -1,3 +1,30 @@
|
|||
# Advent of Code 2023
|
||||
|
||||
![tree](./assets/tree.svg)
|
||||
<div align="center">
|
||||
<img
|
||||
src="./assets/tree.svg"
|
||||
alt="christmas tree"
|
||||
width="600"
|
||||
>
|
||||
</div>
|
||||
|
||||
To generate a new day and fetch the input's for a puzzle use the below command.
|
||||
Note: the day and year are inferred from today's date if not specified.
|
||||
|
||||
```sh
|
||||
nim r ./tools/aoc.nim # -d:1 -y:2023
|
||||
```
|
||||
|
||||
To quickly view solutions from your terminal use my soft-serve instance!
|
||||
|
||||
For an individual solution:
|
||||
|
||||
```sh
|
||||
ssh -p 23231 git.dayl.in repo blob advent-of-code-2023 solutions/day01/solution.nim
|
||||
```
|
||||
|
||||
To interactively explore repo:
|
||||
|
||||
```sh
|
||||
ssh -p 23231 git.dayl.in -t advent-of-code-2023
|
||||
```
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
|
@ -0,0 +1,56 @@
|
|||
import std/[strutils]
|
||||
|
||||
const example* = slurp("example.txt").strip()
|
||||
const example2* = slurp("example2.txt").strip()
|
||||
const input* = slurp("input.txt").strip()
|
||||
|
||||
proc parseInput(input: string): seq[string] =
|
||||
input.split("\n")
|
||||
|
||||
proc partOne*(input: string): int =
|
||||
let lines = parseInput(input)
|
||||
for line in lines:
|
||||
var digits: seq[char]
|
||||
for c in line:
|
||||
case c
|
||||
of '0'..'9':
|
||||
digits.add c
|
||||
else: discard
|
||||
result += parseInt(digits[0] & digits[^1])
|
||||
|
||||
type
|
||||
Number = enum
|
||||
zero = 0,
|
||||
one, two, three, four, five, six, seven, eight, nine
|
||||
|
||||
proc partTwo*(input: string): int =
|
||||
let lines = parseInput(input)
|
||||
for line in lines:
|
||||
var digits: seq[char]
|
||||
var i: int
|
||||
while i < line.len:
|
||||
let c = line[i]
|
||||
case c
|
||||
of '0'..'9':
|
||||
digits.add c
|
||||
inc i
|
||||
else:
|
||||
for number in Number.low..Number.high:
|
||||
if line[i..^1].startsWith($number):
|
||||
digits.add $number.ord
|
||||
# some numbers share chars
|
||||
# let's jump back an extra index to catch them
|
||||
i += len($number)-2
|
||||
break
|
||||
inc i
|
||||
result += parseInt(digits[0] & digits[^1])
|
||||
|
||||
when isMainModule:
|
||||
import std/unittest
|
||||
suite "day 1":
|
||||
test "part one":
|
||||
check partOne(example) == 142
|
||||
check partOne(input) == 54597
|
||||
test "part two":
|
||||
check partTwo(example2) == 281
|
||||
check partTwo(input) == 54504
|
|
@ -30,27 +30,18 @@ proc partOne*(input: string): int = 0
|
|||
proc partTwo*(input: string): int = 0
|
||||
|
||||
when isMainModule:
|
||||
echo partOne(example)
|
||||
echo partOne(input)
|
||||
echo partTwo(example)
|
||||
echo partTwo(input)
|
||||
"""
|
||||
let test = fmt"""
|
||||
import std/unittest
|
||||
import ./solution
|
||||
|
||||
suite "day {day}":
|
||||
test "part one":
|
||||
check partOne(example) == 0
|
||||
check partOne(input) == 0
|
||||
test "part two":
|
||||
check partTwo(example) == 0
|
||||
check partTwo(input) == 0
|
||||
import std/unittest
|
||||
|
||||
suite "day {day}":
|
||||
test "part one":
|
||||
check partOne(example) == 0
|
||||
check partOne(input) == 0
|
||||
test "part two":
|
||||
check partTwo(example) == 0
|
||||
check partTwo(input) == 0
|
||||
"""
|
||||
let d = fmt"solutions/day{day:0>2}"
|
||||
writeFile(d / "solution.nim", solution)
|
||||
writeFile(d / "test.nim", test)
|
||||
|
||||
proc newDay(year, day: int) =
|
||||
let d = fmt"solutions/day{day:0>2}"
|
||||
|
@ -89,7 +80,7 @@ options:
|
|||
let today = now()
|
||||
var
|
||||
year = today.year
|
||||
day = today.weekday.ord
|
||||
day = parseInt(today.format("d"))
|
||||
|
||||
for kind, key, val in getopt():
|
||||
case kind
|
||||
|
|
Loading…
Reference in New Issue