2020: d02: ex2: add solution
This commit is contained in:
parent
a968844df2
commit
57a2d60dc0
33
2021/d02/ex2/ex2.py
Executable file
33
2021/d02/ex2/ex2.py
Executable file
|
@ -0,0 +1,33 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
import sys
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: List[str]) -> int:
|
||||||
|
x, y, aim = 0, 0, 0
|
||||||
|
|
||||||
|
for instruction in input:
|
||||||
|
dir, length = instruction.split(" ")
|
||||||
|
length = int(length)
|
||||||
|
if dir == "forward":
|
||||||
|
x += length
|
||||||
|
y += length * aim
|
||||||
|
elif dir == "down":
|
||||||
|
aim += length
|
||||||
|
elif dir == "up":
|
||||||
|
aim -= length
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
return x * y
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = [line.strip() for line in sys.stdin.readlines()]
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue