Solve a couple of 2017 problems.
This commit is contained in:
52
2017/d11.py
Normal file
52
2017/d11.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from lib import add2
|
||||
from math import ceil
|
||||
|
||||
|
||||
DIR = {
|
||||
"n": (-2, 0),
|
||||
"s": (2, 0),
|
||||
"e": (0, 1),
|
||||
"w": (0, -1),
|
||||
"ne": (-1, 1),
|
||||
"nw": (-1, -1),
|
||||
"se": (1, 1),
|
||||
"sw": (1, -1),
|
||||
}
|
||||
|
||||
|
||||
def steps_to_zero(pos: tuple[int, int]):
|
||||
c = list(map(abs, pos))
|
||||
steps = c[1]
|
||||
c[0] -= c[1]
|
||||
if c[0] > 0:
|
||||
steps += ceil(c[0] / 2)
|
||||
return steps
|
||||
|
||||
|
||||
def part_1(data):
|
||||
c = (0, 0)
|
||||
for d in data.split(","):
|
||||
c = add2(c, DIR[d])
|
||||
return steps_to_zero(c)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
c = (0, 0)
|
||||
steps_max = 0
|
||||
for d in data.split(","):
|
||||
c = add2(c, DIR[d])
|
||||
steps_max = max(steps_to_zero(c), steps_max)
|
||||
return steps_max
|
||||
|
||||
|
||||
def main():
|
||||
data = open(0).read().strip()
|
||||
print(part_1(data))
|
||||
print(part_2(data))
|
||||
assert part_1("ne,ne,ne") == 3
|
||||
assert part_1("ne,ne,sw,sw") == 0
|
||||
assert part_1("ne,ne,s,s") == 2
|
||||
assert part_1("se,sw,se,sw,sw") == 3
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user