-
Notifications
You must be signed in to change notification settings - Fork 1
/
solve-2.ys
executable file
·41 lines (34 loc) · 918 Bytes
/
solve-2.ys
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
#!/usr/bin/env ys-0
defn safe?(report):
or:
actually-safe?: report
safe-after-problem-dampener?: report
defn indices(coll):
map-indexed _ coll:
fn(i x): i
defn remove-by-index(i coll):
keep-indexed _ coll:
fn(j n):
when (j != i): n
defn words-to-nums(s):
s.words().map(to-num)
defn actually-safe?:
(report):
not:
zero?:
sum:
map (fn [f] (actually-safe? report f)):
list: (fn [a b] (- a b)) (fn [a b] (- b a))
(report tester):
first:
reduce _ [1 report.first()] report.rest():
fn([d a] b):
list: (d * (if (<= 1 (tester a b) 3) 1 0)) b
defn safe-after-problem-dampener?(report):
some truey?:
map _ indices(report):
fn(i):
dampened-report =: remove-by-index(i report)
actually-safe?: dampened-report
defn main():
slurp("input"):lines.map(words-to-nums).filter(safe?):count:println