day 4 formatting and day 5 part 1 & part 2

This commit is contained in:
Xevion
2019-07-26 21:22:16 -06:00
parent 78b43513a7
commit d85feead86
4 changed files with 73 additions and 22 deletions

View File

@@ -1,12 +1,12 @@
import time, re, datetime, pprint, process_input
import time, re, datetime, pprint, string, process_input
# Returns processed input, simple script for creating a chronologically ordered input file.
data = process_input.process()
# Comment this out to use real puzzle data
# import sys, os
# path = os.path.join(sys.path[0], '..', 'processed_input')
# data = open(path, 'r').read().split('\n')
import sys, os
path = os.path.join(sys.path[0], '..', 'processed_input')
data = open(path, 'r').read().split('\n')
# Pattern Constants & pprint
pprint = pprint.PrettyPrinter(width=1000).pprint
@@ -83,6 +83,27 @@ for line in data:
current_action = action
last_timechange = time
charset = string.digits + string.ascii_lowercase + string.ascii_uppercase
def format_print(guard_data):
table = []
# Header parts
minutes = map(lambda num : str(num).zfill(2), range(60))
minutes = list(map(lambda num : (num[0], num[1]), minutes))
top, bottom = ' '.join(minutes[i][0] for i in range(len(minutes))), ' '.join(minutes[i][1] for i in range(len(minutes)))
headerp1 = ["ID".ljust(6), "Total", top]
table.append(' | '.join(headerp1))
table.append(bottom.rjust(len(table[0])))
guard_data = sorted(guard_data.guards.items(), key=lambda item : item[0])
for guard in guard_data:
row = [
str(str(guard[0]).ljust(6)),
str(guard[1]['total']).ljust(5),
' '.join(['.' if minute == 0 else charset[minute % len(charset)] for minute in guard[1]['minutes']])
]
row[2] = row[2].rjust(len(bottom))
table.append(' '.join(row))
return '\n'.join(table)
id, total = guard_data.best_total()
minute, minute_count = guard_data.guard_best_minute(guard_data.best_total()[0])
@@ -94,4 +115,6 @@ id, (minute, minute_count) = guard_data.best_minute()
print(f'Guard {id} spent minute {minute} the most @ {minute_count} times.')
p2 = id * minute
print(f'\nPart 1 solution: {p1}\nPart 2 solution: {p2}')
print(f'\nPart 1 solution: {p1}\nPart 2 solution: {p2}')
print(format_print(guard_data))