2019 day 2 and 3
This commit is contained in:
57
2019/d3.py
Normal file
57
2019/d3.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from lib import get_data, add2
|
||||
|
||||
|
||||
DIRS = {"R": (0, 1), "U": (-1, 0), "D": (1, 0), "L": (0, -1)}
|
||||
|
||||
|
||||
def part_1(data):
|
||||
xss = []
|
||||
for path in data.splitlines():
|
||||
xs = [(0, 0)]
|
||||
for word in path.split(","):
|
||||
dir = DIRS[word[0]]
|
||||
x = int(word[1:])
|
||||
for _ in range(x):
|
||||
xs.append(add2(xs[-1], dir))
|
||||
xss.append(xs)
|
||||
ys = set(xss[0]) & set(xss[1])
|
||||
ys.remove((0, 0))
|
||||
min_dist = 10**9
|
||||
for y in ys:
|
||||
d = abs(y[0]) + abs(y[1])
|
||||
min_dist = min(min_dist, d)
|
||||
print(min_dist)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
xss = []
|
||||
for path in data.splitlines():
|
||||
pos = (0, 0)
|
||||
steps = 0
|
||||
xs = {}
|
||||
for word in path.split(","):
|
||||
dir = DIRS[word[0]]
|
||||
x = int(word[1:])
|
||||
for _ in range(x):
|
||||
steps += 1
|
||||
pos = add2(pos, dir)
|
||||
if not pos in xs:
|
||||
xs[pos] = steps
|
||||
xss.append(xs)
|
||||
|
||||
min_steps = 10**9
|
||||
for x in xss[0].keys():
|
||||
if x in xss[1]:
|
||||
steps = xss[0][x] + xss[1][x]
|
||||
min_steps = min(steps, min_steps)
|
||||
print(min_steps)
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user