2019 day 2 and 3
This commit is contained in:
parent
29a556fd54
commit
b0e2334427
@ -21,7 +21,6 @@ def part_2(data):
|
||||
print(r)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
|
41
2019/d2.py
Normal file
41
2019/d2.py
Normal file
@ -0,0 +1,41 @@
|
||||
from lib import get_data, str_to_ints
|
||||
|
||||
|
||||
def part_1(data):
|
||||
xs = str_to_ints(data)
|
||||
xs[1] = 12
|
||||
xs[2] = 2
|
||||
for i in range(0, len(xs), 4):
|
||||
match xs[i]:
|
||||
case 1:
|
||||
xs[xs[i + 3]] = xs[xs[i + 1]] + xs[xs[i + 2]]
|
||||
case 2:
|
||||
xs[xs[i + 3]] = xs[xs[i + 1]] * xs[xs[i + 2]]
|
||||
print(xs[0])
|
||||
|
||||
|
||||
def part_2(data):
|
||||
for noun in range(1, 101):
|
||||
for verb in range(1, 101):
|
||||
xs = str_to_ints(data)
|
||||
xs[1] = noun
|
||||
xs[2] = verb
|
||||
for i in range(0, len(xs), 4):
|
||||
match xs[i]:
|
||||
case 1:
|
||||
xs[xs[i + 3]] = xs[xs[i + 1]] + xs[xs[i + 2]]
|
||||
case 2:
|
||||
xs[xs[i + 3]] = xs[xs[i + 1]] * xs[xs[i + 2]]
|
||||
if xs[0] == 19690720:
|
||||
print(100 * noun + verb)
|
||||
return
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
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()
|
Loading…
Reference in New Issue
Block a user