Added 2018 day4, 2015 some days

This commit is contained in:
Luke Hubmayer-Werner 2018-12-04 15:57:16 +10:30
parent 2d5d718b78
commit 67be7b5b4c
11 changed files with 1379 additions and 0 deletions

View File

@ -13,3 +13,23 @@ def parse_float(val):
with open('day12-input', 'r') as file:
data = json.load(file, parse_float=parse_float, parse_int=parse_int)
print(number_sum) # Part 1
number_sum_2 = 0.0
def count(obj):
if isinstance(obj, dict):
if 'red' in obj.values() or 'red' in obj.keys():
return
for i in obj.keys():
count(i)
for i in obj.values():
count(i)
elif isinstance(obj, list):
for i in obj:
count(i)
elif not isinstance(obj, str):
global number_sum_2
number_sum_2 += obj
for d in data:
count(d)
print(number_sum_2) # Part 2

56
2015/day13-input Normal file
View File

@ -0,0 +1,56 @@
Alice would lose 57 happiness units by sitting next to Bob.
Alice would lose 62 happiness units by sitting next to Carol.
Alice would lose 75 happiness units by sitting next to David.
Alice would gain 71 happiness units by sitting next to Eric.
Alice would lose 22 happiness units by sitting next to Frank.
Alice would lose 23 happiness units by sitting next to George.
Alice would lose 76 happiness units by sitting next to Mallory.
Bob would lose 14 happiness units by sitting next to Alice.
Bob would gain 48 happiness units by sitting next to Carol.
Bob would gain 89 happiness units by sitting next to David.
Bob would gain 86 happiness units by sitting next to Eric.
Bob would lose 2 happiness units by sitting next to Frank.
Bob would gain 27 happiness units by sitting next to George.
Bob would gain 19 happiness units by sitting next to Mallory.
Carol would gain 37 happiness units by sitting next to Alice.
Carol would gain 45 happiness units by sitting next to Bob.
Carol would gain 24 happiness units by sitting next to David.
Carol would gain 5 happiness units by sitting next to Eric.
Carol would lose 68 happiness units by sitting next to Frank.
Carol would lose 25 happiness units by sitting next to George.
Carol would gain 30 happiness units by sitting next to Mallory.
David would lose 51 happiness units by sitting next to Alice.
David would gain 34 happiness units by sitting next to Bob.
David would gain 99 happiness units by sitting next to Carol.
David would gain 91 happiness units by sitting next to Eric.
David would lose 38 happiness units by sitting next to Frank.
David would gain 60 happiness units by sitting next to George.
David would lose 63 happiness units by sitting next to Mallory.
Eric would gain 23 happiness units by sitting next to Alice.
Eric would lose 69 happiness units by sitting next to Bob.
Eric would lose 33 happiness units by sitting next to Carol.
Eric would lose 47 happiness units by sitting next to David.
Eric would gain 75 happiness units by sitting next to Frank.
Eric would gain 82 happiness units by sitting next to George.
Eric would gain 13 happiness units by sitting next to Mallory.
Frank would gain 77 happiness units by sitting next to Alice.
Frank would gain 27 happiness units by sitting next to Bob.
Frank would lose 87 happiness units by sitting next to Carol.
Frank would gain 74 happiness units by sitting next to David.
Frank would lose 41 happiness units by sitting next to Eric.
Frank would lose 99 happiness units by sitting next to George.
Frank would gain 26 happiness units by sitting next to Mallory.
George would lose 63 happiness units by sitting next to Alice.
George would lose 51 happiness units by sitting next to Bob.
George would lose 60 happiness units by sitting next to Carol.
George would gain 30 happiness units by sitting next to David.
George would lose 100 happiness units by sitting next to Eric.
George would lose 63 happiness units by sitting next to Frank.
George would gain 57 happiness units by sitting next to Mallory.
Mallory would lose 71 happiness units by sitting next to Alice.
Mallory would lose 28 happiness units by sitting next to Bob.
Mallory would lose 10 happiness units by sitting next to Carol.
Mallory would gain 44 happiness units by sitting next to David.
Mallory would gain 22 happiness units by sitting next to Eric.
Mallory would gain 79 happiness units by sitting next to Frank.
Mallory would lose 16 happiness units by sitting next to George.

31
2015/day13.py Normal file
View File

@ -0,0 +1,31 @@
with open('day13-input', 'r') as file:
data = [l.strip('\n') for l in file]
from itertools import permutations
guests = set()
points = {}
for line in data:
guest1, _, sign, point, _, _, _, _, _, _, guest2 = line.rstrip('.').split(' ')
guests.add(guest1)
guests.add(guest2)
mul = -1 if sign == 'lose' else +1
points[(guest1, guest2)] = mul*int(point)
route_points = []
for route in permutations(guests):
route_points.append(sum([points[b, a]+points[b, c] for a, b, c in zip(route, route[1:]+route[:1], route[2:]+route[:2])]))
print(max(route_points)) # Part 1
guests.add('Me')
def get_points(guest1, guest2):
if guest1 == 'Me' or guest2 == 'Me':
return 0
else:
return points[(guest1, guest2)]
route_points_2 = []
for route in permutations(guests):
route_points_2.append(sum([get_points(b, a)+get_points(b, c) for a, b, c in zip(route, route[1:]+route[:1], route[2:]+route[:2])]))
print(max(route_points_2)) # Part 2

9
2015/day14-input Normal file
View File

@ -0,0 +1,9 @@
Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds.
Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds.
Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds.
Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds.
Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds.
Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds.
Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds.
Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds.
Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds.

28
2015/day14.py Normal file
View File

@ -0,0 +1,28 @@
with open('day14-input', 'r') as file:
data = [l.strip('\n') for l in file]
def deer(line):
name, _, _, speed, _, _, time, _, _, _, _, _, _, rest, _ = line.split()
return name, (int(speed), int(time), int(rest))
deers = {k:v for k, v in [deer(line) for line in data]}
def avg_speed(speed, time, rest):
return (speed*time)/(time+rest)
def distance(t, speed, time, rest):
period = time + rest
periods, remainder = divmod(t, period)
distance = periods * speed * time
distance += min(time, remainder) * speed
return distance
print(max([distance(2503, *d) for d in deers.values()])) # Part 1
deerpoints = [0 for k in deers.keys()]
deerstats = [d for d in deers.values()]
for t in range(1, 2504):
deer_ds = [distance(t, *d) for d in deerstats]
lead_d = max(deer_ds)
for i, d in enumerate(deer_ds):
if d == lead_d:
deerpoints[i] += 1
print(max(deerpoints)) # Part 2

4
2015/day15-input Normal file
View File

@ -0,0 +1,4 @@
Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2
Sprinkles: capacity -3, durability 3, flavor 0, texture 0, calories 9
Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1
Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8

20
2015/day17-input Normal file
View File

@ -0,0 +1,20 @@
50
44
11
49
42
46
18
32
26
40
21
7
18
43
10
47
36
24
22
40

19
2015/day17.py Normal file
View File

@ -0,0 +1,19 @@
with open('day17-input', 'r') as file:
data = [int(l.strip('\n')) for l in file]
from itertools import combinations
solutions = 0
solution_sets = []
for n in range(1, len(data)):
for c in combinations(data, n):
if sum(c) == 150:
solutions += 1
solution_sets.append(c)
print(solutions) # Part 1
min_containers = min([len(c) for c in solution_sets])
min_c_solutions = 0
for c in solution_sets:
if len(c) == min_containers:
min_c_solutions += 1
print(min_c_solutions) # Part 2

8
2015/day20.py Normal file
View File

@ -0,0 +1,8 @@
for i in range(1, 500001):
presents = 0
for n in range(1, i+1):
if i%n == 0:
presents += 10*n
if presents >= 33100000:
print(i, presents)
break

1131
2018/day4-input Normal file

File diff suppressed because it is too large Load Diff

53
2018/day4.py Normal file
View File

@ -0,0 +1,53 @@
with open('day4-input', 'r') as file:
data = sorted([l.strip('\n') for l in file])
import numpy as np
GUARDs = {}
GUARD = None
GUARD_asleep = False
GUARD_sleeptime = 0
def parse(line):
global GUARD, GUARD_asleep, GUARD_sleeptime
date, time, *event = line.split()
date = date.lstrip('[')
time = time.rstrip(']')
minute = int(time.partition(':')[2])
if event[0] == 'Guard':
if GUARD_asleep and GUARD:
GUARDs[GUARD][GUARD_sleeptime:minute] += 1
GUARD = int(event[1].lstrip('#'))
if GUARD not in GUARDs:
GUARDs[GUARD] = np.zeros((60))
GUARD_asleep = False
elif event[0] == 'wakes':
GUARD_asleep = False
if GUARD:
GUARDs[GUARD][GUARD_sleeptime:minute] += 1
else: # falls asleep
GUARD_asleep = True
GUARD_sleeptime = minute
for line in data:
parse(line)
max_mins = 0
max_g = None
for k, v in GUARDs.items():
val = v.sum()
if val > max_mins:
max_mins = val
max_g = k
biggest_minute = GUARDs[max_g].argmax()
print(max_g*biggest_minute) # Part 1
max_mins_2 = 0
max_g_2 = None
for k, v in GUARDs.items():
val = v.max()
if val > max_mins_2:
max_mins_2 = val
max_g_2 = k
print(max_g_2*GUARDs[max_g_2].argmax()) # Part 1