Some more Factor solutions for the first 3 days (so far) from soweli Niko, on Codeberg.
Factor
: get-input ( -- corrupted-input )
"aoc-2024.03" "input.txt" vocab-file-path utf8 file-contents ;
: get-muls ( corrupted-input -- instructions )
R/ mul\(\d+,\d+\)/ all-matching-subseqs ;
: process-mul ( instruction -- n )
R/ \d+/ all-matching-subseqs
[ string>number ] map-product ;
: solve ( corrupted-input -- n )
get-muls [ process-mul ] map-sum ;
: part1 ( -- n )
get-input solve ;
: part2 ( -- n )
get-input
R/ don't\(\)(.|\n)*?do\(\)/ split concat
R/ don't\(\)(.|\n)*/ "" re-replace
solve ;
Factor
: get-input ( -- reports )
"aoc-2024.02" "input.txt" vocab-file-lines
[ split-words [ string>number ] map ] map ;
: slanted? ( report -- ? )
{ [ [ > ] monotonic? ] [ [ < ] monotonic? ] } || ;
: gradual? ( report -- ? )
[ - abs 1 3 between? ] monotonic? ;
: safe? ( report -- ? )
{ [ slanted? ] [ gradual? ] } && ;
: part1 ( -- n )
get-input [ safe? ] count ;
: fuzzy-reports ( report -- reports )
dup length <iota> [ remove-nth-of ] with map ;
: tolerable? ( report -- ? )
{ [ safe? ] [ fuzzy-reports [ safe? ] any? ] } || ;
: part2 ( -- n )
get-input [ tolerable? ] count ;
Factor
: get-input ( -- left-list right-list )
"aoc-2024.01" "input.txt" vocab-file-lines
[ split-words harvest ] map unzip
[ [ string>number ] map ] bi@ ;
: part1 ( -- n )
get-input
[ sort ] bi@
[ - abs ] 2map-sum ;
: part2 ( -- n )
get-input
histogram
'[ dup _ at 0 or * ] map-sum ;
Day 3
spoiler
: get-input ( -- corrupted-input )
"aoc-2024.03" "input.txt" vocab-file-path utf8 file-contents ;
: get-muls ( corrupted-input -- instructions )
R/ mul\(\d+,\d+\)/ all-matching-subseqs ;
: process-mul ( instruction -- n )
R/ \d+/ all-matching-subseqs
[ string>number ] map-product ;
: solve ( corrupted-input -- n )
get-muls [ process-mul ] map-sum ;
: part1 ( -- n )
get-input solve ;
: part2 ( -- n )
get-input
R/ don't\(\)(.|\n)*?do\(\)/ split concat
R/ don't\(\)(.|\n)*/ "" re-replace
solve ;
Image:
spoiler
Day 2:
spoiler
: get-input ( -- reports )
"aoc-2024.02" "input.txt" vocab-file-lines
[ split-words [ string>number ] map ] map ;
: slanted? ( report -- ? )
{ [ [ > ] monotonic? ] [ [ < ] monotonic? ] } || ;
: gradual? ( report -- ? )
[ - abs 1 3 between? ] monotonic? ;
: safe? ( report -- ? )
{ [ slanted? ] [ gradual? ] } && ;
: part1 ( -- n )
get-input [ safe? ] count ;
: fuzzy-reports ( report -- reports )
dup length <iota> [ remove-nth-of ] with map ;
: tolerable? ( report -- ? )
{ [ safe? ] [ fuzzy-reports [ safe? ] any? ] } || ;
: part2 ( -- n )
get-input [ tolerable? ] count ;
Image:
spoiler
Sure, but nox is the closer counterpart for in-venv-task definitions. List "sessions" with -l
, pick specific sessions to run with -s
.
import nox
from nox.sessions import Session
nox.options.reuse_existing_virtualenvs = True
APP_NAME = 'logging_strict'
@nox.session(python='3.12')
def mypy(session: Session):
"""Static type checker (in strict mode)"""
session.install('-U', 'mypy', '.')
session.run('mypy', '-p', APP_NAME, *session.posargs)
Unfortunately it doesn't currently do any parallel runs, but if anyone wants to track/encourage/contribute in that regard, see nox#544.
As someone's new comments just brought me back to this post, I'll point out that these days there's another good option: uv run.
No, I don't use GHA locally, but the actions are defined to run the same things that I do run locally (e.g. invoke nox
). I try to keep the GHA-exclusive boilerplate to a minimum. Steps can be like:
- name: fetch code
uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
allow-prereleases: true
python-version: |
3.13
3.12
3.11
3.10
3.9
3.8
3.7
- run: pipx install nox
- name: run ward tests in nox environment
run: nox -s test test_without_toml combine_coverage --force-color
env:
PYTHONIOENCODING: utf-8
- name: upload coverage data
uses: codecov/codecov-action@v4
with:
files: ./coverage.json
token: ${{ secrets.CODECOV_TOKEN }}
Sometimes if I want a higher level interface to tasks that run nox
or other things locally, I use taskipy
to define them in my pyproject.toml
, like:
[tool.taskipy.tasks]
fmt = "nox -s fmt"
lock = "nox -s lock"
test = "nox -s test test_without_toml typecheck -p 3.12"
docs = "nox -s render_readme render_api_docs"
Have you had a good look at Factor? FWIW I've got at least the first 3 days with it up here.