LoginSignup
1
1

More than 5 years have passed since last update.

オフラインリアルタイムどう書くF02の問題をPythonで解いてみた

Last updated at Posted at 2017-02-22

問題 : 2つの矩形に含まれるマス目の数 2017.2.22 問題
実装リンク集 : オフラインリアルタイムどう書くF02の問題 - Qiita

ひとまず10x10の方だけ。

def solve(data):
    area = lambda x1, y1, x2, y2: lambda x, y: x1 <= x <= x2 and y1 <= y <= y2
    areas = [area(*map(int, a.replace('-', ',').split(','))) for a in data.split('/')]
    return str(sum(sum(area(x, y) for area in areas) == 2 for x in range(10) for y in range(10)))

def test(data, correct):
    answer = solve(data)
    print ("NG","OK")[answer == correct], data, correct, '->', answer

test( "5,1-7,9/3,2-8,6/0,5-9,5", "15" )
test( "0,0-9,9/0,0-9,9/0,0-9,9", "0" )
test( "0,0-9,9/0,0-0,9/1,0-9,9", "100" )
test( "2,5-7,6/0,5-7,7/2,0-8,6", "0" )
test( "1,9-4,9/4,9-7,9/0,3-7,4", "1" )
test( "6,1-6,9/5,0-7,4/5,1-7,2", "6" )
test( "4,0-9,8/5,1-6,8/0,2-9,7", "28" )
test( "2,8-8,9/7,9-8,9/8,3-8,9", "2" )
test( "3,3-9,4/0,1-8,4/1,2-8,9", "12" )
test( "2,1-8,3/0,1-3,7/8,3-8,4", "7" )
test( "5,4-6,9/0,0-6,0/5,3-9,8", "10" )
test( "1,1-9,7/1,1-3,8/3,8-7,9", "22" )
test( "2,4-6,7/3,2-7,8/1,0-9,4", "24" )
test( "0,2-1,5/8,1-8,3/1,8-6,8", "0" )
test( "5,2-9,5/9,1-9,2/8,0-8,6", "5" )
test( "5,0-6,4/2,1-6,4/3,8-3,9", "8" )
test( "0,4-6,9/4,1-6,9/7,6-9,7", "18" )
test( "0,0-5,5/0,1-2,8/5,3-9,4", "17" )
test( "0,2-5,6/5,6-8,7/0,1-2,6", "16" )
test( "7,2-8,4/1,0-6,8/1,3-7,6", "26" )
test( "4,3-9,3/0,0-6,5/0,0-4,8", "31" )
test( "3,4-4,6/2,2-4,8/2,0-8,4", "11" )
test( "1,2-6,5/0,5-4,7/2,8-2,9", "4" )
test( "4,1-7,5/2,1-9,9/1,7-2,9", "23" )
test( "1,6-5,6/0,3-5,7/0,2-2,6", "13" )
test( "1,3-3,4/1,4-3,4/9,2-9,9", "3" )
test( "6,3-7,6/2,2-2,3/1,3-9,8", "9" )
test( "2,2-9,7/1,8-9,8/2,2-8,9", "49" )
test( "1,2-6,9/7,6-9,9/4,3-9,9", "33" )
test( "6,0-7,5/0,4-3,8/1,4-5,8", "15" )
test( "2,0-9,7/0,5-3,8/5,1-7,7", "27" )
test( "1,2-8,7/3,1-4,3/2,3-5,8", "20" )
test( "1,0-7,7/0,1-5,4/0,0-2,3", "19" )
test( "2,0-3,7/1,1-3,7/5,3-5,9", "14" )
test( "7,2-9,8/1,0-6,8/0,2-9,9", "63" )
test( "1,1-5,3/0,3-8,7/2,3-8,7", "32" )
test( "3,4-6,6/1,0-9,1/4,0-9,9", "21" )
test( "0,0-4,7/0,5-5,9/0,2-4,5", "25" )
test( "1,1-9,9/2,2-7,4/2,4-7,7", "30" )
test( "3,2-9,9/2,0-6,6/0,5-8,9", "36" )
test( "0,1-8,8/0,5-9,8/2,3-2,4", "38" )
test( "0,0-8,6/4,3-9,9/7,1-9,9", "29" )
test( "0,0-8,8/2,4-9,8/0,1-9,2", "53" )
実行結果
OK 5,1-7,9/3,2-8,6/0,5-9,5 15 -> 15
OK 0,0-9,9/0,0-9,9/0,0-9,9 0 -> 0
OK 0,0-9,9/0,0-0,9/1,0-9,9 100 -> 100
OK 2,5-7,6/0,5-7,7/2,0-8,6 0 -> 0
OK 1,9-4,9/4,9-7,9/0,3-7,4 1 -> 1
OK 6,1-6,9/5,0-7,4/5,1-7,2 6 -> 6
OK 4,0-9,8/5,1-6,8/0,2-9,7 28 -> 28
OK 2,8-8,9/7,9-8,9/8,3-8,9 2 -> 2
OK 3,3-9,4/0,1-8,4/1,2-8,9 12 -> 12
OK 2,1-8,3/0,1-3,7/8,3-8,4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> 36
OK 0,1-8,8/0,5-9,8/2,3-2,4 38 -> 38
OK 0,0-8,6/4,3-9,9/7,1-9,9 29 -> 29
OK 0,0-8,8/2,4-9,8/0,1-9,2 53 -> 53
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1