-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
riff-lang/riff#2 riff-lang/riff#28 - min(), push(), sort(), sum() riff-lang/riff#36 - Boolean exprs should be coercible in arithmetic riff-lang/riff#51 riff-lang/riff#52
- Loading branch information
1 parent
cc04a2a
commit 67f3394
Showing
4 changed files
with
543 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
f = open(arg[1] or 'input') | ||
idx = 1 | ||
while read(f,0) { | ||
eval('l = #{read(f)}') | ||
eval('r = #{read(f)}') | ||
sum += cmp(l, r) < 1 and idx | ||
read(f) | ||
++idx | ||
} | ||
close(f) | ||
print(sum) | ||
|
||
fn minv(x,y) { | ||
return x < y ? x : y | ||
} | ||
|
||
fn cmp(l,r) { | ||
if type(l) == 'int' and type(r) == 'int' { | ||
return l < r ? -1 : l > r | ||
} elif type(l) == 'int' and type(r) == 'table' { | ||
return cmp([l],r) | ||
} elif type(l) == 'table' and type(r) == 'int' { | ||
return cmp(l,[r]) | ||
} else { | ||
local res = 0 | ||
for i in 0..minv(#l,#r)-1:1 { | ||
res = cmp(l[i], r[i]) | ||
if res | ||
return res | ||
} | ||
return cmp(#l,#r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
f = open(arg[1] or 'input') | ||
idx = 1 | ||
for i,s in split(read(f,'a')) | ||
eval('packets[#i] = #s') | ||
close(f) | ||
|
||
packets[#packets] = [[2]] | ||
packets[#packets] = [[6]] | ||
qsort(packets, 0, #packets-1) | ||
for i,p in packets { | ||
if cmp(p, [[2]]) == 0 | ||
i1 = i + 1 | ||
elif cmp(p, [[6]]) == 0 | ||
i2 = i + 1 | ||
} | ||
print(i1*i2) | ||
|
||
fn qsort(t, left, right) { | ||
if right - left < 1 | ||
return; | ||
local pivot = left | ||
for i in left+1..right { | ||
if cmp(t[i], t[pivot]) < 1 { | ||
local temp = t[pivot] | ||
if i == pivot + 1 { | ||
t[pivot] = t[pivot+1] | ||
t[pivot+1] = temp | ||
} else { | ||
t[pivot] = t[i] | ||
t[i] = t[pivot+1] | ||
t[pivot+1] = temp | ||
} | ||
++pivot | ||
} | ||
} | ||
qsort(t, left, pivot - 1) | ||
qsort(t, pivot + 1, right) | ||
} | ||
|
||
fn minv(x,y) { | ||
return x < y ? x : y | ||
} | ||
|
||
fn cmp(l,r) { | ||
if type(l) == 'int' and type(r) == 'int' { | ||
return l < r ? -1 : l > r | ||
} elif type(l) == 'int' and type(r) == 'table' { | ||
return cmp([l],r) | ||
} elif type(l) == 'table' and type(r) == 'int' { | ||
return cmp(l,[r]) | ||
} else { | ||
local res = 0 | ||
for i in 0..minv(#l,#r)-1:1 { | ||
res = cmp(l[i], r[i]) | ||
if res | ||
return res | ||
} | ||
return cmp(#l,#r) | ||
} | ||
} |
Oops, something went wrong.