Compare commits
2 commits
880d6c5264
...
7bb4b908bd
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bb4b908bd | |||
| d30947fdec |
6 changed files with 55 additions and 40 deletions
|
|
@ -3,8 +3,6 @@
|
||||||
(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) -)
|
||||||
|
|
@ -55,6 +53,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 print (map part1 (command-line-arguments)))
|
(for-each (lambda (s) (display s) (newline)) (map part1 (cdr (command-line))))
|
||||||
(for-each print (map part2 (command-line-arguments)))
|
(for-each (lambda (s) (display s) (newline)) (map part2 (cdr (command-line))))
|
||||||
|
|
||||||
|
|
|
||||||
24
2/main.scm
24
2/main.scm
|
|
@ -5,7 +5,20 @@
|
||||||
(scheme process-context)
|
(scheme process-context)
|
||||||
(scheme write))
|
(scheme write))
|
||||||
|
|
||||||
(include "../common.scm")
|
(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)))))))
|
||||||
|
|
||||||
(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))
|
||||||
|
|
@ -71,10 +84,5 @@
|
||||||
(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)) (command-line-arguments))
|
(for-each (lambda (file) (part file sum-invalid-in-range-part1)) (cdr (command-line)))
|
||||||
(for-each (lambda (file) (part file sum-invalid-in-range-part2)) (command-line-arguments))
|
(for-each (lambda (file) (part file sum-invalid-in-range-part2)) (cdr (command-line)))
|
||||||
|
|
||||||
|
|
||||||
;; (print (number->string (sum-invalid-in-range 11 22)))
|
|
||||||
|
|
||||||
#;(for-each print (map part2 (command-line-arguments)))
|
|
||||||
|
|
|
||||||
2
3/GNUmakefile
Normal file
2
3/GNUmakefile
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# -*- makefile-gmake -*-
|
||||||
|
include ../gnu.mk
|
||||||
31
3/main.scm
Normal file
31
3/main.scm
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
(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)))
|
||||||
4
3/makefile
Normal file
4
3/makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# -*- makefile-bsdmake -*-
|
||||||
|
# Disable Skint because no numerical tower
|
||||||
|
SCHEMES=chibi chicken gauche guile racket sagittarius
|
||||||
|
.include "../common.mk"
|
||||||
28
common.scm
28
common.scm
|
|
@ -1,28 +0,0 @@
|
||||||
;; 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