b.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os, argparse
  2. import numpy as np
  3. class bingo_brick:
  4. brick = None
  5. marks = None
  6. has_bingo = None
  7. def __init__(self):
  8. self.marks = np.ones((5,5), dtype=bool)
  9. self.brick = np.ndarray((0))
  10. def append_row(self, row):
  11. if self.brick.size == 0:
  12. self.brick = np.array(row)
  13. else:
  14. self.brick = np.vstack((self.brick, row))
  15. def mark(self, mark):
  16. #self.marks[ där self.brick == mark ] = 0
  17. self.marks[self.brick == mark] = False
  18. def bingo(self):
  19. if self.has_bingo:
  20. return True
  21. vert = self.marks.sum(axis = 0) == 0
  22. if vert.any():
  23. self.has_bingo == True
  24. return True
  25. hor = self.marks.sum(axis = 1) == 0
  26. if hor.any():
  27. self.has_bingo == True
  28. return True
  29. return False
  30. def score(self):
  31. remainder = self.brick[self.marks]
  32. score = remainder.sum()
  33. return score
  34. def __str__(self):
  35. result = ''
  36. if self.brick.size > 0:
  37. result = str(self.brick) + '\n'
  38. result += str(self.marks)
  39. else:
  40. result = "Empty brick"
  41. return result
  42. def solve_task(filename):
  43. marks = []
  44. bingo_bricks = []
  45. with open(filename) as infile:
  46. # Read first line of mark announcements
  47. marks_str = infile.readline().rstrip().split(',')
  48. marks = [int(k) for k in marks_str]
  49. # Read the bingo bricks line by line
  50. for raw_line in infile:
  51. line = raw_line.rstrip()
  52. if len(line) == 0: # Empty row separates bingo bricks
  53. bingo_bricks.append(bingo_brick())
  54. else:
  55. bingo_line_str = line.split()
  56. bingo_line = [int(k) for k in bingo_line_str]
  57. bingo_bricks[-1].append_row(bingo_line)
  58. # Time to play some bingo!
  59. # Iterate the mark announcements
  60. losing_brick = None
  61. losing_mark = None
  62. boards_won = 0
  63. for mark in marks:
  64. print("Draws number: " + str(mark))
  65. for brick in bingo_bricks:
  66. if brick.bingo():
  67. continue
  68. brick.mark(mark)
  69. if brick.bingo():
  70. if boards_won == len(bingo_bricks)-1:
  71. print("The last board just got bingo!")
  72. losing_brick = brick
  73. losing_mark = mark
  74. boards_won += 1
  75. if losing_brick:
  76. break
  77. if losing_brick:
  78. print(losing_brick)
  79. print("Brick score: " + str(losing_brick.score()) + "\nLast mark: " + str(losing_mark))
  80. print("Total score: " + str(losing_brick.score() * losing_mark))
  81. os.system("pause")
  82. def parse_arguments():
  83. parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
  84. parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
  85. args = parser.parse_args()
  86. return args
  87. def main():
  88. args = parse_arguments()
  89. solve_task(args.filename)
  90. if __name__ == "__main__":
  91. main()