2019 day 4 and 5

This commit is contained in:
felixm 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()

View File

@ -1,9 +1,8 @@
# aocpy
Solutions and accompanying utility scripts for Advent of Code challenges
written in Python.
Solutions and utility script for Advent of Code challenges in Python.
# 2015
## AoC 2015
- Day 1: 3:10
- Day 2: 5:24 :/
@ -31,7 +30,7 @@ written in Python.
- Day 24: 20:00 ugly - recursive solution would be more elegant
- Day 25: 9:34
# 2016
## AoC 2016
- Day 1: 29:00 That was emberassingly slow. Out of my rhythm?
- Day 2: 13:24 Getting back into it but still slow af, obviously.
@ -59,7 +58,7 @@ written in Python.
- Day 24: 48:00
- Day 25: 6:45
# 2017
## 2017
- Day 1: 7:30
- Day 2: 6:15
@ -87,7 +86,7 @@ written in Python.
- Day 24: 15:45 (48th)
- Day 25: 41:00
# 2018
## AoC 2018
- Day 1: 1:49 (2nd)
- Day 2: 10:53
@ -109,14 +108,16 @@ written in Python.
- Day 18: 24:04
- Day 19:
# 2019
## AoC 2019
- Day 1: 4:25 (copy and paste error)
- Day 2: 7:07
- Day 3: 10:46
- Day 4:
- Day 1: 4:25 (copy and paste error, no leaderboard)
- Day 2: 7:07 (19th)
- Day 3: 10:46 (37th)
- Day 4: 8:48 (just too slow, no leaderboard)
- Day 5: 34:24 (that wasn't hard at all)
- Day 6:
# 2022
## AoC 2022
Done with this. Overall everything is solvable. It's more about consistency
and focus. Of course, learning more algorithms and techniques helps.
@ -164,7 +165,7 @@ Possible to-dos:
if I would have been able to figure it out.
# 2023
## AoC 2023
- Day 1: 40:00 (I don't know what I am doing.)
- Day 2: 14:15 (Okay, but far way from leaderboard.)