···11import fileinput
22import re
3344-lights = [[0 for x in range(1000)] for x in range(1000)]
44+instructions = []
5566for line in fileinput.input():
77 cmd, x1, y1, x2, y2 = re.match(r'(.+) (\d+),(\d+) through (\d+),(\d+)', line).groups()
···1010 y1 = int(y1)
1111 y2 = int(y2)
12121313- for x in range(x1, x2+1):
1414- for y in range(y1, y2+1):
1515- if cmd == "turn off":
1616- # lights[x][y] = 0
1717- lights[x][y] = max(0, lights[x][y] - 1)
1818- elif cmd == "turn on":
1919- # lights[x][y] = 1
2020- lights[x][y] += 1
2121- elif cmd == "toggle":
2222- # lights[x][y] ^= 1
2323- lights[x][y] += 2
1313+ instructions.append((cmd, x1, x2, y1, y2))
1414+1515+1616+def total_lights(instructions, brightness=False):
1717+ lights = [[0 for x in range(1000)] for x in range(1000)]
24182525-# print "Lights on: %d" % sum([sum(x) for x in lights])
2626-print "Total brightness: %d" % sum([sum(x) for x in lights])
1919+ for cmd, x1, x2, y1, y2 in instructions:
2020+ for x in range(x1, x2+1):
2121+ for y in range(y1, y2+1):
2222+ if cmd == "turn off":
2323+ if brightness:
2424+ lights[x][y] = max(0, lights[x][y] - 1)
2525+ else:
2626+ lights[x][y] = 0
2727+2828+ elif cmd == "turn on":
2929+ if brightness:
3030+ lights[x][y] += 1
3131+ else:
3232+ lights[x][y] = 1
3333+3434+ elif cmd == "toggle":
3535+ if brightness:
3636+ lights[x][y] += 2
3737+ else:
3838+ lights[x][y] ^= 1
3939+4040+ return sum(sum(x) for x in lights)
4141+4242+4343+print "Lights on: %d" % total_lights(instructions)
4444+print "Total brightness: %d" % total_lights(instructions, brightness=True)
+7-7
2015/day20.py
···66 return set(x for tup in ([i, n//i]
77 for i in range(1, int(n**0.5)+1) if n % i == 0) for x in tup)
8899-# house = 1
99+house = 1
10101111-# while True:
1212-# if sum(factors(house)) * 10 >= PRESENTS:
1313-# print house
1414-# break
1515-# house += 1
1111+while True:
1212+ if sum(factors(house)) * 10 >= PRESENTS:
1313+ print "Lowest house number #1:", house
1414+ break
1515+ house += 1
16161717house = 1
18181919while True:
2020 if sum(x for x in factors(house) if (x * 50 >= house)) * 11 >= PRESENTS:
2121- print house
2121+ print "Lowest house number #2:", house
2222 break
2323 house += 1
+20-18
2015/day22.py
···152152# for line in fileinput.input():
153153# boss_data.append(int(line.split(': ')[1]))
154154155155-MOST_EFFICIENT = sys.maxint
156156-157155# boss = Character('BOSS', 13, 0, 8, 0)
158156# player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP
159157# boss_fight(player, boss, [SPELLS[3], SPELLS[0]])
160158161159# sys.exit()
162160163163-for n in range(9, 12):
164164- found = False
165165- for so in itertools.product(SPELLS, repeat=n):
166166- if sum(s.cost for s in so) > MOST_EFFICIENT:
167167- continue
161161+def simulate(hard_mode):
162162+ most_efficient = sys.maxint
168163169169- boss = Character('BOSS', 55, 0, 8, 0) # 55hp, 8dmg INPUT
170170- player = Character('iKevinY', 50, 500, 0, 0) # 50HP, 500MP
164164+ for n in range(9, 12):
165165+ found = False
166166+ for so in itertools.product(SPELLS, repeat=n):
167167+ if sum(s.cost for s in so) > most_efficient:
168168+ continue
171169172172- # boss = Character('BOSS', 13, 0, 8, 0)
173173- # player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP
174174- a = boss_fight(player, boss, so, hard_mode=True)
170170+ boss = Character('BOSS', 55, 0, 8, 0) # 55hp, 8dmg INPUT
171171+ player = Character('iKevinY', 50, 500, 0, 0) # 50HP, 500MP
175172176176- if a < MOST_EFFICIENT:
177177- MOST_EFFICIENT = a
178178- found = True
173173+ # boss = Character('BOSS', 13, 0, 8, 0)
174174+ # player = Character('iKevinY', 10, 250, 0, 0) # 50HP, 500MP
175175+ a = boss_fight(player, boss, so, hard_mode)
176176+177177+ if a < most_efficient:
178178+ most_efficient = a
179179+ found = True
179180180180- if found:
181181- break
181181+ if found:
182182+ return most_efficient
182183183183-print 'Most efficient MP: %i' % MOST_EFFICIENT
184184+print 'Most efficient MP on easy mode: %i' % simulate(hard_mode=False)
185185+print 'Most efficient MP on hard mode: %i' % simulate(hard_mode=True)