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,17 +1,9 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up
[2000-01-01 00:00] Guard #1 begins shift
[2000-01-01 00:01] falls asleep
[2000-01-01 00:03] wakes up
[2000-01-02 00:00] Guard #1 begins shift
[2000-01-02 00:02] falls asleep
[2000-01-02 00:04] wakes up
[2000-01-03 00:00] Guard #2 begins shift
[2000-01-03 00:10] falls asleep
[2000-01-03 00:20] wakes up

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])
@@ -95,3 +116,5 @@ 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(format_print(guard_data))

1
2018/day-5/input Normal file
View File

File diff suppressed because one or more lines are too long

35
2018/day-5/python/main.py Normal file
View File

@@ -0,0 +1,35 @@
import os, sys, time, progressbar, string
path = os.path.join(sys.path[0], '..', 'input')
data = open(path, 'r').read()
# data = data[:10000]
data = 'dabAcCaCBAcCcaDA'
def react(string):
look = True
while look:
look = False
for i in range(len(string)-1):
if string[i].lower() == string[i + 1].lower() and string[i] != string[i + 1]:
string = string[:i] + string[i+2:]
look = True
break
return string
# t1 = time.time()
# data = react(data) #11476
# t2 = time.time()
# print('Part 1 Solution: {}'.format(len(data)))
# print('Solved in {}s\n'.format(round(t2 - t1, 3)))
t1 = time.time()
lengths = {}
charset = ''.join(list(set(data)))
for char in progressbar.progressbar(list(charset)):
table = str.maketrans({char : None, char.lower() : None})
lengths[char] = len(react(data.translate(table)))
char, length = min(lengths.items(), key=lambda item : item[1])
t2 = time.time()
print('\nPart 2 Solution: Removing polymer {}{} had the most effect at length {}.'.format(char, char.lower(), length))
print('Solved in {}s\n'.format(round(t2 - t1, 3)))