b.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import os, argparse
  2. tree = '#'
  3. def hit_function(map, x, y):
  4. wrapped_x = x % len(map[0])
  5. if (map[y][wrapped_x] == tree):
  6. return 1
  7. else:
  8. return 0
  9. def number_of_hits(map, right, down):
  10. trees_hit = 0
  11. stages = len(map)//down
  12. for n in range(stages):
  13. trees_hit += hit_function(map, n*right, n*down)
  14. return trees_hit
  15. def solve_task(filename):
  16. toboggan_slope = []
  17. with open(filename) as infile:
  18. for raw_line in infile:
  19. line = raw_line.rstrip()
  20. toboggan_slope.append(line)
  21. tactics = [(1,1),
  22. (3,1),
  23. (5,1),
  24. (7,1),
  25. (1,2)]
  26. score = 1
  27. for tactic in tactics:
  28. hits = number_of_hits(toboggan_slope, tactic[0], tactic[1])
  29. score *= hits
  30. print(f"Hit {hits} trees")
  31. print(f"Score: {score}")
  32. os.system("pause")
  33. def parse_arguments():
  34. parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
  35. parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
  36. args = parser.parse_args()
  37. return args
  38. def main():
  39. args = parse_arguments()
  40. solve_task(args.filename)
  41. if __name__ == "__main__":
  42. main()