Skip to content

Instantly share code, notes, and snippets.

@ryan-mooore
Created July 3, 2021 05:07
Show Gist options
  • Save ryan-mooore/5088d2aff80e2ca5d1bd9bd122bf5e09 to your computer and use it in GitHub Desktop.
Save ryan-mooore/5088d2aff80e2ca5d1bd9bd122bf5e09 to your computer and use it in GitHub Desktop.
from collections import namedtuple
from math import floor
Coords = namedtuple("Coords", "x y")
Mirror = namedtuple("Mirror", "coords direction")
lightx, lighty, intensity, plantmax = [int(num) for num in input().split(" ")]
plant = range(plantmax + 1)
light = Coords(lightx, lighty)
mirrors = []
for _ in range (int(input())):
i = input()
x, y, direction = [int(num) for num in i.split(" ") if num]
mirror = Mirror(Coords(x, y), direction)
mirrors.append(mirror)
def ray_strike(intensity, is_vertical, is_increasing, source):
if is_vertical:
if is_increasing:
axis_mirrors = [mirror for mirror in mirrors if mirror.coords.x == source.x and mirror.coords.y > source.y]
else:
axis_mirrors = [mirror for mirror in mirrors if mirror.coords.x == source.x and mirror.coords.y < source.y]
else:
if is_increasing:
axis_mirrors = [mirror for mirror in mirrors if mirror.coords.y == source.y and mirror.coords.x > source.x]
else:
axis_mirrors = [mirror for mirror in mirrors if mirror.coords.y == source.y and mirror.coords.x < source.x]
if axis_mirrors:
if is_vertical:
axis_mirrors.sort(key=lambda mirror: mirror.coords.y, reverse=not is_increasing)
else:
axis_mirrors.sort(key=lambda mirror: mirror.coords.x, reverse=not is_increasing)
next_mirror = axis_mirrors[0]
if intensity > 1:
return ray_strike(floor(intensity / 2), not is_vertical, is_increasing if next_mirror.direction == 1 else not is_increasing, next_mirror.coords) + ray_strike(floor(intensity / 2), is_vertical, is_increasing, next_mirror.coords)
else:
return 0
else:
if is_vertical and not is_increasing and source.y in plant:
return intensity
else:
return 0
print(ray_strike(intensity, True, False, light))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment