day17
This commit is contained in:
parent
159ce3ca9f
commit
0a0c829454
44
day17.py
Normal file
44
day17.py
Normal file
@ -0,0 +1,44 @@
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
|
||||
def fire(velX: int, velY: int, minX: int, maxX: int, minY: int, maxY: int) -> (bool, int):
|
||||
highY, posX, posY = 0, velX, velY
|
||||
while not posX > maxX and not posY < minY:
|
||||
if velX > 0:
|
||||
velX -= 1
|
||||
velY -= 1
|
||||
|
||||
posX += velX
|
||||
posY += velY
|
||||
if posY > highY:
|
||||
highY = posY
|
||||
|
||||
hit = minX <= posX - velX <= maxX and minY <= posY - velY <= maxY
|
||||
return hit, highY if hit else minY
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
test_solutions_p1 = [45, 5151]
|
||||
test_solutions_p2 = [112, 968]
|
||||
|
||||
def getBoundaries(self) -> (int, int, int, int):
|
||||
_, ranges = self.getInput().split("=", 1)
|
||||
xRange, yRange = ranges.split(", y=")
|
||||
minX, maxX = map(int, xRange.split(".."))
|
||||
minY, maxY = map(int, yRange.split(".."))
|
||||
return minX, maxX, minY, maxY
|
||||
|
||||
def part1(self) -> Any:
|
||||
minX, maxX, minY, maxY = self.getBoundaries()
|
||||
return max(fire(x, y, minX, maxX, minY, maxY)[1] for x in range(maxX) for y in range(abs(minY)))
|
||||
|
||||
def part2(self) -> Any:
|
||||
minX, maxX, minY, maxY = self.getBoundaries()
|
||||
hitCounter = 0
|
||||
for x in range(maxX + 1):
|
||||
for y in range(minY, abs(minY)):
|
||||
h, _ = fire(x, y, minX, maxX, minY, maxY)
|
||||
hitCounter += h
|
||||
|
||||
return hitCounter
|
||||
1
inputs/input17
Normal file
1
inputs/input17
Normal file
@ -0,0 +1 @@
|
||||
target area: x=135..155, y=-102..-78
|
||||
1
inputs/test_input17_1_0
Normal file
1
inputs/test_input17_1_0
Normal file
@ -0,0 +1 @@
|
||||
target area: x=20..30, y=-10..-5
|
||||
1
inputs/test_input17_1_1
Normal file
1
inputs/test_input17_1_1
Normal file
@ -0,0 +1 @@
|
||||
target area: x=135..155, y=-102..-78
|
||||
1
inputs/test_input17_2_0
Normal file
1
inputs/test_input17_2_0
Normal file
@ -0,0 +1 @@
|
||||
target area: x=20..30, y=-10..-5
|
||||
1
inputs/test_input17_2_1
Normal file
1
inputs/test_input17_2_1
Normal file
@ -0,0 +1 @@
|
||||
target area: x=135..155, y=-102..-78
|
||||
Loading…
Reference in New Issue
Block a user