Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

1
2018/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*\~

7
2018/day01/day01.dyalog Normal file
View file

@ -0,0 +1,7 @@
⎕IO←0
x←,⍎¨⎕csv'input.txt'
+/x
y←+\(200×x)x
pos←{⍸⍵=y}¨y
y[1⌷{⍵[⍋⍵]}{1⌷⍵}¨(⊃¨1<¨pos)/pos]

23
2018/day01/day01.ss Normal file
View file

@ -0,0 +1,23 @@
(import :gerbil/gambit/ports)
(export main)
(def freqs (with-input-from-file "input.txt" read-all))
(def (repeat n l)
(match n
(0 [])
(else (append l (repeat (1- n) l)))))
(def (main . args)
(displayln (apply + freqs))
(let/cc exit
(foldl
(lambda (x acc)
(let ((new-x (+ x (car acc))))
(if (memq new-x acc)
(begin (displayln new-x) (exit))
(cons new-x acc))))
[0]
(repeat 200 freqs))))

1029
2018/day01/input.txt Normal file

File diff suppressed because it is too large Load diff

4
2018/day02/day02.dyalog Normal file
View file

@ -0,0 +1,4 @@
⎕IO←0
x←⊃⎕nget'input.txt'1
×/+/2 3∘.∊{≢⍵}⌸¨x
⊃x/⍨+/1=x∘.{+/⍺≠⍵}x

39
2018/day02/day02.ss Normal file
View file

@ -0,0 +1,39 @@
(import :gerbil/gambit/ports)
(export main)
(def (unique-counts l)
(def hash (make-hash-table))
(for-each (lambda (x) (hash-update! hash x 1+ 0)) l)
(hash-values hash))
(def (checksum l)
(let ((twice (length (filter (lambda (x) (memq 2 (unique-counts x))) l)))
(thrice (length (filter (lambda (x) (memq 3 (unique-counts x))) l))))
(* twice thrice)))
(def (similarity id1 id2)
(apply +
(map (lambda (x y) (if (eq? x y) 0 1)) id1 id2)))
(def (find-similar id id-list)
(filter (lambda (x) (eq? 1 (similarity id x))) id-list))
(def (intersection id1 id2)
(reverse (foldl (lambda (x y acc) (if (eq? x y) (cons x acc) acc)) [] id1 id2)))
(def (main . args)
(def ids (call-with-input-file "input.txt" (lambda (p) (read-all p read-line))))
(displayln (checksum (map string->list ids)))
(def common
(let/cc exit
(foldl
(lambda (id acc)
(let ((similar (find-similar id acc)))
(if (< 0 (length similar))
(exit (intersection id (car similar)))
(cons id acc))))
[]
(map string->list ids))))
(displayln (list->string common)))

250
2018/day02/input.txt Normal file
View file

@ -0,0 +1,250 @@
ubkfmdjxyzlbgkrotcepvswaqx
uikfmdkuyzlbgerotcepvswaqh
uikfmdpxyzlbgnrotcepvswoeh
nikfmdjxyzlbgnrotqepvswyqh
qgkfmdjxyzlbgnmotcepvswaqh
uikfmdjxyzqbgnrytcepvsbaqh
uikfmdjxyzibgprotcecvswaqh
uikfmajxyzlcgnrojcepvswaqh
uvkfsdjxyzlbgnrotcepvjwaqh
uikfmdjxfzlbggrotcepvswawh
uikfmhjxyzlbgnuotcepvjwaqh
uikfmdjxyzlbuzcotcepvswaqh
uikfmdwxyzlbgnrotcepvfwamh
uikfmdexyzlbgnroecepvswhqh
uikfmdjuyzlbgnrotcqpvswafh
uikfddjxyzvbgnrotceppswaqh
yikfwdjxyzlbgnrotcepvswagh
uiktmdjxyzlbgnrotceposwajh
uikfmdsxyzlbgnroteetvswaqh
uikfpdjxyzlbgnroncepvswuqh
uikfmtjxyzlbgurotcepvswaoh
eikfmdjxyjlbgnrotcepyswaqh
uikfkdjxyzlbgnrotcepvszaqv
uikfrdjxtwlbgnrotcepvswaqh
uikfmdjxyzlbgnrotpepwswahh
kikfmdjxyzlbgnkotcepvswqqh
uikfkduxyzlbgnrotcepvswafh
uikfxhjxyzlbgnrotcegvswaqh
uikfmdjxyzlmgnrotcenvawaqh
uzkfmddxyzlbgnrltcepvswaqh
uikfmdjxyzlbgnrobcepisqaqh
uijfmdjxyzlbgnrotcexvrwaqh
uiwjmdjxyzlbgnrotceprswaqh
uhkqmdjxwzlbgnrotcepvswaqh
uiktmsjxyzwbgnrotcepvswaqh
uikfmdjxyztbgnqotcepvswcqh
uibfmdjxyzlbgnroqcepvswaqx
uwkfmdjxyxlbgnrotcfpvswaqh
uikumdjxyzlbgnrstceposwaqh
uikfzdjxyznhgnrotcepvswaqh
uikuydjxyzlbgnrotqepvswaqh
uikfmdqxyzlbgnrotfefvswaqh
yikfmdjxyzlbrnrqtcepvswaqh
uiifmdjxyzlbenrotcfpvswaqh
uxkjmdjxyzlbgnrotcmpvswaqh
uikgmdjxyzlbgnrotceovlwaqh
uikfmdjxyzvbgzrotcenvswaqh
uiufmdjxyzlbgnrotceposwazh
uiafmdjxyzlignmotcepvswaqh
uikfmdjxyzwbgnsotlepvswaqh
uikjmdjvyzlbgnrotcenvswaqh
uikfmdjxyzlbonroteepvswaqi
uikfmdjxyzldgnroxcepviwaqh
uikvmdjxyzlbgdrotrepvswaqh
uikfmdjyyzwbgnrotcepvzwaqh
uikfmdjxyzzbnnvotcepvswaqh
uikomdjxyzlbgnrotcepvuwhqh
uikfmmjxyzbbgnrotcepvswayh
uikfmdjfezlbgprotcepvswaqh
uzkfmojxmzlbgnrotcepvswaqh
uipfmdjxyzlbgnrotceyuswaqh
uikfmdjxyzlkgnmotcepvswadh
uikfmdjxyzlbgnuctcbpvswaqh
uikfqdjxyzlbogrotcepvswaqh
uikfmdjxyzlfynrotcepvswash
uikfmdjxizzmgnrotcepvswaqh
uhkfmdjxyzlbhnrotcenvswaqh
uipfmdjxyzlbgorotcepfswaqh
uikfmdjxyznbgnrotcepvswfah
uikfmujxyzlbgnrotnepvssaqh
uikfmdjxyzlxgnrotcepvsrwqh
uikfmdjxszlbgnrottvpvswaqh
umkfmdskyzlbgnrotcepvswaqh
uikfmdjxyzlbgorotcwpzswaqh
uikfmdhxyzzbgnzotcepvswaqh
jikfmdjxyzlbgnrotcepveyaqh
uirfmdlxyzlbgnjotcepvswaqh
uikfmdjxyzlbgnrorxepvswazh
uikfmdjxyzltgnrotcepvsxaqi
uikfmdjxyzlbxlrotcepkswaqh
uvkfmdjxyzlbgnrotcopvswxqh
uxkfmdjxkzlbgnrotcepvswqqh
uikfmdjxyzlbqnrotcepvhwrqh
uikfmdjxyzlvgnrolcepvswrqh
urkfmdixyzlbgnrotcepvsjaqh
uikfmdjxymlbsnrotcepvswcqh
uikfmdjxyilbgnrotcepvcwzqh
uikfadjxlzlbgnrotcepvswaql
uikfmdjxuzlbgdrotcgpvswaqh
yikfmdgxyzlbgnrotcepvswarh
uikfmdjxyzlbgorotcepcswaqv
uikkmdjxyzlbvnrotcepvvwaqh
uwzfmdjxyxlbgnrotcfpvswaqh
uikfmdjxyztbgnrotcrtvswaqh
uiufmdjxyzhbgnrotcupvswaqh
uikfzdjcyzlbgnrotcfpvswaqh
uipfmdjxyzlbgnrotavpvswaqh
uikfmajxyzlbgnrotcepbsxaqh
uikfmdjfyzlbgnrotbepvswmqh
gikkmdjxyzlbgnrptcepvswaqh
uikfmdjxqvlbgnrotsepvswaqh
fikfmdjxyzlbgnrotcegvswoqh
idkfmdjxyzlbgnrotcepwswaqh
uikfmdqxyzlbmnrobcepvswaqh
uikfmdjxyzpbgnroicepvsyaqh
uikfmkoxyzlbgnrotcgpvswaqh
unkfmdjxyzlbpnrolcepvswaqh
uikfmdjmyzlbgfrotcupvswaqh
ujkfmdjxynlbgnroteepvswaqh
uikfmljxyzlbgnaotcepvsiaqh
uikfmdjdyzlbgnrotcepvtwaqd
uikfmdjxyzlbgnyotcepimwaqh
uikfmdjxyzfbjnrotcepvxwaqh
eiwfmdjxyzlbgnrdtcepvswaqh
umkhmdjxyzlbgnrotceivswaqh
uikfmdjxyzlbgnrotcwpvswneh
uckfmdjxyzlbknrotcepvswauh
uiofmdjoyzlbgnrbtcepvswaqh
uikfmdbxyzlbgnrotcepaslaqh
uimfmdjxyalbgnrotcepvswaxh
uqkfmdjxyzlbwnrotcepmswaqh
uiyfmdjxyzlbgnrotcepvswxuh
uikfmdjxyzlbgmrotgepvswamh
uikfmdjxyqlbgarozcepvswaqh
uikfmdjxyzabanpotcepvswaqh
uikfmdjgyzlbsnrotcepvswaqj
uikfmdjxyzlbwnrottepvsvaqh
uikfmdjxyzlbbnrotcepvofaqh
uikfudjxyzlbgnustcepvswaqh
cskfmqjxyzlbgnrotcepvswaqh
uiwfmddxyzlbgnrotccpvswaqh
eikpmdjxyzlbgnrotcesvswaqh
uikfmdzxyzlngnrrtcepvswaqh
uikfmdjxyzlbgnrotcepaswtph
uirfmdjxyzlbgnrotcepvswsqe
uikjmdjxqzlbgirotcepvswaqh
uikfmdjxsllbknrotcepvswaqh
uikfmdjxyqlbgcrotcepvswaqu
uikfmdjsymlbgnrotcebvswaqh
uikfmdjxezlbgnroccepviwaqh
uikfmdjiyzjbgnrotcepvswarh
uqkfmdjxyzmbgnrotcepvslaqh
unkfmdjxyzlbinrotceplswaqh
uikfmdjxyzpbgnrjtcedvswaqh
uicfmdjxyzlbgrrotcepvswamh
ukknmdjxyzlbgnqotcepvswaqh
uikfudjxyzlbdnrztcepvswaqh
uikfmdjxyzlbgnrozcepvswawk
uikfmduxyzsbgnrotcepvswauh
uikfmdjxyzljbnrotcenvswaqh
uukfmdjxyzlbgnrotcckvswaqh
uilfldjxyzlbgnrotcdpvswaqh
uckfmdjxyvybgnrotcepvswaqh
qikfmdjxyglbgnrotcepvrwaqh
uikfmhjxyzltgnrotcepvswbqh
uikfmdjxipabgnrotcepvswaqh
uikfmdjxyzlbgnrotceovswanl
uikfmdjxyzlbgirotcapvswahh
uikfucjxyflbgnrotcepvswaqh
pikfmdjxyzpbgnrotcepvswaeh
uikfmdjiyylbgnrotcervswaqh
uikfmdjgyzlbgnrotcaevswaqh
uikvmdjxyzlbunrotcepvswarh
uikfadjuyzpbgnrotcepvswaqh
uikfmdjxyzlbgnrotcepsawaqj
eikfmdjxyflbgnrotcepvswaeh
uisfmdaxyzlbgnrotcepvswlqh
vikfmdjxyzlzgnrotcepvswanh
ukkfmdoxyzlbgnrotcewvswaqh
uikfmdhxyzlbgnrotcrpvbwaqh
uhkfmdjwezlbgnrotcepvswaqh
uikfddjxyzlbgnroteepvpwaqh
uikfmdjxczlbgncotiepvswaqh
uikfvdjxyzlbgnrotcnpvsaaqh
uikfmdjxyzlbgnritcepvlwmqh
uikfmdjxczlcgnrotcecvswaqh
mikfmdjxyzlbgnrotcepvswasi
uifvmdjxyzlbgnrotpepvswaqh
uikzmdjxyzlbgnrotrepvswaqs
uikfmqjqyzlbunrotcepvswaqh
uikfpdyxyzlbgnrotcepvswagh
uikfmdjxyzobgnrotrlpvswaqh
zisdmdjxyzlbgnrotcepvswaqh
uikfmdjxyzlbgnlotiesvswaqh
uikfddixyzlbgnroucepvswaqh
uijfmdrxyzlbgnrotoepvswaqh
uikfmdcxbzlbgnrotcepvpwaqh
uikfmdjxxzlbfnrotcecvswaqh
upkfmdjxyzmtgnrotcepvswaqh
uikfmdrxyzlbgnrjtcepvswaqp
uizfmdjxyzlbsnrotcepviwaqh
uidfmdjxyslbgnrotcxpvswaqh
uikfmdjxyzlbgnrovyepvsdaqh
uiafmdjxyzlbgnrxtcepvsdaqh
ugkfmdjxyzlbgnrodcepvswawh
pikfmtjxyzhbgnrotcepvswaqh
uikfmdjxyzlfgnvotcepvswtqh
uikbmdjxyzlbgerotcepvswaqm
uikfmdjxyhlbdnrotcepvswaqy
uikfgdjxyzlbgnhotcepvswdqh
uikfmdpxyzlbgnrotcepvscanh
uikfmdjxyzsbgnretcepvswaqn
uikfddjxyzlrgnrotcepvsbaqh
uikfmdjxyzlbgnrotcqnrswaqh
uhkfmejxyzlbgnrotvepvswaqh
uikimdjxyzlngnrotceprswaqh
uikfmdjxyzwbunrotiepvswaqh
rikfmdjxyzlbgnrotcypvssaqh
uikfmdjxyzlbdnrotcrpvswsqh
uekfmdjxkzlbznrotcepvswaqh
uikfmdjxyglbgvrotcepvswaqv
uikfmcjxyzlbgnrotmeovswaqh
uikfmdjxyznbgnrozcepvswaqm
uikfmdjxyzlbdnrotcepdsyaqh
umkfmdjxfzlbgnrotiepvswaqh
uitfmdjxyzvbcnrotcepvswaqh
uikfmdjqyzlbgnrvtcepvlwaqh
uikfmdjxyzkbworotcepvswaqh
uikfmdzxyzlbwnrotcypvswaqh
uikfmdjxyklbgnrftyepvswaqh
uinfmsjxyzlbgnrotcepsswaqh
xisfmdjxymlbgnrotcepvswaqh
uikfmdjxjzlbgnropcepvswaqv
uikfmdjxyalegyrotcepvswaqh
uikfydjxyzlbgnrotcekvswtqh
uikwmojxyzlbgnromcepvswaqh
uikdmdjayzlbgnrotcepvswzqh
uikfmdjxyzlmvnrotctpvswaqh
uikfmbjxyzlbgnrotceptsweqh
yvkfmdjxyzlbgqrotcepvswaqh
uikomdjxfxlbgnrotcepvswaqh
uikfmdjxczlbgnroocepgswaqh
uikemdjxizlbgnrotcegvswaqh
uikdmdjxyzlbgwrotceprswaqh
tiyfmdjfyzlbgnrotcepvswaqh
tmkfmdjxyzlbgirotcepvswaqh
uikfmdjxyzebgnzotcepqswaqh
uikfmljxyzlbgnrwtcepvswaeh
uikfmojxyzlbgnrotcepbswwqh
uikfmdjxyzlbgsrotcewvswwqh
uikfmdjhyzlbgdrogcepvswaqh
uikfmvjxyzlbrnrltcepvswaqh
jikfmdjxyzlbgnrotcepvgcaqh
uikhtdjxyzlbgnrotcepvswarh
uikfmdjxyezbgnritcepvswaqh
uikfwdjxyzlbgnrotzepvsnaqh
uikfmdjxqylbgnrobcepvswaqh
zikzmdjxyzlbgnrhtcepvswaqh
uiksmzjxyzlbgnrftcepvswaqh
uikfmdjxuzlbgnrotcepvsvaqc

12
2018/day03/day03.dyalog Normal file
View file

@ -0,0 +1,12 @@
⎕IO←0
x←⊃⎕nget'input.txt'1
x←⍎¨¨'\d+'⎕S'&'¨x
⍝ Part 1
claims←{id x y w h←⍵ ⋄ (x+w)∘.,y+h}¨x
fabric←1000 10000
{fabric[⍵]+←1}¨claims
+/,1<fabric
⍝ Part 2
⊃⊃x/⍨{∧/,1=fabric[⍵]}¨claims

30
2018/day03/day03.ss Normal file
View file

@ -0,0 +1,30 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/iter)
(export main)
(def (claim-area! fabric claim)
(match claim
([id x y w h] (for* ((i (in-range x w))
(j (in-range y h)))
(hash-update! fabric [i j] (lambda (l) (cons id l)) [])))))
(def (non-overlapping-claim fabric)
(def overlapping (make-hash-table))
(hash-for-each (lambda (k v) (for ((id v)) (hash-put! overlapping id #t))) fabric)
(hash-for-each (lambda (k v) (when (< 1 (length v))
(for ((id v)) (hash-put! overlapping id #f))))
fabric)
(hash->list overlapping)
(hash-find (lambda (k v) (if v k #f)) overlapping))
(def (main . args)
(def claims-str (call-with-input-file "input.txt" (lambda (p) (read-all p read-line))))
(def claims (map (lambda (x) (filter-map string->number (pregexp-split "[ #@,:x]" x))) claims-str))
(def fabric (make-hash-table))
(for ((claim claims))
(claim-area! fabric claim))
(displayln (hash-fold (lambda (k v acc) (if (> (length v) 1) (1+ acc) acc)) 0 fabric))
(displayln (non-overlapping-claim fabric)))

1307
2018/day03/input.txt Normal file

File diff suppressed because it is too large Load diff

77
2018/day04/day04.ss Normal file
View file

@ -0,0 +1,77 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/iter)
(import :std/sort)
(import :std/srfi/1)
(import :std/srfi/43)
(export main)
(defstruct date (year month day))
(defstruct shift (date id sleep))
(def (parse-date str)
(def d (apply make-date (filter-map string->number (pregexp-split "[-\\[ ]" str))))
(let ((parsed-hour (string->number (cadr (pregexp-match "\\s(\\d\\d):\\d\\d" str)))))
(if (< 12 parsed-hour) (set! (date-day d) (1+ (date-day d)))))
d)
(def (parse-minute str)
(string->number (cadr (pregexp-match "\\s\\d\\d:(\\d\\d)" str))))
(def (parse-id str)
(let ((parsed-id (pregexp-match "\\s\\#(\\d+)\\s" str)))
(if parsed-id (string->number (cadr parsed-id)) #f)))
(def (partition-shifts records-str)
(if (null? records-str)
'()
(let-values (((shift rest) (break (lambda (x) (pregexp-match "begins shift" x)) (cdr records-str))))
(cons (cons (car records-str) shift) (partition-shifts rest)))))
(def (parse-shift records-str)
(def id (parse-id (car records-str)))
(def date (parse-date (car records-str)))
(def sleep (make-vector 60 0))
(for ((record (cdr records-str)))
(let ((minute (parse-minute record)))
(if (pregexp-match "falls asleep" record)
(for ((i (in-range minute (- 60 minute)))) (vector-set! sleep i 1))
(for ((i (in-range minute (- 60 minute)))) (vector-set! sleep i 0)))))
(make-shift date id sleep))
(def (find-sleepiest-guard shifts)
(def guards (make-hash-table))
(for ((shift shifts))
(let ((time-asleep (apply + (vector->list (shift-sleep shift)))))
(hash-update! guards (shift-id shift) (lambda (x) (+ x time-asleep)) 0)))
(car (hash-fold (lambda (k v acc) (if (> v (cadr acc)) (list k v) acc)) '(0 0) guards)))
(def (find-sleepiest-minute id shifts)
(def total (make-list 60 0))
(for ((shift shifts))
(when (= id (shift-id shift))
(set! total (map + total (vector->list (shift-sleep shift))))))
(list-index (lambda (x) (= x (apply max total))) total))
(def (find-sleepy shifts)
(def hash (make-hash-table))
(for* ((shift shifts)
(i (in-range 60)))
(hash-update! hash
[(shift-id shift) i]
(lambda (x) (+ x (vector-ref (shift-sleep shift) i)))
0))
(car (hash-fold (lambda (k v acc) (if (> v (cadr acc)) (list k v) acc)) '(0 0) hash)))
(def (main . args)
(def records-str
(sort
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))
string<?))
(def shifts (map parse-shift (partition-shifts records-str)))
(def sleepy-guard (find-sleepiest-guard shifts))
(def sleepy-minute (find-sleepiest-minute sleepy-guard shifts))
(displayln (* sleepy-guard sleepy-minute))
(displayln (apply * (find-sleepy shifts))))

1088
2018/day04/input.txt Normal file

File diff suppressed because it is too large Load diff

28
2018/day05/day05.ss Normal file
View file

@ -0,0 +1,28 @@
(import :gerbil/gambit/ports)
(import :std/iter)
(import :std/srfi/13)
(export main)
(def (opposite? x y)
(= (abs (- (char->integer x) (char->integer y))) 32))
(def (react-polymer polymer)
(foldl (lambda (x acc) (match acc
((cons y rest) (if (opposite? x y) rest (cons x acc)))
(else (cons x acc))))
'()
polymer))
(def (remove-unit unit polymer)
(string-delete (lambda (x) (char=? (char-downcase x) (char-downcase unit))) polymer))
(def (find-minimal polymer)
(apply min
(for/collect ((c (in-range (char->integer #\a) 26)))
(length (react-polymer (string->list (remove-unit (integer->char c) polymer)))))))
(def (main . args)
(def polymer (call-with-input-file "input.txt" read-line))
(displayln (length (react-polymer (string->list polymer))))
(displayln (find-minimal polymer)))

1
2018/day05/input.txt Normal file

File diff suppressed because one or more lines are too long

35
2018/day07/day07.ss Normal file
View file

@ -0,0 +1,35 @@
(import :gerbil/gambit/ports)
(import :std/iter)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/sort)
(export main)
(def (no-incoming? edges m)
(not (member m (map cadr edges))))
(def (find-no-incoming edges)
(sort
(filter (lambda (m) (no-incoming? edges m)) (delete-duplicates (apply append edges)))
char<?))
(def (topological-sort edges)
(def (aux edges l s)
(match s
([] l)
([n . rest] (let* ((new-edges (filter (lambda (e) (not (eq? n (car e)))) edges))
(additional-s (map cadr (filter (lambda (e)
(and (eq? n (car e)) (no-incoming? new-edges (cadr e))))
edges)))
(new-s (sort (delete-duplicates (append rest additional-s)) char<?)))
(aux new-edges (cons n l) new-s)))))
(reverse (aux edges [] (find-no-incoming edges))))
(def (main . args)
(def edges
(map (lambda (e)
(map (lambda (s) (car (string->list s)))
(cdr (pregexp-match "Step (.) must be finished before step (.) can begin." e))))
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))))
(displayln (list->string (topological-sort edges))))

101
2018/day07/input.txt Normal file
View file

@ -0,0 +1,101 @@
Step C must be finished before step P can begin.
Step V must be finished before step Q can begin.
Step T must be finished before step X can begin.
Step B must be finished before step U can begin.
Step Z must be finished before step O can begin.
Step P must be finished before step I can begin.
Step D must be finished before step G can begin.
Step A must be finished before step Y can begin.
Step R must be finished before step O can begin.
Step J must be finished before step E can begin.
Step N must be finished before step S can begin.
Step X must be finished before step H can begin.
Step F must be finished before step L can begin.
Step S must be finished before step I can begin.
Step W must be finished before step Q can begin.
Step H must be finished before step K can begin.
Step K must be finished before step Q can begin.
Step E must be finished before step L can begin.
Step Q must be finished before step O can begin.
Step U must be finished before step G can begin.
Step L must be finished before step O can begin.
Step Y must be finished before step G can begin.
Step G must be finished before step I can begin.
Step M must be finished before step I can begin.
Step I must be finished before step O can begin.
Step A must be finished before step N can begin.
Step H must be finished before step O can begin.
Step T must be finished before step O can begin.
Step H must be finished before step U can begin.
Step A must be finished before step I can begin.
Step B must be finished before step R can begin.
Step V must be finished before step T can begin.
Step H must be finished before step M can begin.
Step C must be finished before step A can begin.
Step B must be finished before step G can begin.
Step L must be finished before step Y can begin.
Step T must be finished before step J can begin.
Step A must be finished before step R can begin.
Step X must be finished before step L can begin.
Step B must be finished before step L can begin.
Step A must be finished before step F can begin.
Step K must be finished before step O can begin.
Step W must be finished before step M can begin.
Step Z must be finished before step N can begin.
Step Z must be finished before step S can begin.
Step R must be finished before step K can begin.
Step Q must be finished before step L can begin.
Step G must be finished before step O can begin.
Step F must be finished before step Y can begin.
Step V must be finished before step H can begin.
Step E must be finished before step I can begin.
Step W must be finished before step Y can begin.
Step U must be finished before step I can begin.
Step F must be finished before step K can begin.
Step M must be finished before step O can begin.
Step Z must be finished before step H can begin.
Step X must be finished before step S can begin.
Step J must be finished before step O can begin.
Step B must be finished before step I can begin.
Step F must be finished before step H can begin.
Step D must be finished before step U can begin.
Step E must be finished before step M can begin.
Step Z must be finished before step X can begin.
Step P must be finished before step L can begin.
Step W must be finished before step H can begin.
Step C must be finished before step D can begin.
Step A must be finished before step X can begin.
Step Q must be finished before step I can begin.
Step R must be finished before step Y can begin.
Step B must be finished before step A can begin.
Step N must be finished before step L can begin.
Step H must be finished before step G can begin.
Step Y must be finished before step M can begin.
Step L must be finished before step G can begin.
Step G must be finished before step M can begin.
Step Z must be finished before step R can begin.
Step S must be finished before step Q can begin.
Step P must be finished before step J can begin.
Step V must be finished before step J can begin.
Step J must be finished before step I can begin.
Step J must be finished before step X can begin.
Step W must be finished before step O can begin.
Step B must be finished before step F can begin.
Step R must be finished before step M can begin.
Step V must be finished before step S can begin.
Step H must be finished before step E can begin.
Step E must be finished before step U can begin.
Step R must be finished before step W can begin.
Step X must be finished before step Q can begin.
Step N must be finished before step G can begin.
Step T must be finished before step I can begin.
Step L must be finished before step M can begin.
Step H must be finished before step I can begin.
Step U must be finished before step M can begin.
Step C must be finished before step H can begin.
Step P must be finished before step H can begin.
Step J must be finished before step F can begin.
Step A must be finished before step O can begin.
Step X must be finished before step M can begin.
Step H must be finished before step L can begin.
Step W must be finished before step K can begin.

46
2018/day08/day08.ss Normal file
View file

@ -0,0 +1,46 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/iter)
(export main)
(defstruct node (children metadata))
(def (parse-node lst)
(match lst
([m n . rest]
(let*-values (((children rest) (parse-children rest m))
((metadata rest) (split-at rest n)))
(values (make-node children metadata) rest)))))
(def (parse-children lst m)
(if (= m 0)
(values [] lst)
(let*-values (((children rest) (parse-children lst (1- m)))
((child rest) (parse-node rest)))
(values (cons child children) rest))))
(def (dfs node f g)
(if (null? (node-children node))
(f (node-metadata node))
(g (cons (f (node-metadata node))
(map (lambda (n) (dfs n f g)) (node-children node))))))
(def (node-value node)
(if (null? (node-children node))
(apply + (node-metadata node))
(apply + (for/collect ((m (node-metadata node)))
(if (and (> m 0) (< (1- m) (length (node-children node))))
(node-value (list-ref (reverse (node-children node)) (1- m)))
0)))))
(def (main . args)
(def example-lst [2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2])
(define-values (example _) (parse-node example-lst))
(displayln (dfs example (lambda (m) (apply + m)) (lambda (m) (apply + m))))
(displayln (node-value example))
(def lst (map string->number (pregexp-split " " (call-with-input-file "input.txt" read-line))))
(define-values (tree _) (parse-node lst))
(displayln (dfs tree (lambda (m) (apply + m)) (lambda (m) (apply + m))))
(displayln (node-value tree)))

1
2018/day08/input.txt Normal file

File diff suppressed because one or more lines are too long

61
2018/day09/day09.ss Normal file
View file

@ -0,0 +1,61 @@
(import :std/srfi/1)
(export main)
(def (rotate-right deque k)
(def left (car deque))
(def right (cadr deque))
(if (= k 0)
deque
(if (null? right)
(rotate-right [[] (reverse left)] k)
(rotate-right [(cons (car right) left) (cdr right)] (1- k)))))
(def (rotate-left deque k)
(def left (car deque))
(def right (cadr deque))
(if (= k 0)
deque
(if (null? left)
(rotate-left [(reverse right) '()] k)
(rotate-left [(cdr left) (cons (car left) right)] (1- k)))))
(def (deque-car deque)
(match deque
([left right] (car right))))
(def (deque-cdr deque)
(match deque
([left right] [left (cdr right)])))
(def (deque-cons x deque)
(match deque
([left right] [left (cons x right)])))
(def (display-deque deque)
(displayln (reverse (car deque)) " " (cadr deque)))
(def (play circle marbles scores player)
;; (display-deque circle)
(match marbles
([] scores)
([marble . rest]
(def new-player (remainder (1+ player) (vector-length scores)))
(if (= 0 (remainder marble 23))
(let ((new-circle (rotate-left circle 7)))
(vector-set! scores player (+ marble (deque-car new-circle) (vector-ref scores player)))
(play (deque-cdr new-circle) (cdr marbles) scores new-player))
(let ((new-circle (deque-cons marble (rotate-right circle 2))))
(play new-circle (cdr marbles) scores new-player))))))
(def (marbles-game n-players last-marble)
(apply max (vector->list (play [[] [0]] (iota last-marble 1) (make-vector n-players 0) 0))))
(def (main (n-players "9") (last-marble "25") . args)
(displayln (marbles-game 9 25))
(displayln (marbles-game 10 1618))
(displayln (marbles-game 13 7999))
(displayln (marbles-game 17 1104))
(displayln (marbles-game 21 6111))
(displayln (marbles-game 30 5807))
(displayln (marbles-game (string->number n-players) (string->number last-marble))))

1
2018/day09/input.txt Normal file
View file

@ -0,0 +1 @@
463 players; last marble is worth 71787 points

51
2018/day10/day10.ss Normal file
View file

@ -0,0 +1,51 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/srfi/13)
(import :std/iter)
(export main)
(defstruct point (x y vx vy))
(def (step-point p n)
(make-point (+ (* n (point-vx p)) (point-x p))
(+ (* n (point-vy p)) (point-y p))
(point-vx p)
(point-vy p)))
(def (step-points points n)
(map (lambda (p) (step-point p n)) points))
(def (bounding-box points)
(let ((xs (map point-x points))
(ys (map point-y points)))
[(- (apply max xs) (apply min xs) -1)
(- (apply max ys) (apply min ys) -1)]))
(def (plot-points points)
(def box (bounding-box points))
(def min-x (apply min (map point-x points)))
(def min-y (apply min (map point-y points)))
(def new-points
(delete-duplicates
(map (lambda (p) [(- (point-x p) min-x) (- (point-y p) min-y)]) points)))
(def plot (make-vector (cadr box) #f))
(for ((i (in-range (cadr box)))) (vector-set! plot i (make-string (car box) #\.)))
(for ((p new-points))
(string-set! (vector-ref plot (cadr p)) (car p) #\#))
(for ((i (in-range (cadr box))))
(displayln (vector-ref plot i))))
(def (main (n "10240") . args)
(def params
(map (lambda (s) (filter-map string->number (pregexp-split "[<>, ]" s)))
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))))
(def points (map (lambda (p) (apply make-point p)) params))
;; (def sizes (for/collect ((i (in-range 20000)))
;; [i (apply * (bounding-box (step-points points i)))]))
;; (displayln
;; (foldl (lambda (s acc) (if (< (cadr s) (cadr acc)) s acc))
;; (car sizes)
;; (cdr sizes)))
(plot-points (step-points points (string->number n))))

379
2018/day10/input.txt Normal file
View file

@ -0,0 +1,379 @@
position=<-50948, 20587> velocity=< 5, -2>
position=< 20732, -51094> velocity=<-2, 5>
position=<-30471, -10131> velocity=< 3, 1>
position=<-40758, 10355> velocity=< 4, -1>
position=< 30912, 20590> velocity=<-3, -2>
position=< 30927, 30827> velocity=<-3, -3>
position=<-20237, 20591> velocity=< 2, -2>
position=< 51423, -51086> velocity=<-5, 5>
position=<-40732, -20370> velocity=< 4, 2>
position=<-20271, -51094> velocity=< 2, 5>
position=<-40764, -30605> velocity=< 4, 3>
position=<-30492, 41073> velocity=< 3, -4>
position=<-30473, -30614> velocity=< 3, 3>
position=<-51004, 41074> velocity=< 5, -4>
position=<-30481, 30829> velocity=< 3, -3>
position=<-50961, -10130> velocity=< 5, 1>
position=<-50968, 51306> velocity=< 5, -5>
position=<-30489, -40850> velocity=< 3, 4>
position=<-20273, 51307> velocity=< 2, -5>
position=<-40732, 10351> velocity=< 4, -1>
position=<-40764, -30612> velocity=< 4, 3>
position=< 10483, 51307> velocity=<-1, -5>
position=<-30511, -30614> velocity=< 3, 3>
position=< 20719, -40848> velocity=<-2, 4>
position=<-20249, -10129> velocity=< 2, 1>
position=< 30919, -51090> velocity=<-3, 5>
position=<-20281, 20595> velocity=< 2, -2>
position=< 30931, 30830> velocity=<-3, -3>
position=<-20240, 41071> velocity=< 2, -4>
position=< -9988, -51087> velocity=< 1, 5>
position=<-50991, 51315> velocity=< 5, -5>
position=<-10022, 30826> velocity=< 1, -3>
position=<-20241, 30830> velocity=< 2, -3>
position=< 10492, -30610> velocity=<-1, 3>
position=<-30525, 51313> velocity=< 3, -5>
position=< 10492, -20370> velocity=<-1, 2>
position=< 10492, -20371> velocity=<-1, 2>
position=<-20255, 30830> velocity=< 2, -3>
position=< 51403, 20595> velocity=<-5, -2>
position=< 10489, -20370> velocity=<-1, 2>
position=< 10492, 41067> velocity=<-1, -4>
position=< 20722, -40854> velocity=<-2, 4>
position=< 10451, 30830> velocity=<-1, -3>
position=< 10452, -10125> velocity=<-1, 1>
position=< 41151, 51306> velocity=<-4, -5>
position=< 51410, -30610> velocity=<-5, 3>
position=<-20289, -51093> velocity=< 2, 5>
position=<-40708, 30828> velocity=< 4, -3>
position=<-20244, -40851> velocity=< 2, 4>
position=<-20257, -30612> velocity=< 2, 3>
position=< 30943, -10131> velocity=<-3, 1>
position=<-40718, 10346> velocity=< 4, -1>
position=<-40769, -51091> velocity=< 4, 5>
position=< 30967, 41074> velocity=<-3, -4>
position=<-40728, -30614> velocity=< 4, 3>
position=<-40711, 20589> velocity=< 4, -2>
position=< 10487, -51090> velocity=<-1, 5>
position=< 10459, 20595> velocity=<-1, -2>
position=<-10025, 10346> velocity=< 1, -1>
position=< 30913, 41066> velocity=<-3, -4>
position=< 51407, 20595> velocity=<-5, -2>
position=< 30911, -10134> velocity=<-3, 1>
position=<-40769, 51310> velocity=< 4, -5>
position=<-10001, -51085> velocity=< 1, 5>
position=<-10004, 20594> velocity=< 1, -2>
position=<-20281, -40849> velocity=< 2, 4>
position=< 51444, -20365> velocity=<-5, 2>
position=<-40769, 20593> velocity=< 4, -2>
position=< 41183, 20588> velocity=<-4, -2>
position=< 30932, 51315> velocity=<-3, -5>
position=< 41192, -10130> velocity=<-4, 1>
position=< 10488, 41068> velocity=<-1, -4>
position=<-10022, -20370> velocity=< 1, 2>
position=< 30954, 30831> velocity=<-3, -3>
position=<-30513, -40845> velocity=< 3, 4>
position=< 51428, 30831> velocity=<-5, -3>
position=<-40760, -40845> velocity=< 4, 4>
position=<-10009, 30832> velocity=< 1, -3>
position=< 41151, 20586> velocity=<-4, -2>
position=< 30972, 30831> velocity=<-3, -3>
position=< 20675, -30607> velocity=<-2, 3>
position=< 51436, -30606> velocity=<-5, 3>
position=<-50977, -40852> velocity=< 5, 4>
position=< 10471, -40847> velocity=<-1, 4>
position=< 10463, 20590> velocity=<-1, -2>
position=< 30911, -10129> velocity=<-3, 1>
position=< 10476, 41074> velocity=<-1, -4>
position=< 51423, 51309> velocity=<-5, -5>
position=<-50948, -20367> velocity=< 5, 2>
position=< 51419, 30835> velocity=<-5, -3>
position=< 10474, 51306> velocity=<-1, -5>
position=< 10431, 51308> velocity=<-1, -5>
position=<-40733, 41074> velocity=< 4, -4>
position=< 30915, 51306> velocity=<-3, -5>
position=< 30959, 10354> velocity=<-3, -1>
position=< 30972, 20595> velocity=<-3, -2>
position=< 30913, -30610> velocity=<-3, 3>
position=<-10041, -20368> velocity=< 1, 2>
position=<-50972, -20368> velocity=< 5, 2>
position=<-30476, 30828> velocity=< 3, -3>
position=< 41196, -40851> velocity=<-4, 4>
position=<-40753, -20374> velocity=< 4, 2>
position=< 30927, -20368> velocity=<-3, 2>
position=<-20271, -10130> velocity=< 2, 1>
position=<-10046, -40850> velocity=< 1, 4>
position=<-20244, 51308> velocity=< 2, -5>
position=<-20280, 30835> velocity=< 2, -3>
position=< 51391, -30610> velocity=<-5, 3>
position=< 20724, -20365> velocity=<-2, 2>
position=<-40710, 10352> velocity=< 4, -1>
position=<-20289, 41075> velocity=< 2, -4>
position=< 20690, -51085> velocity=<-2, 5>
position=<-40729, 30834> velocity=< 4, -3>
position=< 51428, 51313> velocity=<-5, -5>
position=< -9998, -10129> velocity=< 1, 1>
position=< 20728, -30612> velocity=<-2, 3>
position=< 51447, 41075> velocity=<-5, -4>
position=< 41159, -30605> velocity=<-4, 3>
position=<-40744, -20374> velocity=< 4, 2>
position=< 41188, -51085> velocity=<-4, 5>
position=< 30943, -10126> velocity=<-3, 1>
position=<-10006, -10134> velocity=< 1, 1>
position=< 51399, -10131> velocity=<-5, 1>
position=< 41204, 30834> velocity=<-4, -3>
position=< 30914, -40850> velocity=<-3, 4>
position=< 30948, -51093> velocity=<-3, 5>
position=<-30486, 30826> velocity=< 3, -3>
position=< 10439, 10346> velocity=<-1, -1>
position=< 20721, -51089> velocity=<-2, 5>
position=<-50956, -10130> velocity=< 5, 1>
position=< 10464, -30613> velocity=<-1, 3>
position=< 10492, -30608> velocity=<-1, 3>
position=<-10031, 10346> velocity=< 1, -1>
position=< 30953, 30830> velocity=<-3, -3>
position=< 41204, 51308> velocity=<-4, -5>
position=< 20695, -10127> velocity=<-2, 1>
position=<-40709, -10126> velocity=< 4, 1>
position=<-30497, -20369> velocity=< 3, 2>
position=< 51396, 10354> velocity=<-5, -1>
position=< 20698, 41075> velocity=<-2, -4>
position=<-20286, -51089> velocity=< 2, 5>
position=< 30955, 41066> velocity=<-3, -4>
position=<-10046, 30830> velocity=< 1, -3>
position=<-20254, 10352> velocity=< 2, -1>
position=< -9993, -51094> velocity=< 1, 5>
position=<-50967, -40850> velocity=< 5, 4>
position=<-40753, -51088> velocity=< 4, 5>
position=<-20272, 30835> velocity=< 2, -3>
position=< 10475, 51313> velocity=<-1, -5>
position=<-40769, -40851> velocity=< 4, 4>
position=< 51447, -10128> velocity=<-5, 1>
position=<-10036, 20595> velocity=< 1, -2>
position=< 51431, -40852> velocity=<-5, 4>
position=< 41204, -10127> velocity=<-4, 1>
position=< 51448, 51307> velocity=<-5, -5>
position=<-51008, -51094> velocity=< 5, 5>
position=< 20727, -20369> velocity=<-2, 2>
position=<-50969, 41075> velocity=< 5, -4>
position=< 20707, 10353> velocity=<-2, -1>
position=< 51447, -10130> velocity=<-5, 1>
position=< 10439, 20588> velocity=<-1, -2>
position=< 10490, -20368> velocity=<-1, 2>
position=<-40769, 41068> velocity=< 4, -4>
position=< 30915, -51090> velocity=<-3, 5>
position=<-20273, 30831> velocity=< 2, -3>
position=< 10463, 20586> velocity=<-1, -2>
position=< 51417, 41075> velocity=<-5, -4>
position=<-50948, 41071> velocity=< 5, -4>
position=<-30470, 20591> velocity=< 3, -2>
position=< 51439, 51311> velocity=<-5, -5>
position=< 51399, 51307> velocity=<-5, -5>
position=<-30478, -40849> velocity=< 3, 4>
position=<-40727, 41070> velocity=< 4, -4>
position=<-30503, 51306> velocity=< 3, -5>
position=<-20252, 51308> velocity=< 2, -5>
position=<-20265, -51094> velocity=< 2, 5>
position=<-30468, -40846> velocity=< 3, 4>
position=<-30501, -30611> velocity=< 3, 3>
position=<-20263, -40849> velocity=< 2, 4>
position=< 20714, 20591> velocity=<-2, -2>
position=<-20252, -40854> velocity=< 2, 4>
position=<-40729, -40853> velocity=< 4, 4>
position=<-10047, 30826> velocity=< 1, -3>
position=< 10479, -51088> velocity=<-1, 5>
position=< 51443, 41071> velocity=<-5, -4>
position=<-50969, -40854> velocity=< 5, 4>
position=<-40742, -20374> velocity=< 4, 2>
position=< 41155, 20592> velocity=<-4, -2>
position=< 41175, 41074> velocity=<-4, -4>
position=< 20679, 20594> velocity=<-2, -2>
position=<-20265, 10355> velocity=< 2, -1>
position=< 41184, 20587> velocity=<-4, -2>
position=< 41177, -40845> velocity=<-4, 4>
position=< 30938, -20374> velocity=<-3, 2>
position=< 20732, 30830> velocity=<-2, -3>
position=<-20273, 51314> velocity=< 2, -5>
position=< 41178, 51310> velocity=<-4, -5>
position=< 20687, 30829> velocity=<-2, -3>
position=<-50964, 10347> velocity=< 5, -1>
position=< 51391, -51093> velocity=<-5, 5>
position=<-30485, -51088> velocity=< 3, 5>
position=<-30468, -30611> velocity=< 3, 3>
position=<-30513, 51312> velocity=< 3, -5>
position=<-40753, 10347> velocity=< 4, -1>
position=<-50993, 51308> velocity=< 5, -5>
position=< 41194, -51089> velocity=<-4, 5>
position=< 30919, -30607> velocity=<-3, 3>
position=< 51436, 51315> velocity=<-5, -5>
position=<-20245, -20368> velocity=< 2, 2>
position=< 30913, -20370> velocity=<-3, 2>
position=< 30943, -51086> velocity=<-3, 5>
position=<-20273, 41073> velocity=< 2, -4>
position=< 10484, -40850> velocity=<-1, 4>
position=< 10492, -40848> velocity=<-1, 4>
position=<-10009, 30826> velocity=< 1, -3>
position=< 20679, -20368> velocity=<-2, 2>
position=<-50959, -30614> velocity=< 5, 3>
position=<-40741, -10134> velocity=< 4, 1>
position=< 30919, 30834> velocity=<-3, -3>
position=<-10033, 30833> velocity=< 1, -3>
position=<-30489, 30831> velocity=< 3, -3>
position=<-30492, -40848> velocity=< 3, 4>
position=<-40720, -30613> velocity=< 4, 3>
position=<-50993, -30612> velocity=< 5, 3>
position=<-40745, -20365> velocity=< 4, 2>
position=<-10024, -20368> velocity=< 1, 2>
position=< 41152, 30830> velocity=<-4, -3>
position=< 30951, 20586> velocity=<-3, -2>
position=<-40740, 41075> velocity=< 4, -4>
position=< 51416, -40845> velocity=<-5, 4>
position=<-30527, -51090> velocity=< 3, 5>
position=< 41161, 51315> velocity=<-4, -5>
position=<-40764, -20365> velocity=< 4, 2>
position=< 51448, -51093> velocity=<-5, 5>
position=<-30521, 10349> velocity=< 3, -1>
position=<-20249, 51312> velocity=< 2, -5>
position=<-30513, 10354> velocity=< 3, -1>
position=< 30945, 30830> velocity=<-3, -3>
position=< -9996, 30832> velocity=< 1, -3>
position=< 10468, -51085> velocity=<-1, 5>
position=<-30487, 10346> velocity=< 3, -1>
position=< 51391, 10352> velocity=<-5, -1>
position=< 10471, 41067> velocity=<-1, -4>
position=<-10033, 30830> velocity=< 1, -3>
position=< 30932, 30835> velocity=<-3, -3>
position=< 41199, 51308> velocity=<-4, -5>
position=<-40713, -10134> velocity=< 4, 1>
position=<-10005, -51088> velocity=< 1, 5>
position=< 10475, -20370> velocity=<-1, 2>
position=< 10484, 41071> velocity=<-1, -4>
position=< 10439, -20367> velocity=<-1, 2>
position=< 10448, -51090> velocity=<-1, 5>
position=<-30529, 20591> velocity=< 3, -2>
position=< 41188, 41068> velocity=<-4, -4>
position=< 51419, 10349> velocity=<-5, -1>
position=<-20289, 10348> velocity=< 2, -1>
position=<-20265, -10134> velocity=< 2, 1>
position=< 30927, 30831> velocity=<-3, -3>
position=< 41183, -40849> velocity=<-4, 4>
position=< 10487, -10128> velocity=<-1, 1>
position=<-20236, -51086> velocity=< 2, 5>
position=< 30943, -20371> velocity=<-3, 2>
position=< 30948, 20589> velocity=<-3, -2>
position=<-10015, -30611> velocity=< 1, 3>
position=< 41208, -20372> velocity=<-4, 2>
position=< 51431, -20372> velocity=<-5, 2>
position=<-10016, 41068> velocity=< 1, -4>
position=< 41180, -40852> velocity=<-4, 4>
position=<-20230, -10129> velocity=< 2, 1>
position=<-40713, -51092> velocity=< 4, 5>
position=<-40712, 41067> velocity=< 4, -4>
position=< 51418, 20595> velocity=<-5, -2>
position=< 41191, 30827> velocity=<-4, -3>
position=<-20233, -10133> velocity=< 2, 1>
position=< 51395, 10352> velocity=<-5, -1>
position=< 41212, -30609> velocity=<-4, 3>
position=< 41167, -51086> velocity=<-4, 5>
position=<-40737, 20595> velocity=< 4, -2>
position=<-10040, -10125> velocity=< 1, 1>
position=<-40753, -10129> velocity=< 4, 1>
position=< 30927, 51310> velocity=<-3, -5>
position=< 20676, 20589> velocity=<-2, -2>
position=<-30529, -10128> velocity=< 3, 1>
position=< 20729, 51310> velocity=<-2, -5>
position=<-20229, -40847> velocity=< 2, 4>
position=< 41196, 20587> velocity=<-4, -2>
position=< 10468, 51314> velocity=<-1, -5>
position=< 20676, 41067> velocity=<-2, -4>
position=< -9993, 30834> velocity=< 1, -3>
position=<-51004, 51307> velocity=< 5, -5>
position=< 51410, 30835> velocity=<-5, -3>
position=<-30476, 41075> velocity=< 3, -4>
position=<-20281, 20590> velocity=< 2, -2>
position=< 20727, -40852> velocity=<-2, 4>
position=<-10001, -30609> velocity=< 1, 3>
position=< 20691, 30826> velocity=<-2, -3>
position=<-51001, 20587> velocity=< 5, -2>
position=<-51001, 51310> velocity=< 5, -5>
position=<-20252, -30612> velocity=< 2, 3>
position=<-20257, -30610> velocity=< 2, 3>
position=< 20703, -20370> velocity=<-2, 2>
position=< 41207, -51093> velocity=<-4, 5>
position=<-30481, 20593> velocity=< 3, -2>
position=<-30510, -30614> velocity=< 3, 3>
position=<-10001, -20366> velocity=< 1, 2>
position=<-30485, 20590> velocity=< 3, -2>
position=< 10460, -51094> velocity=<-1, 5>
position=< 51410, -40850> velocity=<-5, 4>
position=<-20236, 20589> velocity=< 2, -2>
position=<-20241, 20594> velocity=< 2, -2>
position=<-10044, -10132> velocity=< 1, 1>
position=<-10048, 20586> velocity=< 1, -2>
position=<-20245, -10130> velocity=< 2, 1>
position=< 30951, -10131> velocity=<-3, 1>
position=< -9993, -40852> velocity=< 1, 4>
position=< 30971, -30607> velocity=<-3, 3>
position=< 51391, 20594> velocity=<-5, -2>
position=<-50951, -51091> velocity=< 5, 5>
position=< -9993, 20593> velocity=< 1, -2>
position=< 10480, -10129> velocity=<-1, 1>
position=< 41211, 51314> velocity=<-4, -5>
position=< 30912, -10134> velocity=<-3, 1>
position=< 30944, 30828> velocity=<-3, -3>
position=< 30915, -51090> velocity=<-3, 5>
position=< -9999, 10351> velocity=< 1, -1>
position=< 51395, 51312> velocity=<-5, -5>
position=<-40729, 20588> velocity=< 4, -2>
position=<-20229, -40846> velocity=< 2, 4>
position=<-10033, 30833> velocity=< 1, -3>
position=< 41168, 41066> velocity=<-4, -4>
position=< 20703, 51306> velocity=<-2, -5>
position=< 20732, 51309> velocity=<-2, -5>
position=<-10020, 10347> velocity=< 1, -1>
position=<-40716, -10130> velocity=< 4, 1>
position=< 41212, 10352> velocity=<-4, -1>
position=< 10464, -51093> velocity=<-1, 5>
position=< 10434, 41066> velocity=<-1, -4>
position=< 10451, 51315> velocity=<-1, -5>
position=<-40764, -10133> velocity=< 4, 1>
position=<-10049, -10126> velocity=< 1, 1>
position=< 10463, 10347> velocity=<-1, -1>
position=< 20732, 41075> velocity=<-2, -4>
position=<-30488, -10130> velocity=< 3, 1>
position=<-10009, 10349> velocity=< 1, -1>
position=<-40744, -10134> velocity=< 4, 1>
position=< 20703, 41073> velocity=<-2, -4>
position=< 41204, -51092> velocity=<-4, 5>
position=<-50960, -51093> velocity=< 5, 5>
position=<-40748, 20586> velocity=< 4, -2>
position=< 51428, 20591> velocity=<-5, -2>
position=<-30489, 30831> velocity=< 3, -3>
position=< 10472, 10350> velocity=<-1, -1>
position=< 41199, -20371> velocity=<-4, 2>
position=<-30489, 41069> velocity=< 3, -4>
position=<-20241, 10352> velocity=< 2, -1>
position=< 41188, -51090> velocity=<-4, 5>
position=<-10004, -40845> velocity=< 1, 4>
position=<-30497, -51088> velocity=< 3, 5>
position=< 30943, 20591> velocity=<-3, -2>
position=<-50956, -10129> velocity=< 5, 1>
position=< 41186, -20369> velocity=<-4, 2>
position=< 30948, 41066> velocity=<-3, -4>
position=< 30919, 51309> velocity=<-3, -5>
position=< 10451, 10355> velocity=<-1, -1>
position=< 10475, 20586> velocity=<-1, -2>
position=< 30959, 20588> velocity=<-3, -2>
position=< 41187, -51087> velocity=<-4, 5>
position=< 30956, 51309> velocity=<-3, -5>
position=<-30497, 41075> velocity=< 3, -4>
position=< 51396, 51309> velocity=<-5, -5>
position=< 30962, -51094> velocity=<-3, 5>
position=< 30967, -51091> velocity=<-3, 5>
position=< 10487, 20587> velocity=<-1, -2>
position=<-40732, 30826> velocity=< 4, -3>
position=<-50985, 10354> velocity=< 5, -1>
position=<-40753, -40850> velocity=< 4, 4>
position=< 10476, -51092> velocity=<-1, 5>
position=<-50966, 10350> velocity=< 5, -1>
position=< 30930, 51306> velocity=<-3, -5>

75
2018/day11/day11.ss Normal file
View file

@ -0,0 +1,75 @@
(import :std/srfi/1)
(import :std/srfi/43)
(import :std/iter)
(import :gerbil/gambit/hvectors)
(export main)
(def (hundreds-digit n)
(remainder (quotient n 100) 10))
(def (power serial-number x y)
(def rack-id (+ x 10))
(- (hundreds-digit (* rack-id (+ (* rack-id y) serial-number))) 5))
(def (build-grid serial-number grid-size)
(def grid (make-vector grid-size #f))
(for ((i (in-range grid-size)))
(vector-set! grid i (make-s32vector grid-size 0)))
(for* ((i (in-range grid-size))
(j (in-range grid-size)))
(s32vector-set! (vector-ref grid i)
j
(power serial-number i j)))
(for* ((i (in-range grid-size))
(j (in-range grid-size)))
(if (> i 0)
(let ((cur-row (vector-ref grid i))
(prev-row (vector-ref grid (- i 1))))
(s32vector-set! cur-row j
(+ (s32vector-ref cur-row j)
(if (> j 0) (s32vector-ref cur-row (- j 1)) 0)
(s32vector-ref prev-row j)
(if (> j 0) (- (s32vector-ref prev-row (- j 1))) 0))))
(let ((cur-row (vector-ref grid i)))
(s32vector-set! cur-row j
(+ (s32vector-ref cur-row j)
(if (> j 0) (s32vector-ref cur-row (- j 1)) 0))))))
grid)
(def (largest-power-cell grid cell-size)
(def grid-size (vector-length grid))
(def max -10000)
(def max-idx [0 0])
(for* ((i (in-range (- grid-size cell-size)))
(j (in-range (- grid-size cell-size))))
(let* ((top-row (vector-ref grid i))
(bottom-row (vector-ref grid (+ i cell-size)))
(sum (- (+ (s32vector-ref bottom-row (+ j cell-size))
(s32vector-ref top-row j))
(+ (s32vector-ref bottom-row j)
(s32vector-ref top-row (+ j cell-size))))))
(when (> sum max)
(begin
(set! max sum)
(set! max-idx [(1+ i) (1+ j)])))))
(cons max max-idx))
(def (largest-power-cell-size grid)
(def grid-size (vector-length grid))
(def max-size 0)
(def max -1000)
(def res #f)
(for ((size (in-range grid-size)))
(let ((x (largest-power-cell grid size)))
(when (> (car x) max)
(begin
(set! max (car x))
(set! max-size size)
(set! res (cons size x))))))
res)
(def (main (serial-number "9005") . args)
(def grid (build-grid (string->number serial-number) 300))
(displayln (largest-power-cell grid 3))
(displayln (largest-power-cell-size grid)))