Compare commits
No commits in common. "7bb4b908bd91db1ef8ee0e8074b423cb210822fe" and "880d6c526436502686e03f8480e22588ed545d2b" have entirely different histories.
7bb4b908bd
...
880d6c5264
6 changed files with 40 additions and 55 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
(scheme process-context)
|
(scheme process-context)
|
||||||
(scheme write))
|
(scheme write))
|
||||||
|
|
||||||
|
(include "../common.scm")
|
||||||
|
|
||||||
(define (direction->arithmetic direction)
|
(define (direction->arithmetic direction)
|
||||||
(cond
|
(cond
|
||||||
((char=? direction #\L) -)
|
((char=? direction #\L) -)
|
||||||
|
|
@ -53,6 +55,6 @@
|
||||||
(+ zeros add-zeros (if (and (zero? dial) (negative? dial+-distance)) -1 0))
|
(+ zeros add-zeros (if (and (zero? dial) (negative? dial+-distance)) -1 0))
|
||||||
(read-line)))))))))
|
(read-line)))))))))
|
||||||
|
|
||||||
(for-each (lambda (s) (display s) (newline)) (map part1 (cdr (command-line))))
|
(for-each print (map part1 (command-line-arguments)))
|
||||||
(for-each (lambda (s) (display s) (newline)) (map part2 (cdr (command-line))))
|
(for-each print (map part2 (command-line-arguments)))
|
||||||
|
|
||||||
|
|
|
||||||
24
2/main.scm
24
2/main.scm
|
|
@ -5,20 +5,7 @@
|
||||||
(scheme process-context)
|
(scheme process-context)
|
||||||
(scheme write))
|
(scheme write))
|
||||||
|
|
||||||
(define (string-split str delim?)
|
(include "../common.scm")
|
||||||
(let ((in (open-input-string str)))
|
|
||||||
(let loop ((acc '())
|
|
||||||
(out (open-output-string)))
|
|
||||||
(let ((c (read-char in)))
|
|
||||||
(cond
|
|
||||||
((eof-object? c)
|
|
||||||
(reverse (cons (get-output-string out) acc)))
|
|
||||||
((delim? c)
|
|
||||||
(loop (cons (get-output-string out) acc)
|
|
||||||
(open-output-string)))
|
|
||||||
(else
|
|
||||||
(write-char c out)
|
|
||||||
(loop acc out)))))))
|
|
||||||
|
|
||||||
(define powers-of-ten
|
(define powers-of-ten
|
||||||
#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000))
|
#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000))
|
||||||
|
|
@ -84,5 +71,10 @@
|
||||||
(newline))
|
(newline))
|
||||||
(loop (cdr ranges) (+ sum (sumfn (string->number (caar ranges)) (string->number (cadar ranges)))))))))
|
(loop (cdr ranges) (+ sum (sumfn (string->number (caar ranges)) (string->number (cadar ranges)))))))))
|
||||||
|
|
||||||
(for-each (lambda (file) (part file sum-invalid-in-range-part1)) (cdr (command-line)))
|
(for-each (lambda (file) (part file sum-invalid-in-range-part1)) (command-line-arguments))
|
||||||
(for-each (lambda (file) (part file sum-invalid-in-range-part2)) (cdr (command-line)))
|
(for-each (lambda (file) (part file sum-invalid-in-range-part2)) (command-line-arguments))
|
||||||
|
|
||||||
|
|
||||||
|
;; (print (number->string (sum-invalid-in-range 11 22)))
|
||||||
|
|
||||||
|
#;(for-each print (map part2 (command-line-arguments)))
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
# -*- makefile-gmake -*-
|
|
||||||
include ../gnu.mk
|
|
||||||
31
3/main.scm
31
3/main.scm
|
|
@ -1,31 +0,0 @@
|
||||||
(import (scheme base)
|
|
||||||
(scheme char)
|
|
||||||
(scheme cxr)
|
|
||||||
(scheme file)
|
|
||||||
(scheme process-context)
|
|
||||||
(scheme write))
|
|
||||||
|
|
||||||
(define (max-joltage line batteries)
|
|
||||||
(let ((lst (map digit-value (string->list line))))
|
|
||||||
(let loop ((req-tail-length (- batteries 1))
|
|
||||||
(lst lst)
|
|
||||||
(sum 0))
|
|
||||||
(if (negative? req-tail-length)
|
|
||||||
sum
|
|
||||||
;; this is O^one million at least
|
|
||||||
(let ((m (memq (apply max (reverse (list-tail (reverse lst) req-tail-length))) lst)))
|
|
||||||
(loop (- req-tail-length 1) (cdr m) (+ (* sum 10) (car m))))))))
|
|
||||||
|
|
||||||
(define (part file batteries)
|
|
||||||
(with-input-from-file file
|
|
||||||
(lambda ()
|
|
||||||
(let loop ((line (read-line))
|
|
||||||
(sum 0))
|
|
||||||
(if (eof-object? line)
|
|
||||||
(begin
|
|
||||||
(display (number->string sum))
|
|
||||||
(newline))
|
|
||||||
(loop (read-line) (+ sum (max-joltage line batteries))))))))
|
|
||||||
|
|
||||||
(for-each (lambda (file) (part file 2)) (cdr (command-line)))
|
|
||||||
(for-each (lambda (file) (part file 12)) (cdr (command-line)))
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
# -*- makefile-bsdmake -*-
|
|
||||||
# Disable Skint because no numerical tower
|
|
||||||
SCHEMES=chibi chicken gauche guile racket sagittarius
|
|
||||||
.include "../common.mk"
|
|
||||||
28
common.scm
Normal file
28
common.scm
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
;; Shitty print for convenience
|
||||||
|
(define (print . args)
|
||||||
|
(display (apply string-append args))
|
||||||
|
(newline))
|
||||||
|
|
||||||
|
;; Chicken command-line-arguments for no reason at all
|
||||||
|
(define (command-line-arguments)
|
||||||
|
(cdr (command-line)))
|
||||||
|
|
||||||
|
;; Guile string-split
|
||||||
|
(define (string-split str delim?)
|
||||||
|
(let ((in (open-input-string str)))
|
||||||
|
(let loop ((acc '())
|
||||||
|
(out (open-output-string)))
|
||||||
|
(let ((c (read-char in)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c)
|
||||||
|
(reverse (cons (get-output-string out) acc)))
|
||||||
|
((delim? c)
|
||||||
|
(loop (cons (get-output-string out) acc)
|
||||||
|
(open-output-string)))
|
||||||
|
(else
|
||||||
|
(write-char c out)
|
||||||
|
(loop acc out)))))))
|
||||||
|
|
||||||
|
;; Reverse for-each, for convenience
|
||||||
|
(define (xfor-each1 lst fn)
|
||||||
|
(for-each fn lst))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue