Skip to content

Commit 0f5b784

Browse files
committed
Add Lua version
1 parent eb1eb5b commit 0f5b784

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ language: crystal
33
before_install:
44
- sudo apt-get -qq update
55
- sudo apt-get install -y ruby default-jdk haskell-platform python3
6+
- wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
7+
- tar -zxv -f lua-5.3.4.tar.gz
8+
- pushd lua-5.3.4 && make CFLAGS="-O2 -fPIC -DLUA_COMPAT_5_2 -DLUA_COMPAT_5_1" linux && sudo make INSTALL_TOP=/usr install && popd
69

710
install: make clean build
811

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ build:
1515
g++ -O2 main.cpp -o $(BIN_DIR)/cpp
1616
javac -d $(BIN_DIR) Main.java
1717
ghc -O2 main.hs -outputdir=$(BUILD_DIR) -o $(BIN_DIR)/haskell
18+
luac -o $(BIN_DIR)/lua main.lua
1819

1920
test:
2021
mkdir $(TMP_DIR)
@@ -32,4 +33,6 @@ test:
3233
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/haskell.out || (echo "Failed: Haskell" && exit 1)
3334
python3 main.py < $(TMP_DIR)/test_data.in > $(TMP_DIR)/python3.out
3435
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/python3.out || (echo "Failed: Python3" && exit 1)
36+
lua $(BIN_DIR)/lua < $(TMP_DIR)/test_data.in > $(TMP_DIR)/lua.out
37+
@cmp $(TMP_DIR)/crystal.out $(TMP_DIR)/lua.out || (echo "Failed: Lua" && exit 1)
3538
@echo "Passed"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Solve 2D Rank Finding Problem in Several Languages
1313
- Java
1414
- Haskell
1515
- Python 3
16+
- Lua
1617

1718
## Problem
1819

@@ -80,3 +81,4 @@ Your output should consist of a single line for each test case containing n numb
8081
- [scps940707](https://github.com/scps940707) YyWang - Java
8182
- [sifmelcara](https://github.com/sifmelcara) mingchuan - Haskell
8283
- [chuanchan1116](https://github.com/chuanchan1116) William Tsai - Python 3
84+
- [firejox](https://github.com/firejox) Firejox - Lua

main.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local Point = {}
2+
Point.__index = Point
3+
Point.__lt = function(a, b)
4+
if a.x == b.x then
5+
return a.y < b.y
6+
end
7+
return a.x < b.x
8+
end
9+
10+
function Point:new(x, y, idx)
11+
return setmetatable({ x = x, y = y, idx = idx }, Point)
12+
end
13+
14+
while true do
15+
local ranks = {}
16+
17+
function find_ranks(pts)
18+
local sz = #pts
19+
local l_sz = math.floor(sz / 2)
20+
local r_sz = sz - l_sz
21+
22+
if sz == 1 then
23+
return pts
24+
end
25+
26+
local pts_l = find_ranks(table.pack(table.unpack(pts, 1, l_sz)))
27+
local pts_r = find_ranks(table.pack(table.unpack(pts, l_sz + 1)))
28+
local res = {}
29+
local i, j = 1, 1
30+
31+
while true do
32+
if pts_l[i].y <= pts_r[j].y then
33+
table.insert(res, pts_l[i])
34+
i = i + 1
35+
if i > l_sz then
36+
while j <= r_sz do
37+
ranks[pts_r[j].idx] = ranks[pts_r[j].idx] + i - 1
38+
table.insert(res, pts_r[j])
39+
j = j + 1
40+
end
41+
break
42+
end
43+
else
44+
table.insert(res, pts_r[j])
45+
ranks[pts_r[j].idx] = ranks[pts_r[j].idx] + i - 1
46+
j = j + 1
47+
if j > r_sz then
48+
table.move(pts_l, i, l_sz, #res + 1, res)
49+
break
50+
end
51+
end
52+
end
53+
54+
return res
55+
end
56+
57+
local pts = {}
58+
local n = io.read("*n")
59+
if n == 0 then
60+
break
61+
end
62+
63+
for i = 1, n do
64+
x, y = io.read("*n", "*n")
65+
pts[i] = Point:new(x, y, i)
66+
ranks[i] = 0
67+
end
68+
69+
table.sort(pts, cmp)
70+
find_ranks(pts)
71+
print(table.concat(ranks, ' '))
72+
end

0 commit comments

Comments
 (0)