Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Created March 11, 2014 13:15
Show Gist options
  • Save mizuhara/9485366 to your computer and use it in GitHub Desktop.
Save mizuhara/9485366 to your computer and use it in GitHub Desktop.
def angle_of(sector)
div, mod = sector.divmod(100)
angle = { 1 => 360 / 8r, 2 => 360 / 16r, 3 => 360 / 24r, 4 => 360 / 32r }[div]
base = angle / 2
if mod.zero?
[base, base + (div * 8 - 1) * angle]
else
[base + angle * mod, base + angle * (mod - 1)]
end
end
def neighboring_sector?(sector, target)
b = target[0] < sector[0] && sector[1] < target[0]
b ||= target[1] < sector[0] && sector[1] < target[1]
b ||= sector[0] < target[0] && target[1] < sector[0] && sector[1] < target[0] && target[1] < sector[1]
b ||= sector[0] < sector[1] && target[0] < target[1]
b ||= sector[0] < sector[1] && sector[0] < target[0] && target[1] < sector[0]
b ||= sector[0] < sector[1] && sector[1] < target[0] && target[1] < sector[1]
b
end
def neighboring_sector_of(sector)
div, mod = sector.divmod(100)
targets = {
1 => (200..215).to_a,
2 => (100..107).to_a + (300..323).to_a,
3 => (200..215).to_a + (400..431).to_a,
4 => (300..323).to_a
}[div]
sector_angle = angle_of(sector)
# 内側・外側のセクタの計算
# 初期値は両端のエッジ
edges = [
div * 100 + (mod.zero? ? div * 8 - 1 : mod - 1),
div * 100 + (mod == div * 8 - 1 ? 0 : mod + 1)
]
ans = targets.each_with_object(edges) do |target, ary|
target_angle = angle_of(target)
ary << target if neighboring_sector?(sector_angle, target_angle)
end
end
def solve(src)
sectors = src.split(",").map(&:to_i)
return "none" if sectors.size == 1
targets = sectors.each_with_object([]) do |sector, ary|
ary << neighboring_sector_of(sector)
end
ans = targets.
flatten.
group_by { |e| e }.
select { |k, v| 2 <= v.size }.
keys.
select { |e| !sectors.include?(e) }.
sort.
join(",")
ans.empty? ? "none" : ans
end
DATA.readlines.each do |line|
no, src, expected = line.split(/\s/)
actual = solve(src)
result = actual == expected ? "ok" : "***NG***"
puts "#{no}: " + result
end
__END__
0 400,401,302 300,301,402
1 105,100,306,414 none
2 100 none
3 211 none
4 317 none
5 414 none
6 100,106 107
7 205,203 102,204
8 303,305 304
9 407,409 306,408
10 104,103 207
11 204,203 102,305
12 313,314 209,418
13 419,418 314
14 100,102,101 201,203
15 103,206,309 205,207,308,310
16 414,310,309 206,311,413
17 104,102,206,307,102,202 101,103,203,204,205,207,308
18 104,206,308,409,407 103,205,207,306,307,309,408,410
19 313,406,213,301,409,422,412,102,428 none
20 101,300,210,308,423,321,403,408,415 none
21 304,316,307,207,427,402,107,431,412,418,424 none
22 205,408,210,215,425,302,311,400,428,412 none
23 200,311,306,412,403,318,427,105,420 none
24 105,305,407,408,309,208,427 104,209,306,406
25 311,304,322,404,429,305,316 203,303,321,405,406,430
26 210,401,316,425,101 211,315
27 414,403,404,416,428,421 303,415
28 207,300,103,211,428 104,206
29 322,314,310 none
30 427,200,215 100,323
31 311,402,424,307,318,430,323,305,201 200,204,301,302,306,322,423,425,431
32 425,430,408 none
33 202,320,209,426 319,427
34 430,209,302,310,304,431,320 202,303,323
35 208,206,406,424,213,312 207,311,313
36 420,302,313,413,317,402 301,403
37 319,306,309,418,204,411 305,307,308,412
38 400,308,105,430,203,428,209 104,210,429,431
39 200,305,214 215
40 214,408,410,407,317,422 306,316,409,423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment