-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.coffee
49 lines (42 loc) · 776 Bytes
/
2.coffee
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
{_log,test,expect,testAndRun} = require './util'
checksum1 = ( sh )->
sum = 0
for s in sh.split '\n'
min = max = null
for n in s.split /\s+/
min = Math.min +n, min ? +n
max = Math.max +n, max ? +n
sum += max - min
sum
div = ( row )->
for a, i in row
for j in [0...i]
b = row[j]
return a/b unless a%b
return b/a unless b%a
throw new Error 'invalid input'
return
checksum2 = ( sh )->
sum = 0
for s in sh.split '\n'
sum += div s.split /\s+/
sum
test.checksum = ->
expect 18, checksum1 '''
5 1 9 5
7 5 3
2 4 6 8
'''
return
test.checksum2 = ->
expect 9, checksum2 '''
5 9 2 8
9 4 7 3
3 8 6 5
'''
return
testAndRun ->
input = require './input/2.txt'
_log.yellow '1:', checksum1 input
_log.yellow '2:', checksum2 input
return