-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.lisp
74 lines (63 loc) · 2.3 KB
/
day2.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(in-package #:day2)
(coalton-toplevel
(define-type Command
(Forward Integer)
(Up Integer)
(Down Integer))
(declare parse-direction (String -> (Optional Integer) -> (Optional Command)))
(define (parse-direction str num)
(match num
((Some n)
(match str
("forward" (Some (Forward n)))
("up" (Some (Up n)))
("down" (Some (Down n)))
(_ None)))
(_ None)))
(declare read-command (String -> (Optional Command)))
(define (read-command line)
(match (split #\Space line)
((Cons dir (Cons num (Nil)))
(parse-direction dir (parse-int num)))
(_ None)))
(declare read-commands (String -> (List Command)))
(define (read-commands pathname)
(let ((only-some (fn (c)
(match c
((Some x) x))))
(lines (lisp (List String) (pathname)
(adventofcode2021:read-lines pathname))))
(map only-some
(filter issome
(map read-command lines)))))
(declare horiz-x-depth ((List Command) -> Integer))
(define (horiz-x-depth commands)
(let ((inner (fn (h d cmds)
(match cmds
((Nil) (* h d))
((Cons c cs)
(match c
((Forward x) (inner (+ h x) d cs))
((Up x) (inner h (- d x) cs))
((Down x) (inner h (+ d x) cs))))))))
(inner 0 0 commands)))
(declare depth-score ((List Command) -> Integer))
(define (depth-score commands)
(let ((inner (fn (a h d cmds)
(match cmds
((Nil) (* h d))
((Cons c cs)
(match c
((Forward x) (inner a (+ h x) (+ d (* a x)) cs))
((Up x) (inner (- a x) h d cs))
((Down x) (inner (+ a x) h d cs))))))))
(inner 0 0 0 commands)))
#+nil nil)
#+nil
(
(coalton (read-command "forward 5"))
(coalton (map (fn (c) (match c ((Some x) x))) (filter issome (Cons (parse-direction "forward 4") Nil))))
(coalton (horiz-x-depth (read-commands "day2input")))
(coalton (horiz-x-depth (make-list (Forward 5) (Down 4))))
(coalton (depth-score (read-commands "day2input")))
nil)