Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 47:21 | 8:50 | 56:11 |
Another long one for me which is frustrating. Andrew Kritzler, my nemesis in the NJIT ACM leaderboard, completed Part 1 in 12:03 and Part 2 in 5:11. He's too good!
Unfortunately, I failed to handle the cases where the recursive input didn't have any conclusive output. For example, if the lists ran out at the same time, I was returning True
, completely ignoring whatever came next in the checks. I switched from using booleans to -1, 0, and 1
representing False, Inconclusive, and True
respectively. If one of the recursive calls is inconclusive, just keep on going!
Took me really long to try that. In my mind, I knew that I needed to do something like that from really early on, I think I had the bulk of it done in 5 minutes (thanks eval
!) but I was too scared to try it. This has cost me time yesterday as well, and it's a trend I'd like to break.
from helpers.datagetter import data_in, submit
ans = 0
din = data_in(split=True, numbers=False)
def check(left, right):
for i in range(max(len(left), len(right))):
# When lists run out
if i >= len(left) and i < len(right):
return 1
elif i < len(left) and i >= len(right):
return -1
elif i >= len(left) and i >= len(right):
return 0
# When comparing ints
if type(left[i]) is int and type(right[i]) is int:
if left[i] < right[i]:
return 1
elif left[i] > right[i]:
return -1
# When comparing lists
if type(left[i]) is list and type(right[i]) is list:
ch = check(left[i], right[i])
if ch != 0:
return ch
# When comparing one int and one list
if type(left[i]) is int and type(right[i]) is list:
ch = check([left[i]], right[i])
if ch != 0:
return ch
if type(left[i]) is list and type(right[i]) is int:
ch = check(left[i], [right[i]])
if ch != 0:
return ch
# If all tests are inconclusive, return 0
return 0
for line in range(0, len(din), 3):
left = eval(din[line])
right = eval(din[line+1])
if check(left, right) == 1:
ans += (line // 3) + 1
print(ans)
submit(ans)
Simple insertion sort.
# ADD THIS AFTER FUNCTION DEFINITION
packets = []
for line in range(0, len(din), 3):
packets.append(eval(din[line]))
packets.append(eval(din[line+1]))
packet_list = [[[2]], [[6]]]
# Insertion sort
for packet in packets:
for i in range(len(packet_list)):
if check(packet, packet_list[i]) == 1:
packet_list.insert(i, packet)
break
packet_list.append(packet)
ans = (packet_list.index([[2]])+1) * (packet_list.index([[6]])+1)
print(ans)
submit(ans)