2019 day 4 and 5

This commit is contained in:
2024-08-02 21:16:46 -04:00
parent b0e2334427
commit 9718165d7b
3 changed files with 207 additions and 13 deletions

70
2019/d4.py Normal file
View File

@@ -0,0 +1,70 @@
from lib import get_data, str_to_ints
def in_range_1(x):
contains_double = False
s = str(x)
for i in range(len(s) - 1):
if s[i] == s[i + 1]:
contains_double = True
break
if not contains_double:
return False
for i in range(len(s) - 1):
if ord(s[i]) > ord(s[i + 1]):
return False
return True
def in_range_2(x):
contains_double = False
s = str(x)
for i in range(len(s) - 1):
if s[i] == s[i + 1]:
if i + 2 < len(s):
if s[i + 2] == s[i + 1]:
continue
if i - 1 >= 0:
if s[i - 1] == s[i]:
continue
contains_double = True
if not contains_double:
return False
for i in range(len(s) - 1):
if ord(s[i]) > ord(s[i + 1]):
return False
return True
def part_1(data):
a, b = str_to_ints(data)
b = -b
r = 0
for x in range(a, b + 1):
if in_range_1(x):
r += 1
print(r)
def part_2(data):
a, b = str_to_ints(data)
b = -b
r = 0
for x in range(a, b + 1):
if in_range_2(x):
r += 1
print(r)
def main():
data = get_data(__file__)
part_1(data)
part_2(data)
if __name__ == "__main__":
main()

123
2019/d5.py Normal file
View File

@@ -0,0 +1,123 @@
from lib import get_data, str_to_ints
def part_1(data):
xs = str_to_ints(data)
i = 0
while i < len(xs):
inst = str(xs[i])
inst = "0" * (5 - len(inst)) + inst
assert len(inst) == 5
op = int(inst[3:5])
mode_p1 = int(inst[2])
mode_p2 = int(inst[1])
mode_p3 = int(inst[0])
match op:
case 1:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
xs[xs[i + 3]] = p1 + p2
i += 4
case 2:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
xs[xs[i + 3]] = p1 * p2
i += 4
case 3:
print("input", i, 1)
assert mode_p1 == 0
xs[xs[i + 1]] = 1
i += 2
case 4:
if mode_p1 == 0:
v = xs[xs[i + 1]]
else:
v = xs[i + 1]
print("output", v)
i += 2
case 99:
break
def part_2(data):
xs = str_to_ints(data)
i = 0
while i < len(xs):
inst = str(xs[i])
inst = "0" * (5 - len(inst)) + inst
assert len(inst) == 5
op = int(inst[3:5])
mode_p1 = int(inst[2])
mode_p2 = int(inst[1])
mode_p3 = int(inst[0])
match op:
case 1:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
xs[xs[i + 3]] = p1 + p2
i += 4
case 2:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
xs[xs[i + 3]] = p1 * p2
i += 4
case 3:
print("input", i, 5)
assert mode_p1 == 0
xs[xs[i + 1]] = 5
i += 2
case 4:
if mode_p1 == 0:
v = xs[xs[i + 1]]
else:
v = xs[i + 1]
print("output", v)
i += 2
case 99:
break
case 5:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
if p1 != 0:
i = p2
else:
i += 3
case 6:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
if p1 == 0:
i = p2
else:
i += 3
case 7:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
if p1 < p2:
xs[xs[i + 3]] = 1
else:
xs[xs[i + 3]] = 0
i += 4
case 8:
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
assert mode_p3 == 0
if p1 == p2:
xs[xs[i + 3]] = 1
else:
xs[xs[i + 3]] = 0
i += 4
def main():
data = get_data(__file__)
part_1(data)
part_2(data)
if __name__ == "__main__":
main()