My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix miscellaneous bugs with 2015 solutions

+60 -41
+1 -2
2015/day03.py
··· 1 1 import fileinput 2 - from collections import namedtuple 3 2 4 3 DIRS = { 5 4 "^": (0, 1), ··· 14 13 yield house 15 14 house = tuple(map(sum, zip(house, DIRS[c]))) 16 15 17 - path = fileinput.input()[0].strip()) 16 + path = fileinput.input()[0].strip() 18 17 19 18 year_1_houses = set(visit_houses(path)) 20 19 year_2_houses = set(visit_houses(path[::2])) | set(visit_houses(path[1::2]))
+32 -14
2015/day06.py
··· 1 1 import fileinput 2 2 import re 3 3 4 - lights = [[0 for x in range(1000)] for x in range(1000)] 4 + instructions = [] 5 5 6 6 for line in fileinput.input(): 7 7 cmd, x1, y1, x2, y2 = re.match(r'(.+) (\d+),(\d+) through (\d+),(\d+)', line).groups() ··· 10 10 y1 = int(y1) 11 11 y2 = int(y2) 12 12 13 - for x in range(x1, x2+1): 14 - for y in range(y1, y2+1): 15 - if cmd == "turn off": 16 - # lights[x][y] = 0 17 - lights[x][y] = max(0, lights[x][y] - 1) 18 - elif cmd == "turn on": 19 - # lights[x][y] = 1 20 - lights[x][y] += 1 21 - elif cmd == "toggle": 22 - # lights[x][y] ^= 1 23 - lights[x][y] += 2 13 + instructions.append((cmd, x1, x2, y1, y2)) 14 + 15 + 16 + def total_lights(instructions, brightness=False): 17 + lights = [[0 for x in range(1000)] for x in range(1000)] 24 18 25 - # print "Lights on: %d" % sum([sum(x) for x in lights]) 26 - print "Total brightness: %d" % sum([sum(x) for x in lights]) 19 + for cmd, x1, x2, y1, y2 in instructions: 20 + for x in range(x1, x2+1): 21 + for y in range(y1, y2+1): 22 + if cmd == "turn off": 23 + if brightness: 24 + lights[x][y] = max(0, lights[x][y] - 1) 25 + else: 26 + lights[x][y] = 0 27 + 28 + elif cmd == "turn on": 29 + if brightness: 30 + lights[x][y] += 1 31 + else: 32 + lights[x][y] = 1 33 + 34 + elif cmd == "toggle": 35 + if brightness: 36 + lights[x][y] += 2 37 + else: 38 + lights[x][y] ^= 1 39 + 40 + return sum(sum(x) for x in lights) 41 + 42 + 43 + print "Lights on: %d" % total_lights(instructions) 44 + print "Total brightness: %d" % total_lights(instructions, brightness=True)
+7 -7
2015/day20.py
··· 6 6 return set(x for tup in ([i, n//i] 7 7 for i in range(1, int(n**0.5)+1) if n % i == 0) for x in tup) 8 8 9 - # house = 1 9 + house = 1 10 10 11 - # while True: 12 - # if sum(factors(house)) * 10 >= PRESENTS: 13 - # print house 14 - # break 15 - # house += 1 11 + while True: 12 + if sum(factors(house)) * 10 >= PRESENTS: 13 + print "Lowest house number #1:", house 14 + break 15 + house += 1 16 16 17 17 house = 1 18 18 19 19 while True: 20 20 if sum(x for x in factors(house) if (x * 50 >= house)) * 11 >= PRESENTS: 21 - print house 21 + print "Lowest house number #2:", house 22 22 break 23 23 house += 1
+20 -18
2015/day22.py
··· 152 152 # for line in fileinput.input(): 153 153 # boss_data.append(int(line.split(': ')[1])) 154 154 155 - MOST_EFFICIENT = sys.maxint 156 - 157 155 # boss = Character('BOSS', 13, 0, 8, 0) 158 156 # player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP 159 157 # boss_fight(player, boss, [SPELLS[3], SPELLS[0]]) 160 158 161 159 # sys.exit() 162 160 163 - for n in range(9, 12): 164 - found = False 165 - for so in itertools.product(SPELLS, repeat=n): 166 - if sum(s.cost for s in so) > MOST_EFFICIENT: 167 - continue 161 + def simulate(hard_mode): 162 + most_efficient = sys.maxint 168 163 169 - boss = Character('BOSS', 55, 0, 8, 0) # 55hp, 8dmg INPUT 170 - player = Character('iKevinY', 50, 500, 0, 0) # 50HP, 500MP 164 + for n in range(9, 12): 165 + found = False 166 + for so in itertools.product(SPELLS, repeat=n): 167 + if sum(s.cost for s in so) > most_efficient: 168 + continue 171 169 172 - # boss = Character('BOSS', 13, 0, 8, 0) 173 - # player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP 174 - a = boss_fight(player, boss, so, hard_mode=True) 170 + boss = Character('BOSS', 55, 0, 8, 0) # 55hp, 8dmg INPUT 171 + player = Character('iKevinY', 50, 500, 0, 0) # 50HP, 500MP 175 172 176 - if a < MOST_EFFICIENT: 177 - MOST_EFFICIENT = a 178 - found = True 173 + # boss = Character('BOSS', 13, 0, 8, 0) 174 + # player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP 175 + a = boss_fight(player, boss, so, hard_mode) 176 + 177 + if a < most_efficient: 178 + most_efficient = a 179 + found = True 179 180 180 - if found: 181 - break 181 + if found: 182 + return most_efficient 182 183 183 - print 'Most efficient MP: %i' % MOST_EFFICIENT 184 + print 'Most efficient MP on easy mode: %i' % simulate(hard_mode=False) 185 + print 'Most efficient MP on hard mode: %i' % simulate(hard_mode=True)