diff options
| -rw-r--r-- | README.md | 12 | ||||
| -rwxr-xr-x | main | 64 | ||||
| -rwxr-xr-x | main.lua | 29 | ||||
| -rwxr-xr-x | main.scm | 35 |
4 files changed, 73 insertions, 67 deletions
@@ -1,16 +1,22 @@ # Advent of code 2024 -These are my solutions for Advent of Code in 2024. This year I'm using Lua as the programming language. To launch the program for a given day, run +These are my solutions for Advent of Code in 2024. This year I'm using Guile +Scheme as the programming language. To launch the program for a given day, run ```sh ./main <<ID>> [<<FILE>>] ``` -where `<<ID>>` is the day number followed by `a` for the first part or `b` for the second part. If the file is not specified, it defaults to the number of the day with two digits and extension `.in`, i.e. `01.in`. One can use the name of a file that doesn't exist, e.g. `-`, to read from standard input. +where `<<ID>>` is the day number followed by `a` for the first part or `b` for +the second part. If the file is not specified, it defaults to the number of the +day with two digits and extension `.in`, i.e. `01.in`. To read from standard +input, use `-` as the file name. Example: ```sh -./main 01b input +./main 05b - ``` +Some of the first days are written in Lua rather than Guile Scheme. To run +them, simply replace `main` with `main.lua`. @@ -1,29 +1,35 @@ -#!/usr/bin/lua - -if #arg < 1 or #arg > 2 then - error ("Syntax: %s <DAY><a|b> [inputfile]"):format(arg[0]) - os.exit(1) -end - -local num, letter = arg[1]:match("^(%d+)([ab])$") -if not num then - error ('Expected <DAY><a|b> as first argument (e.g. "01b"), got: %s'):format(arg[1]) - os.exit(1) -end -num = tonumber(num) - -local script = ("%02d.lua"):format(num) -local infile = arg[2] or ("%02d.in"):format(num) -local input_stream = io.open(infile) -if input_stream then - io.input(input_stream) -else - print "Reading from standard input." -end - -local parsed = loadfile(script)() -if letter == 'a' then - parsed:part1() -else - parsed:part2() -end +#!/usr/bin/env guile +!# + +(use-modules (ice-9 format) (ice-9 regex)) + +(define args (program-arguments)) + +(unless (<= 2 (length args) 3) + (error (format #f "Syntax: ~a <DAY><a|b> [INFILE]\n" (car args))) + (exit 1)) + +(define mtch (string-match "^([0-9]+)(a|b)$" (list-ref args 1))) + +(unless mtch + (error (format #f "Expected day and part (like \"03a\" or \"02b\"), got: ~a" + (list-ref args 1)))) + +(define day (format #f "~2,'0d" (string->number (match:substring mtch 1)))) +(define part (if (string= (match:substring mtch 2) "a") 'part1 'part2)) + +(set! %load-path (cons "." %load-path)) + +(define day-module + (resolve-module (list (string->symbol (string-append "day" day))))) +(define func (module-ref day-module part)) + +(define input-file + (cond ((= (length args) 2) (string-append day ".in")) + ((string= (list-ref args 2) "-") #f) + (else (list-ref args 2)))) + +(display (if input-file + (call-with-input-file input-file func) + (func (current-input-port)))) +(newline) diff --git a/main.lua b/main.lua new file mode 100755 index 0000000..38189ee --- /dev/null +++ b/main.lua @@ -0,0 +1,29 @@ +#!/usr/bin/lua + +if #arg < 1 or #arg > 2 then + error ("Syntax: %s <DAY><a|b> [inputfile]"):format(arg[0]) + os.exit(1) +end + +local num, letter = arg[1]:match("^(%d+)([ab])$") +if not num then + error ('Expected <DAY><a|b> as first argument (e.g. "01b"), got: %s'):format(arg[1]) + os.exit(1) +end +num = tonumber(num) + +local script = ("%02d.lua"):format(num) +local infile = arg[2] or ("%02d.in"):format(num) +local input_stream = io.open(infile) +if input_stream then + io.input(input_stream) +else + print "Reading from standard input." +end + +local parsed = loadfile(script)() +if letter == 'a' then + parsed:part1() +else + parsed:part2() +end diff --git a/main.scm b/main.scm deleted file mode 100755 index 39f8da9..0000000 --- a/main.scm +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env guile -!# - -(use-modules (ice-9 format) (ice-9 regex)) - -(define args (program-arguments)) - -(unless (<= 2 (length args) 3) - (error (format #f "Syntax: ~a <DAY><a|b> [INFILE]\n" (car args))) - (exit 1)) - -(define mtch (string-match "^([0-9]+)(a|b)$" (list-ref args 1))) - -(unless mtch - (error (format #f "Expected day and part (like \"03a\" or \"02b\"), got: ~a" - (list-ref args 1)))) - -(define day (format #f "~2,'0d" (string->number (match:substring mtch 1)))) -(define part (if (string= (match:substring mtch 2) "a") 'part1 'part2)) - -(set! %load-path (cons "." %load-path)) - -(define day-module - (resolve-module (list (string->symbol (string-append "day" day))))) -(define func (module-ref day-module part)) - -(define input-file - (cond ((= (length args) 2) (string-append day ".in")) - ((string= (list-ref args 2) "-") #f) - (else (list-ref args 2)))) - -(display (if input-file - (call-with-input-file input-file func) - (func (current-input-port)))) -(newline) |
