Compare commits

...

48 commits

Author SHA1 Message Date
0b4080c73a 2018: d12: ex2: add solution 2024-12-29 22:03:50 -05:00
20261f9b62 2018: d12: ex2: add input 2024-12-29 22:03:37 -05:00
a128018314 2018: d12: ex1: add solution 2024-12-29 22:03:31 -05:00
9d1029ba74 2018: d12: ex1: add input 2024-12-29 22:03:18 -05:00
264b4f325d 2018: d11: ex2: add solution 2024-12-29 20:55:43 -05:00
17d1ad9419 2018: d11: ex2: add input 2024-12-29 20:55:20 -05:00
0b5bf6596c 2018: d11: ex1: add solution 2024-12-29 20:55:14 -05:00
f1d9cad493 2018: d11: ex1: add input 2024-12-29 20:55:00 -05:00
ec598508d3 2018: d10: ex2: add solution 2024-12-29 20:03:09 -05:00
103875f37e 2018: d10: ex2: add input 2024-12-29 20:03:05 -05:00
e2fabb127d 2018: d10: ex1: add solution 2024-12-29 20:03:00 -05:00
23a1ec49d2 2018: d10: ex1: add input 2024-12-29 20:02:55 -05:00
e9a904a960 2018: d09: ex2: add solution 2024-12-29 19:40:37 -05:00
c282de0062 2018: d09: ex2: add input 2024-12-29 19:40:32 -05:00
5cb92a87d9 2018: d09: ex1: add solution 2024-12-29 19:40:26 -05:00
60fc57c011 2018: d09: ex1: add input 2024-12-29 19:40:21 -05:00
ecdd1e15ec 2018: d08: ex2: add solution 2024-12-29 18:20:00 -05:00
6537d437ad 2018: d08: ex2: add input 2024-12-29 18:19:49 -05:00
0d4e074483 2018: d08: ex1: add solution 2024-12-29 18:19:44 -05:00
398251b014 2018: d08: ex1: add input 2024-12-29 18:19:38 -05:00
74c97e2b2f 2018: d07: ex2: add solution 2024-12-29 17:54:43 -05:00
2d8d834e7d 2018: d07: ex2: add input 2024-12-29 17:54:39 -05:00
4d421694f7 2018: d07: ex1: add solution 2024-12-29 17:54:29 -05:00
024ec4e111 2018: d07: ex1: add input 2024-12-29 17:54:24 -05:00
13f61cb9e9 2018: d06: ex2: add solution 2024-12-29 16:42:57 -05:00
5b7641dc6d 2018: d06: ex2: add input 2024-12-29 16:42:43 -05:00
eaa88b2e39 2018: d06: ex1: add solution 2024-12-29 16:42:36 -05:00
55eeb0f1ae 2018: d06: ex1: add input 2024-12-29 16:42:28 -05:00
28b6b5ee46 2018: d05: ex2: add solution 2024-12-29 16:10:49 -05:00
a62cc81ee4 2018: d05: ex2: add input 2024-12-29 16:10:33 -05:00
911e0aa6a2 2018: d05: ex1: add solution 2024-12-29 16:10:28 -05:00
51fe4bd802 2018: d05: ex1: add input 2024-12-29 16:09:39 -05:00
038c4830dc 2018: d04: ex2: add solution 2024-12-29 15:54:22 -05:00
74ebba0d4c 2018: d04: ex2: add input 2024-12-29 15:54:08 -05:00
b8ed1d773d 2018: d04: ex1: add solution 2024-12-29 15:54:02 -05:00
7e43ba67b7 2018: d04: ex1: add input 2024-12-29 15:53:57 -05:00
ac1b5df0b6 2018: d03: ex2: add solution 2024-12-29 15:26:28 -05:00
d084632fe9 2018: d03: ex2: add input 2024-12-29 15:26:16 -05:00
a035967ba6 2018: d03: ex1: add solution 2024-12-29 15:26:09 -05:00
22e21e5c88 2018: d03: ex1: add input 2024-12-29 15:26:05 -05:00
1f4c687139 2018: d02: ex2: add solution 2024-12-29 14:43:50 -05:00
ebea4a5f08 2018: d02: ex2: add input 2024-12-29 14:43:32 -05:00
7eeab208b0 2018: d02: ex1: add solution 2024-12-29 14:43:26 -05:00
8da9ad7eb6 2018: d02: ex1: add input 2024-12-29 14:43:20 -05:00
1cf545aa1a 2018: d01: ex2: add solution 2024-12-29 14:32:45 -05:00
53c6dcdb2a 2018: d01: ex2: add input 2024-12-29 14:32:39 -05:00
54895fc874 2018: d01: ex1: add solution 2024-12-29 14:32:33 -05:00
dae2865c13 2018: d01: ex1: add input 2024-12-29 14:32:28 -05:00
48 changed files with 9787 additions and 0 deletions

20
2018/d01/ex1/ex1.py Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env python
import sys
def solve(input: str) -> int:
def parse(input: list[str]) -> list[int]:
return [int(n) for n in input]
deltas = parse(input.splitlines())
return sum(deltas)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1033
2018/d01/ex1/input Normal file

File diff suppressed because it is too large Load diff

31
2018/d01/ex2/ex2.py Executable file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env python
import itertools
import sys
def solve(input: str) -> int:
def parse(input: list[str]) -> list[int]:
return [int(n) for n in input]
def find_duplicate(deltas: list[int]) -> int:
start = 0
seen = {start}
for delta in itertools.cycle(deltas):
start += delta
if start in seen:
return start
seen.add(start)
assert False # Sanity check
deltas = parse(input.splitlines())
return find_duplicate(deltas)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1033
2018/d01/ex2/input Normal file

File diff suppressed because it is too large Load diff

24
2018/d02/ex1/ex1.py Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env python
import sys
from collections import Counter
def solve(input: str) -> int:
def any_letter(boxes: list[str], count: int) -> int:
return sum(count in Counter(box).values() for box in boxes)
def checksum(boxes: list[str]) -> int:
return any_letter(boxes, 2) * any_letter(boxes, 3)
boxes = input.splitlines()
return checksum(boxes)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

250
2018/d02/ex1/input Normal file
View file

@ -0,0 +1,250 @@
nkucgflathzwwijtrevymbdplq
nkucgflathzwsidxrlvymbdpiq
nkucgjlathqwsijxrevymbypoq
nkucgflarrzwsmjxrevymbdpoq
nkucgflzthtwsijxrevymbdpjq
nkucgflahhzwskjxrevymbgpoq
bkycgflathzwsijxrsvymbdpoq
nkucgflathzwsijxdevymbdmog
nkucgflaehzwsmjxrevymbdpow
nkucgflathzwsijxrevwmbdnbq
nkucgflathzssijxrevynbdqoq
ngucgflathzwsijxsevymndpoq
nfucgflathzvsijxrevymbspoq
nkucgflwthzwsijxreeymbdpkq
nkucgflpthzwsijxrevdmbdpoe
nkungflatuzwsijurevymbdpoq
nkucgflathzwsiqxrevyybdpom
nkucgflathzwsicxrevtmbtpoq
nkucgfladhzwsijxreuymbdboq
nkumgflathznsijxzevymbdpoq
nkuagflethzwsijxrqvymbdpoq
nkucgflatozwhijxrevymbdpkq
nkuggflathzwsijxrejymbdpob
nkucgflathzwlijxrqvambdpoq
hkucnflathzasijxrevymbdpoq
nkuigflathzwsirxrevymbdooq
nkucgflatezwsijxwetymbdpoq
nkucmflavhbwsijxrevymbdpoq
nkucgflathzwssjxrevytbmpoq
nkucgflmthzwsvjxrevymbdpgq
nkucgtlathzwsijcrevymbjpoq
nkucgflathfwsfjxrevymbdpsq
nkucgflathjwsijxrwvymbdpok
nkucgeldthzwsijxrevymqdpoq
nkutgcpathzwsijxrevymbdpoq
nkucgflaehzmsijxrevymydpoq
mkucdflathzwsvjxrevymbdpoq
nkucgflathzwsijxtevymidpfq
nkucgfllthzwsijirevlmbdpoq
nkucgfuathzwsijxrevymbqpou
nkufgflathzwsijxrgvymbdpor
nkuygflatrzwsijxrevymbdpoo
nkunoflathzwsijxrevyabdpoq
nksogflathzwsijxrevymbdpeq
nkucgflathzwciexrevymbdhoq
nkucgfnathzwsijxdevyobdpoq
nkudgflazhzwsijxrevymbmpoq
nkucgylathzwscjxrevymbdaoq
nkucgflqthzisijxrerymbdpoq
nkucgxlathzwsijxgebymbdpoq
nkucgflathzssijxrwvymadpoq
nkucgflathzwsijxrvvymbdloi
nkucaflathzwskjxzevymbdpoq
wkuchflathzwsijxrevymbdioq
nkucgilathzwsijxrgvympdpoq
nkubgflaohzwsijxrevymbnpoq
nkucgwoathzwnijxrevymbdpoq
nkprgflathzwsijxrevywbdpoq
nkucgflatlzwsijxievymzdpoq
nkucgflathzwsijxrevymbmdow
nkucgzlathzwsitlrevymbdpoq
nkubgfladhzwsijxrevymbdpsq
nkucgflathzwsijxrzvyibdroq
wkucgflathznsijqrevymbdpoq
nkupgilathzwsijxrnvymbdpoq
nkucgflathzwwijnrevymgdpoq
nkucgflathjwsijxrewymbopoq
mkwcdflathzwsvjxrevymbdpoq
nkucgflathzwsujxoevymbdboq
nkucvflathzwsojxrevymzdpoq
nkocgflabhzwsijxrevyebdpoq
skuciflpthzwsijxrevymbdpoq
nkuxgflathzrsijxrevymbupoq
nkucgblathzwsezxrevymbdpoq
nkucgflathzwsijxrevymbvtop
nkucdflathzwsiaxrefymbdpoq
nkucgflathzwsijxzevkmbdpmq
nkucgflarhzwsijroevymbdpoq
nkuccflathzwsinxrevymsdpoq
nkucgflathzwsijxregymidsoq
nkucgflathnwsijxrvvumbdpoq
nkucgfaathzwssjxrevymbdwoq
nkucgflothzwsijxrevymbdloz
naucgulathzwsijxremymbdpoq
nkucgflaqhzwsijxrevymbdnqq
wkucgflatrzwsijxrevymbdpof
nvucgflaehzwyijxrevymbdpoq
nkusaflaahzwsijxrevymbdpoq
nkucgfkathzwsijxrevymbdbfq
nkucgfkathzwsijrrevymodpoq
nkuegflathzwsijxrrvbmbdpoq
ykucgflathzwsijfrlvymbdpoq
nkucgflathzrsujxrevymbdkoq
nkuclflatsowsijxrevymbdpoq
nkucgflathzwsgjxrqvymbdpor
ekucgflathzwsijorevlmbdpoq
nkucgflathizsijgrevymbdpoq
nkucgfoathzksijbrevymbdpoq
nkucgflachzwsijxrevymbupoa
nkuhgflathzwsijxravylbdpoq
nkncgflithzwsijnrevymbdpoq
nvucgjlathzjsijxrevymbdpoq
nhucgflathzwsijxrtvymbtpoq
akucgflathzwhijxrevymbdpor
nkucgflatozisijxrvvymbdpoq
nkucgzlatgzwsijxrevymbepoq
nkjcgflqthmwsijxrevymbdpoq
nkucgflaohzosijxrhvymbdpoq
ntucgflatrzwsijxrevymbdpol
nkucgflathzwsijkriqymbdpoq
nkuggflathzwsijmrevymbdpvq
nkucgflpmhzwsmjxrevymbdpoq
okucgflathzwsijxrevgmbdsoq
nkucgflaehzwkijxrevymwdpoq
zkucgfllthzwsijxrevymbdpod
nkicgflathzasijxfevymbdpoq
kkucgfhathzwsijxaevymbdpoq
nkucqflsthzwsijxrevyjbdpoq
nkucgflaghzwsijxoevykbdpoq
nkucgflaohzwsljxryvymbdpoq
bkucfflathzwsijxrexymbdpoq
nkucnflathzwsbjxrpvymbdpoq
nkucjflatlzwsijxrevymqdpoq
nkucgflathzwsijsyevyxbdpoq
nkwcgflathzosijxqevymbdpoq
nkucgslathzesijxievymbdpoq
nkuciflauhzwsiaxrevymbdpoq
nkucgflathzwsiwxreeymbdwoq
nkucgblatwzwsijxkevymbdpoq
njucgfkathzwsijxrevymbvpoq
nkucgfladhzwsijfrevyibdpoq
nkukgflathzwsijprenymbdpoq
nkucgflathzwsijxrchymbupoq
nkucgfeathzwsitxaevymbdpoq
nkufjflathzwsijxresymbdpoq
nkuggflatlzwsijxrevymbdpoa
nkucgflsthnwsijxrevumbdpoq
nkuceflathzwsnjxrevymbmpoq
nkucgflabhzwsijxrevymblplq
nkucgfmathzwsijxrevdybdpoq
niuvgflathzwsijxrcvymbdpoq
nkscgflathzwsijxrevyzbdooq
nkucgflatszwsbjxrevymbgpoq
nkucgflazhzwsijxcevymzdpoq
nkucgflathzwsfjqrevymbdpxq
nkucgflathcwsijxrrvymbdroq
nkurgflathzwsijxrepymzdpoq
nlucgflathzwrijxrevdmbdpoq
kkucgflkthzwswjxrevymbdpoq
nktcgflathzwgijxrevbmbdpoq
nbucgfiathzwsijxreyymbdpoq
lkucgflathswsijxrevymbdpxq
ntucgflathzwswrxrevymbdpoq
nkscgflathzwssjxravymbdpoq
nuocgflathzwsijxrevyebdpoq
nbucgllathzwsijxregymbdpoq
ckucbflathzwsijxrelymbdpoq
nkucgflathzwsijxremymbqpor
nkgcgfljthzwsijkrevymbdpoq
nkdcgflashzwsijxrjvymbdpoq
nkecgflathzwsijxuevumbdpoq
njucgflatpfwsijxrevymbdpoq
nkucgwlathzjsijxrevymbzpoq
nkucgfxathzqsijxrenymbdpoq
dkfcgflathzwsijxrevymbdtoq
nkupgfhathzwsijxrevymbjpoq
nkucgflathzwsjjxrevymldooq
pkucgfbathhwsijxrevymbdpoq
nkuciflayhzwsijxrevymbdpfq
nkucpfdathzwsajxrevymbdpoq
ykucgflathdwsijzrevymbdpoq
nkucgwlstnzwsijxrevymbdpoq
nkucwfzazhzwsijxrevymbdpoq
nkucgflatczwssjxretymbdpoq
nkucgflathzwsijpreaymxdpoq
ntucgflathzwsijxrepymvdpoq
nkucgqlathzdsijxrevymbopoq
nkucgflathzusijxfevymbdptq
nkocgflathzwdijxrevymbipoq
nklcgflatgzwsijxrevymbdsoq
nkucgflathzwsgjxgevymbopoq
nkucgflathzwuijxreaymbdyoq
nkucgwlathzwsvjxrevymbdpos
nkucrflathzwliqxrevymbdpoq
nkucgflathzxsijxievysbdpoq
nkufgolhthzwsijxrevymbdpoq
niucgflathzwsiixrevyabdpoq
nkucgflathzhsijxrevymbdyuq
nkucgqlathzwsijxreaymbdpob
nzucgflathzesijxrevymwdpoq
nkucgflatlzwsirxrevymmdpoq
nkucgfxavhzwsijxrevymbwpoq
nkucgflathswsijxrevvmbdpoe
nkucgfgethzwsrjxrevymbdpoq
nkucgzlayhzwsinxrevymbdpoq
nkucgflwthzwsiyxrevymbdpdq
nkucgflpthzwsijxrezombdpoq
nkurgflathdwsijxuevymbdpoq
nkjcgflathzwsijxrevkmbdpoc
nkucmflatuzwsijxrevmmbdpoq
nkucgfldthzwsijxrevevbdpoq
nkucgflatrzgsijxrevambdpoq
nkicgflathzwsijxrevymhdhoq
nkbcgflathzwsijxrevymxdpos
nkucgflatfzwsijxrevymwdqoq
hkucgflaqwzwsijxrevymbdpoq
nkjcgflathzvsijxrevyjbdpoq
niucgflathzwsijxrezymbdpob
ynucgflathzwsijxremymbdpoq
nkubgflathzwsijxrhvymldpoq
nkucqflrthzesijxrevymbdpoq
nkucgulathzwsijxrevyubdioq
nkuczflathzwsijxaebymbdpoq
nkucgfldthzwsibxrevymrdpoq
nkucgflatwzdsijxrevymsdpoq
nkncgffathzwsijxrejymbdpoq
nkucgflathzqsijxrevxmodpoq
nkucgflathwwsijqrevymbipoq
nkucgflathzwhajxrebymbdpoq
gkucgflathzwsijxreirmbdpoq
nkucgflathzesijzravymbdpoq
nkucgflaghzwsijxrerymbdplq
wkucgflathxwgijxrevymbdpoq
nkucgfljthfwsijxrevymbdpfq
nkucgflathwwsimxrevymbdpjq
nkucgdlachzwsijxrevymmdpoq
njucgclathzwsiixrevymbdpoq
nkucgflatdzwsijxrevymzrpoq
nkuckflatvzcsijxrevymbdpoq
nkucgflathzhsijxrevqmbkpoq
nkucqflathzjsijvrevymbdpoq
nkucgftathzwsijxrevympdpoi
nvucgflatmzwsijxrevymbdpsq
nkocgflathznsijxrevymbdphq
mkgcgflathzwsijxrevymbdpvq
nkucnflathzwsijbrevymbdcoq
nkucgflathzwsijsrevymsdgoq
nkuckflatxzwsiwxrevymbdpoq
nkucyflathzwsijxrehcmbdpoq
nkurgflajhzwsijxrevkmbdpoq
wkucgflathzwsijxrfvymbapoq
nkucgflathzwsijxaekymbdpon
nkucgfkathywsijxrevymbdpsq
nkucgflathzwsijxaexcmbdpoq
nkucgflathzwsijxrevymddhox
nkucgflathzwgijxrevymydooq
nkycqflathzwsijxrezymbdpoq
nkucgflathwwsijxrevymbspsq
nkucgflatpzwssjfrevymbdpoq
nkwcgflhthzwsijxrevcmbdpoq

26
2018/d02/ex2/ex2.py Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env python
import itertools
import sys
def solve(input: str) -> str:
def find_box_ids(boxes: list[str]) -> str:
for lhs, rhs in itertools.combinations(boxes, 2):
assert len(lhs) == len(rhs) # Sanity check
for i in range(len(lhs)):
if lhs[:i] == rhs[:i] and lhs[i + 1 :] == rhs[i + 1 :]:
return lhs[:i] + lhs[i + 1 :]
assert False # Sanity check
boxes = input.splitlines()
return find_box_ids(boxes)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

250
2018/d02/ex2/input Normal file
View file

@ -0,0 +1,250 @@
nkucgflathzwwijtrevymbdplq
nkucgflathzwsidxrlvymbdpiq
nkucgjlathqwsijxrevymbypoq
nkucgflarrzwsmjxrevymbdpoq
nkucgflzthtwsijxrevymbdpjq
nkucgflahhzwskjxrevymbgpoq
bkycgflathzwsijxrsvymbdpoq
nkucgflathzwsijxdevymbdmog
nkucgflaehzwsmjxrevymbdpow
nkucgflathzwsijxrevwmbdnbq
nkucgflathzssijxrevynbdqoq
ngucgflathzwsijxsevymndpoq
nfucgflathzvsijxrevymbspoq
nkucgflwthzwsijxreeymbdpkq
nkucgflpthzwsijxrevdmbdpoe
nkungflatuzwsijurevymbdpoq
nkucgflathzwsiqxrevyybdpom
nkucgflathzwsicxrevtmbtpoq
nkucgfladhzwsijxreuymbdboq
nkumgflathznsijxzevymbdpoq
nkuagflethzwsijxrqvymbdpoq
nkucgflatozwhijxrevymbdpkq
nkuggflathzwsijxrejymbdpob
nkucgflathzwlijxrqvambdpoq
hkucnflathzasijxrevymbdpoq
nkuigflathzwsirxrevymbdooq
nkucgflatezwsijxwetymbdpoq
nkucmflavhbwsijxrevymbdpoq
nkucgflathzwssjxrevytbmpoq
nkucgflmthzwsvjxrevymbdpgq
nkucgtlathzwsijcrevymbjpoq
nkucgflathfwsfjxrevymbdpsq
nkucgflathjwsijxrwvymbdpok
nkucgeldthzwsijxrevymqdpoq
nkutgcpathzwsijxrevymbdpoq
nkucgflaehzmsijxrevymydpoq
mkucdflathzwsvjxrevymbdpoq
nkucgflathzwsijxtevymidpfq
nkucgfllthzwsijirevlmbdpoq
nkucgfuathzwsijxrevymbqpou
nkufgflathzwsijxrgvymbdpor
nkuygflatrzwsijxrevymbdpoo
nkunoflathzwsijxrevyabdpoq
nksogflathzwsijxrevymbdpeq
nkucgflathzwciexrevymbdhoq
nkucgfnathzwsijxdevyobdpoq
nkudgflazhzwsijxrevymbmpoq
nkucgylathzwscjxrevymbdaoq
nkucgflqthzisijxrerymbdpoq
nkucgxlathzwsijxgebymbdpoq
nkucgflathzssijxrwvymadpoq
nkucgflathzwsijxrvvymbdloi
nkucaflathzwskjxzevymbdpoq
wkuchflathzwsijxrevymbdioq
nkucgilathzwsijxrgvympdpoq
nkubgflaohzwsijxrevymbnpoq
nkucgwoathzwnijxrevymbdpoq
nkprgflathzwsijxrevywbdpoq
nkucgflatlzwsijxievymzdpoq
nkucgflathzwsijxrevymbmdow
nkucgzlathzwsitlrevymbdpoq
nkubgfladhzwsijxrevymbdpsq
nkucgflathzwsijxrzvyibdroq
wkucgflathznsijqrevymbdpoq
nkupgilathzwsijxrnvymbdpoq
nkucgflathzwwijnrevymgdpoq
nkucgflathjwsijxrewymbopoq
mkwcdflathzwsvjxrevymbdpoq
nkucgflathzwsujxoevymbdboq
nkucvflathzwsojxrevymzdpoq
nkocgflabhzwsijxrevyebdpoq
skuciflpthzwsijxrevymbdpoq
nkuxgflathzrsijxrevymbupoq
nkucgblathzwsezxrevymbdpoq
nkucgflathzwsijxrevymbvtop
nkucdflathzwsiaxrefymbdpoq
nkucgflathzwsijxzevkmbdpmq
nkucgflarhzwsijroevymbdpoq
nkuccflathzwsinxrevymsdpoq
nkucgflathzwsijxregymidsoq
nkucgflathnwsijxrvvumbdpoq
nkucgfaathzwssjxrevymbdwoq
nkucgflothzwsijxrevymbdloz
naucgulathzwsijxremymbdpoq
nkucgflaqhzwsijxrevymbdnqq
wkucgflatrzwsijxrevymbdpof
nvucgflaehzwyijxrevymbdpoq
nkusaflaahzwsijxrevymbdpoq
nkucgfkathzwsijxrevymbdbfq
nkucgfkathzwsijrrevymodpoq
nkuegflathzwsijxrrvbmbdpoq
ykucgflathzwsijfrlvymbdpoq
nkucgflathzrsujxrevymbdkoq
nkuclflatsowsijxrevymbdpoq
nkucgflathzwsgjxrqvymbdpor
ekucgflathzwsijorevlmbdpoq
nkucgflathizsijgrevymbdpoq
nkucgfoathzksijbrevymbdpoq
nkucgflachzwsijxrevymbupoa
nkuhgflathzwsijxravylbdpoq
nkncgflithzwsijnrevymbdpoq
nvucgjlathzjsijxrevymbdpoq
nhucgflathzwsijxrtvymbtpoq
akucgflathzwhijxrevymbdpor
nkucgflatozisijxrvvymbdpoq
nkucgzlatgzwsijxrevymbepoq
nkjcgflqthmwsijxrevymbdpoq
nkucgflaohzosijxrhvymbdpoq
ntucgflatrzwsijxrevymbdpol
nkucgflathzwsijkriqymbdpoq
nkuggflathzwsijmrevymbdpvq
nkucgflpmhzwsmjxrevymbdpoq
okucgflathzwsijxrevgmbdsoq
nkucgflaehzwkijxrevymwdpoq
zkucgfllthzwsijxrevymbdpod
nkicgflathzasijxfevymbdpoq
kkucgfhathzwsijxaevymbdpoq
nkucqflsthzwsijxrevyjbdpoq
nkucgflaghzwsijxoevykbdpoq
nkucgflaohzwsljxryvymbdpoq
bkucfflathzwsijxrexymbdpoq
nkucnflathzwsbjxrpvymbdpoq
nkucjflatlzwsijxrevymqdpoq
nkucgflathzwsijsyevyxbdpoq
nkwcgflathzosijxqevymbdpoq
nkucgslathzesijxievymbdpoq
nkuciflauhzwsiaxrevymbdpoq
nkucgflathzwsiwxreeymbdwoq
nkucgblatwzwsijxkevymbdpoq
njucgfkathzwsijxrevymbvpoq
nkucgfladhzwsijfrevyibdpoq
nkukgflathzwsijprenymbdpoq
nkucgflathzwsijxrchymbupoq
nkucgfeathzwsitxaevymbdpoq
nkufjflathzwsijxresymbdpoq
nkuggflatlzwsijxrevymbdpoa
nkucgflsthnwsijxrevumbdpoq
nkuceflathzwsnjxrevymbmpoq
nkucgflabhzwsijxrevymblplq
nkucgfmathzwsijxrevdybdpoq
niuvgflathzwsijxrcvymbdpoq
nkscgflathzwsijxrevyzbdooq
nkucgflatszwsbjxrevymbgpoq
nkucgflazhzwsijxcevymzdpoq
nkucgflathzwsfjqrevymbdpxq
nkucgflathcwsijxrrvymbdroq
nkurgflathzwsijxrepymzdpoq
nlucgflathzwrijxrevdmbdpoq
kkucgflkthzwswjxrevymbdpoq
nktcgflathzwgijxrevbmbdpoq
nbucgfiathzwsijxreyymbdpoq
lkucgflathswsijxrevymbdpxq
ntucgflathzwswrxrevymbdpoq
nkscgflathzwssjxravymbdpoq
nuocgflathzwsijxrevyebdpoq
nbucgllathzwsijxregymbdpoq
ckucbflathzwsijxrelymbdpoq
nkucgflathzwsijxremymbqpor
nkgcgfljthzwsijkrevymbdpoq
nkdcgflashzwsijxrjvymbdpoq
nkecgflathzwsijxuevumbdpoq
njucgflatpfwsijxrevymbdpoq
nkucgwlathzjsijxrevymbzpoq
nkucgfxathzqsijxrenymbdpoq
dkfcgflathzwsijxrevymbdtoq
nkupgfhathzwsijxrevymbjpoq
nkucgflathzwsjjxrevymldooq
pkucgfbathhwsijxrevymbdpoq
nkuciflayhzwsijxrevymbdpfq
nkucpfdathzwsajxrevymbdpoq
ykucgflathdwsijzrevymbdpoq
nkucgwlstnzwsijxrevymbdpoq
nkucwfzazhzwsijxrevymbdpoq
nkucgflatczwssjxretymbdpoq
nkucgflathzwsijpreaymxdpoq
ntucgflathzwsijxrepymvdpoq
nkucgqlathzdsijxrevymbopoq
nkucgflathzusijxfevymbdptq
nkocgflathzwdijxrevymbipoq
nklcgflatgzwsijxrevymbdsoq
nkucgflathzwsgjxgevymbopoq
nkucgflathzwuijxreaymbdyoq
nkucgwlathzwsvjxrevymbdpos
nkucrflathzwliqxrevymbdpoq
nkucgflathzxsijxievysbdpoq
nkufgolhthzwsijxrevymbdpoq
niucgflathzwsiixrevyabdpoq
nkucgflathzhsijxrevymbdyuq
nkucgqlathzwsijxreaymbdpob
nzucgflathzesijxrevymwdpoq
nkucgflatlzwsirxrevymmdpoq
nkucgfxavhzwsijxrevymbwpoq
nkucgflathswsijxrevvmbdpoe
nkucgfgethzwsrjxrevymbdpoq
nkucgzlayhzwsinxrevymbdpoq
nkucgflwthzwsiyxrevymbdpdq
nkucgflpthzwsijxrezombdpoq
nkurgflathdwsijxuevymbdpoq
nkjcgflathzwsijxrevkmbdpoc
nkucmflatuzwsijxrevmmbdpoq
nkucgfldthzwsijxrevevbdpoq
nkucgflatrzgsijxrevambdpoq
nkicgflathzwsijxrevymhdhoq
nkbcgflathzwsijxrevymxdpos
nkucgflatfzwsijxrevymwdqoq
hkucgflaqwzwsijxrevymbdpoq
nkjcgflathzvsijxrevyjbdpoq
niucgflathzwsijxrezymbdpob
ynucgflathzwsijxremymbdpoq
nkubgflathzwsijxrhvymldpoq
nkucqflrthzesijxrevymbdpoq
nkucgulathzwsijxrevyubdioq
nkuczflathzwsijxaebymbdpoq
nkucgfldthzwsibxrevymrdpoq
nkucgflatwzdsijxrevymsdpoq
nkncgffathzwsijxrejymbdpoq
nkucgflathzqsijxrevxmodpoq
nkucgflathwwsijqrevymbipoq
nkucgflathzwhajxrebymbdpoq
gkucgflathzwsijxreirmbdpoq
nkucgflathzesijzravymbdpoq
nkucgflaghzwsijxrerymbdplq
wkucgflathxwgijxrevymbdpoq
nkucgfljthfwsijxrevymbdpfq
nkucgflathwwsimxrevymbdpjq
nkucgdlachzwsijxrevymmdpoq
njucgclathzwsiixrevymbdpoq
nkucgflatdzwsijxrevymzrpoq
nkuckflatvzcsijxrevymbdpoq
nkucgflathzhsijxrevqmbkpoq
nkucqflathzjsijvrevymbdpoq
nkucgftathzwsijxrevympdpoi
nvucgflatmzwsijxrevymbdpsq
nkocgflathznsijxrevymbdphq
mkgcgflathzwsijxrevymbdpvq
nkucnflathzwsijbrevymbdcoq
nkucgflathzwsijsrevymsdgoq
nkuckflatxzwsiwxrevymbdpoq
nkucyflathzwsijxrehcmbdpoq
nkurgflajhzwsijxrevkmbdpoq
wkucgflathzwsijxrfvymbapoq
nkucgflathzwsijxaekymbdpon
nkucgfkathywsijxrevymbdpsq
nkucgflathzwsijxaexcmbdpoq
nkucgflathzwsijxrevymddhox
nkucgflathzwgijxrevymydooq
nkycqflathzwsijxrezymbdpoq
nkucgflathwwsijxrevymbspsq
nkucgflatpzwssjfrevymbdpoq
nkwcgflhthzwsijxrevcmbdpoq

51
2018/d03/ex1/ex1.py Executable file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from collections.abc import Iterator
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Claim(NamedTuple):
top_left: Point
size: Point
def points(self) -> Iterator[Point]:
for dx, dy in map(
Point._make, itertools.product(range(self.size.x), range(self.size.y))
):
yield Point(self.top_left.x + dx, self.top_left.y + dy)
def solve(input: str) -> int:
def parse_claim(input: str) -> Claim:
offset, size = input.split("@")[1].strip().split(": ")
return Claim(
Point(*map(int, offset.split(","))),
Point(*map(int, size.split("x"))),
)
def parse(input: list[str]) -> list[Claim]:
return [parse_claim(line) for line in input]
def claim_per_point(claims: list[Claim]) -> dict[Point, int]:
points = itertools.chain.from_iterable(map(Claim.points, claims))
return Counter(points)
claims = parse(input.splitlines())
return sum(count > 1 for count in claim_per_point(claims).values())
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1397
2018/d03/ex1/input Normal file

File diff suppressed because it is too large Load diff

67
2018/d03/ex2/ex2.py Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python
import itertools
import sys
from collections.abc import Iterator
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Claim(NamedTuple):
top_left: Point
size: Point
def points(self) -> Iterator[Point]:
for dx, dy in map(
Point._make, itertools.product(range(self.size.x), range(self.size.y))
):
yield Point(self.top_left.x + dx, self.top_left.y + dy)
def solve(input: str) -> int:
def parse_claim(input: str) -> Claim:
offset, size = input.split("@")[1].strip().split(": ")
return Claim(
Point(*map(int, offset.split(","))),
Point(*map(int, size.split("x"))),
)
def parse(input: list[str]) -> list[Claim]:
return [parse_claim(line) for line in input]
def claims_overlap(claim: Claim, other: Claim) -> bool:
min_x1, min_y1 = claim.top_left
max_x1, max_y1 = map(sum, zip(claim.top_left, claim.size))
min_x2, min_y2 = other.top_left
max_x2, max_y2 = map(sum, zip(other.top_left, other.size))
return (
True
and (min_x2 < max_x1 and min_x1 < max_x2)
and (min_y2 < max_y1 and min_y1 < max_y2)
)
def find_non_overlapping(claims: list[Claim]) -> int:
for i, claim in enumerate(claims):
overlaps = any(
claims_overlap(claim, other) for j, other in enumerate(claims) if i != j
)
if not overlaps:
return i + 1 # They use 1-based indexing
assert False # Sanity check
claims = parse(input.splitlines())
return find_non_overlapping(claims)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1397
2018/d03/ex2/input Normal file

File diff suppressed because it is too large Load diff

78
2018/d04/ex1/ex1.py Executable file
View file

@ -0,0 +1,78 @@
#!/usr/bin/env python
import sys
from collections import Counter, defaultdict
from typing import NamedTuple
class DateTime(NamedTuple):
year: int
month: int
day: int
hour: int
min: int
class Log(NamedTuple):
datetime: DateTime
event: str
def solve(input: str) -> int:
def parse_datetime(input: str) -> DateTime:
date, time = input.split()
year, month, day = map(int, date.split("-"))
hour, min = map(int, time.split(":"))
return DateTime(year, month, day, hour, min)
def parse_log(input: str) -> Log:
date, event = input.split("] ")
return Log(parse_datetime(date.removeprefix("[")), event)
def parse(input: list[str]) -> list[Log]:
return [parse_log(line) for line in input]
def parse_guard_number(log: Log) -> int:
assert log.event.startswith("Guard #") # Sanity check
assert log.event.endswith(" begins shift") # Sanity check
return int(log.event.split()[1].removeprefix("#"))
def guards_events(logs: list[Log]) -> dict[int, list[Log]]:
res: dict[int, list[Log]] = defaultdict(list)
current_guard = parse_guard_number(logs[0])
for log in logs[1:]:
if log.event == "falls asleep" or log.event == "wakes up":
res[current_guard].append(log)
else:
current_guard = parse_guard_number(log)
return res
def sleep_counts(logs: list[Log]) -> Counter[int]:
res: Counter[int] = Counter()
assert len(logs) % 2 == 0 # Sanity check
for i in range(0, len(logs), 2):
assert logs[i].event == "falls asleep" # Sanity check
assert logs[i + 1].event == "wakes up" # Sanity check
start, end = logs[i].datetime, logs[i + 1].datetime
res.update(range(start.min, end.min))
return res
def guard_sleep_counts(
guard_logs: dict[int, list[Log]],
) -> dict[int, Counter[int]]:
return {guard: sleep_counts(logs) for guard, logs in guard_logs.items()}
logs = sorted(parse(input.splitlines()))
guard_logs = guards_events(logs)
guard_sleeps = guard_sleep_counts(guard_logs)
most_asleep = max(guard_sleeps, key=lambda guard: sum(guard_sleeps[guard].values()))
return most_asleep * guard_sleeps[most_asleep].most_common()[0][0]
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1098
2018/d04/ex1/input Normal file

File diff suppressed because it is too large Load diff

80
2018/d04/ex2/ex2.py Executable file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
import sys
from collections import Counter, defaultdict
from typing import NamedTuple
class DateTime(NamedTuple):
year: int
month: int
day: int
hour: int
min: int
class Log(NamedTuple):
datetime: DateTime
event: str
def solve(input: str) -> int:
def parse_datetime(input: str) -> DateTime:
date, time = input.split()
year, month, day = map(int, date.split("-"))
hour, min = map(int, time.split(":"))
return DateTime(year, month, day, hour, min)
def parse_log(input: str) -> Log:
date, event = input.split("] ")
return Log(parse_datetime(date.removeprefix("[")), event)
def parse(input: list[str]) -> list[Log]:
return [parse_log(line) for line in input]
def parse_guard_number(log: Log) -> int:
assert log.event.startswith("Guard #") # Sanity check
assert log.event.endswith(" begins shift") # Sanity check
return int(log.event.split()[1].removeprefix("#"))
def guards_events(logs: list[Log]) -> dict[int, list[Log]]:
res: dict[int, list[Log]] = defaultdict(list)
current_guard = parse_guard_number(logs[0])
for log in logs[1:]:
if log.event == "falls asleep" or log.event == "wakes up":
res[current_guard].append(log)
else:
current_guard = parse_guard_number(log)
return res
def sleep_counts(logs: list[Log]) -> Counter[int]:
res: Counter[int] = Counter()
assert len(logs) % 2 == 0 # Sanity check
for i in range(0, len(logs), 2):
assert logs[i].event == "falls asleep" # Sanity check
assert logs[i + 1].event == "wakes up" # Sanity check
start, end = logs[i].datetime, logs[i + 1].datetime
res.update(range(start.min, end.min))
return res
def guard_sleep_counts(
guard_logs: dict[int, list[Log]],
) -> dict[int, Counter[int]]:
return {guard: sleep_counts(logs) for guard, logs in guard_logs.items()}
logs = sorted(parse(input.splitlines()))
guard_logs = guards_events(logs)
guard_sleeps = guard_sleep_counts(guard_logs)
most_asleep = max(
guard_sleeps, key=lambda guard: guard_sleeps[guard].most_common()[0][1]
)
return most_asleep * guard_sleeps[most_asleep].most_common()[0][0]
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1098
2018/d04/ex2/input Normal file

File diff suppressed because it is too large Load diff

29
2018/d05/ex1/ex1.py Executable file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env python
import sys
def solve(input: str) -> int:
def reduce_polymer(polymer: str) -> str:
for i in range(26):
lower, upper = chr(ord("a") + i), chr(ord("A") + i)
polymer = polymer.replace(lower + upper, "")
polymer = polymer.replace(upper + lower, "")
return polymer
polymer = input.strip()
while True:
reduced = reduce_polymer(polymer)
if reduced == polymer:
return len(polymer)
polymer = reduced
assert False # Sanity check
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d05/ex1/input Normal file

File diff suppressed because one or more lines are too long

38
2018/d05/ex2/ex2.py Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
import sys
def solve(input: str) -> int:
def reduce_polymer(polymer: str) -> str:
for i in range(26):
lower, upper = chr(ord("a") + i), chr(ord("A") + i)
polymer = polymer.replace(lower + upper, "")
polymer = polymer.replace(upper + lower, "")
return polymer
def fully_reduced(polymer: str) -> str:
while True:
reduced = reduce_polymer(polymer)
if reduced == polymer:
return polymer
polymer = reduced
assert False # Sanity check
def remove_defective(polymer: str, unit: str) -> str:
return polymer.replace(unit.lower(), "").replace(unit.upper(), "")
polymer = input.strip()
return min(
len(fully_reduced(remove_defective(polymer, chr(ord("a") + i))))
for i in range(26)
)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d05/ex2/input Normal file

File diff suppressed because one or more lines are too long

75
2018/d06/ex1/ex1.py Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter, defaultdict
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> int:
def parse(input: list[str]) -> list[Point]:
return [Point(*map(int, line.split(", "))) for line in input]
def dist(lhs: Point, rhs: Point) -> int:
return sum(abs(a - b) for a, b in zip(lhs, rhs))
def points_distances(coords: list[Point]) -> dict[Point, Counter[Point]]:
top_left = Point(min(p.x for p in coords), min(p.y for p in coords))
bot_right = Point(max(p.x for p in coords), max(p.y for p in coords))
return {
p: Counter({root: dist(root, p) for root in coords})
for p in map(
Point._make,
itertools.product(
range(top_left.x, bot_right.x + 1),
range(top_left.y, bot_right.y + 1),
),
)
}
def remove_areas_on_border(
areas: dict[Point, set[Point]],
top_left: Point,
bot_right: Point,
) -> dict[Point, set[Point]]:
def on_border(points: set[Point]) -> bool:
return any(
p.x in (top_left.x, bot_right.x) or p.y in (top_left.y, bot_right.y)
for p in points
)
return {root: points for root, points in areas.items() if not on_border(points)}
def points_areas(coords: list[Point]) -> dict[Point, int]:
top_left = Point(min(p.x for p in coords), min(p.y for p in coords))
bot_right = Point(max(p.x for p in coords), max(p.y for p in coords))
distances = points_distances(coords)
areas: dict[Point, set[Point]] = defaultdict(set)
for p, root_distances in distances.items():
closest, dist = root_distances.most_common()[-1]
if dist == root_distances.most_common()[-2][1]:
continue
areas[closest].add(p)
areas = remove_areas_on_border(areas, top_left, bot_right)
return {root: len(points) for root, points in areas.items()}
coords = parse(input.splitlines())
areas = points_areas(coords)
return max(areas.values())
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

50
2018/d06/ex1/input Normal file
View file

@ -0,0 +1,50 @@
80, 357
252, 184
187, 139
101, 247
332, 328
302, 60
196, 113
271, 201
334, 89
85, 139
327, 161
316, 352
343, 208
303, 325
316, 149
270, 319
318, 153
257, 332
306, 348
299, 358
172, 289
303, 349
271, 205
347, 296
220, 276
235, 231
133, 201
262, 355
72, 71
73, 145
310, 298
138, 244
322, 334
278, 148
126, 135
340, 133
311, 118
193, 173
319, 99
50, 309
160, 356
155, 195
61, 319
80, 259
106, 318
49, 169
134, 61
74, 204
337, 174
108, 287

47
2018/d06/ex2/ex2.py Executable file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> int:
def parse(input: list[str]) -> list[Point]:
return [Point(*map(int, line.split(", "))) for line in input]
def dist(lhs: Point, rhs: Point) -> int:
return sum(abs(a - b) for a, b in zip(lhs, rhs))
def points_distances(coords: list[Point]) -> dict[Point, Counter[Point]]:
top_left = Point(min(p.x for p in coords), min(p.y for p in coords))
bot_right = Point(max(p.x for p in coords), max(p.y for p in coords))
return {
p: Counter({root: dist(root, p) for root in coords})
for p in map(
Point._make,
itertools.product(
range(top_left.x, bot_right.x + 1),
range(top_left.y, bot_right.y + 1),
),
)
}
coords = parse(input.splitlines())
distances = points_distances(coords)
return sum(dist.total() < 10000 for dist in distances.values())
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

50
2018/d06/ex2/input Normal file
View file

@ -0,0 +1,50 @@
80, 357
252, 184
187, 139
101, 247
332, 328
302, 60
196, 113
271, 201
334, 89
85, 139
327, 161
316, 352
343, 208
303, 325
316, 149
270, 319
318, 153
257, 332
306, 348
299, 358
172, 289
303, 349
271, 205
347, 296
220, 276
235, 231
133, 201
262, 355
72, 71
73, 145
310, 298
138, 244
322, 334
278, 148
126, 135
340, 133
311, 118
193, 173
319, 99
50, 309
160, 356
155, 195
61, 319
80, 259
106, 318
49, 169
134, 61
74, 204
337, 174
108, 287

57
2018/d07/ex1/ex1.py Executable file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
import sys
from collections import defaultdict
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> str:
def parse(input: list[str]) -> dict[str, set[str]]:
graph: dict[str, set[str]] = defaultdict(set)
for line in input:
split = line.split()
prev, after = split[1], split[7]
graph[after].add(prev)
graph[prev] # Ensure that all nodes are in the dictionary
return graph
def topo_sort(graph: dict[str, set[str]]) -> list[str]:
res: list[str] = []
queue = {n for n, deps in graph.items() if not deps}
seen: set[str] = set()
while queue:
# We must pop in alphabetical order
node = min(queue)
queue.remove(node)
res.append(node)
seen.add(node)
# Iterate over all nodes as we don't have information on children
for child, deps in graph.items():
if child in seen:
continue
if deps - seen:
continue
queue.add(child)
return res
graph = parse(input.splitlines())
return "".join(topo_sort(graph))
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

101
2018/d07/ex1/input Normal file
View file

@ -0,0 +1,101 @@
Step F must be finished before step P can begin.
Step R must be finished before step J can begin.
Step X must be finished before step H can begin.
Step L must be finished before step N can begin.
Step U must be finished before step Z can begin.
Step B must be finished before step C can begin.
Step S must be finished before step C can begin.
Step N must be finished before step Y can begin.
Step I must be finished before step J can begin.
Step H must be finished before step K can begin.
Step G must be finished before step Z can begin.
Step Q must be finished before step V can begin.
Step E must be finished before step P can begin.
Step P must be finished before step W can begin.
Step J must be finished before step D can begin.
Step V must be finished before step W can begin.
Step T must be finished before step D can begin.
Step Z must be finished before step A can begin.
Step K must be finished before step A can begin.
Step Y must be finished before step O can begin.
Step O must be finished before step W can begin.
Step C must be finished before step M can begin.
Step D must be finished before step A can begin.
Step W must be finished before step M can begin.
Step M must be finished before step A can begin.
Step C must be finished before step A can begin.
Step F must be finished before step Z can begin.
Step I must be finished before step A can begin.
Step W must be finished before step A can begin.
Step T must be finished before step C can begin.
Step S must be finished before step K can begin.
Step B must be finished before step J can begin.
Step O must be finished before step A can begin.
Step Q must be finished before step P can begin.
Step G must be finished before step M can begin.
Step R must be finished before step T can begin.
Step B must be finished before step G can begin.
Step J must be finished before step O can begin.
Step X must be finished before step E can begin.
Step X must be finished before step C can begin.
Step H must be finished before step Y can begin.
Step Y must be finished before step A can begin.
Step X must be finished before step W can begin.
Step H must be finished before step A can begin.
Step X must be finished before step A can begin.
Step I must be finished before step M can begin.
Step G must be finished before step J can begin.
Step N must be finished before step G can begin.
Step D must be finished before step M can begin.
Step L must be finished before step D can begin.
Step V must be finished before step T can begin.
Step I must be finished before step Y can begin.
Step S must be finished before step J can begin.
Step K must be finished before step Y can begin.
Step F must be finished before step R can begin.
Step U must be finished before step T can begin.
Step Z must be finished before step M can begin.
Step T must be finished before step Z can begin.
Step B must be finished before step I can begin.
Step E must be finished before step K can begin.
Step N must be finished before step J can begin.
Step X must be finished before step Q can begin.
Step F must be finished before step Y can begin.
Step H must be finished before step P can begin.
Step Z must be finished before step D can begin.
Step V must be finished before step O can begin.
Step E must be finished before step C can begin.
Step V must be finished before step C can begin.
Step P must be finished before step A can begin.
Step B must be finished before step N can begin.
Step S must be finished before step W can begin.
Step P must be finished before step D can begin.
Step L must be finished before step W can begin.
Step D must be finished before step W can begin.
Step K must be finished before step C can begin.
Step L must be finished before step M can begin.
Step R must be finished before step O can begin.
Step F must be finished before step L can begin.
Step R must be finished before step H can begin.
Step K must be finished before step O can begin.
Step T must be finished before step W can begin.
Step R must be finished before step K can begin.
Step C must be finished before step W can begin.
Step N must be finished before step T can begin.
Step R must be finished before step P can begin.
Step E must be finished before step M can begin.
Step G must be finished before step T can begin.
Step U must be finished before step K can begin.
Step Q must be finished before step D can begin.
Step U must be finished before step S can begin.
Step J must be finished before step V can begin.
Step P must be finished before step Y can begin.
Step X must be finished before step Z can begin.
Step U must be finished before step H can begin.
Step H must be finished before step M can begin.
Step I must be finished before step C can begin.
Step V must be finished before step M can begin.
Step N must be finished before step I can begin.
Step B must be finished before step K can begin.
Step R must be finished before step Q can begin.
Step O must be finished before step C can begin.

67
2018/d07/ex2/ex2.py Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python
import sys
from collections import defaultdict
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> int:
def parse(input: list[str]) -> dict[str, set[str]]:
graph: dict[str, set[str]] = defaultdict(set)
for line in input:
split = line.split()
prev, after = split[1], split[7]
graph[after].add(prev)
graph[prev] # Ensure that all nodes are in the dictionary
return graph
def job_done(graph: dict[str, set[str]], job: str) -> None:
assert not graph.pop(job) # Sanity check
for node in graph:
graph[node].discard(job)
def next_job(graph: dict[str, set[str]], ongoing: list[str]) -> str | None:
return min(
(n for n, deps in graph.items() if not deps and n not in ongoing),
default=None,
)
def assemble(graph: dict[str, set[str]], additional_time: int, workers: int) -> int:
res = 0
worker_time = [0] * workers
worker_jobs = [""] * workers
while graph:
# Step to the next worker, ignoring idle workers, with a default for the first step
dt = min((t for t in worker_time if t > 0), default=0)
res += dt
worker_time = [max(0, t - dt) for t in worker_time]
for i in range(workers):
if worker_time[i] != 0:
continue
if worker_jobs[i] != "":
job_done(graph, worker_jobs[i])
worker_jobs[i] = ""
if (job := next_job(graph, worker_jobs)) is None:
continue
worker_time[i] = additional_time + ord(job) - ord("A") + 1
worker_jobs[i] = job
assert all(j == "" for j in worker_jobs) # Sanity check
assert all(t == 0 for t in worker_time) # Sanity check
return res
graph = parse(input.splitlines())
return assemble(graph, 60, 5)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

101
2018/d07/ex2/input Normal file
View file

@ -0,0 +1,101 @@
Step F must be finished before step P can begin.
Step R must be finished before step J can begin.
Step X must be finished before step H can begin.
Step L must be finished before step N can begin.
Step U must be finished before step Z can begin.
Step B must be finished before step C can begin.
Step S must be finished before step C can begin.
Step N must be finished before step Y can begin.
Step I must be finished before step J can begin.
Step H must be finished before step K can begin.
Step G must be finished before step Z can begin.
Step Q must be finished before step V can begin.
Step E must be finished before step P can begin.
Step P must be finished before step W can begin.
Step J must be finished before step D can begin.
Step V must be finished before step W can begin.
Step T must be finished before step D can begin.
Step Z must be finished before step A can begin.
Step K must be finished before step A can begin.
Step Y must be finished before step O can begin.
Step O must be finished before step W can begin.
Step C must be finished before step M can begin.
Step D must be finished before step A can begin.
Step W must be finished before step M can begin.
Step M must be finished before step A can begin.
Step C must be finished before step A can begin.
Step F must be finished before step Z can begin.
Step I must be finished before step A can begin.
Step W must be finished before step A can begin.
Step T must be finished before step C can begin.
Step S must be finished before step K can begin.
Step B must be finished before step J can begin.
Step O must be finished before step A can begin.
Step Q must be finished before step P can begin.
Step G must be finished before step M can begin.
Step R must be finished before step T can begin.
Step B must be finished before step G can begin.
Step J must be finished before step O can begin.
Step X must be finished before step E can begin.
Step X must be finished before step C can begin.
Step H must be finished before step Y can begin.
Step Y must be finished before step A can begin.
Step X must be finished before step W can begin.
Step H must be finished before step A can begin.
Step X must be finished before step A can begin.
Step I must be finished before step M can begin.
Step G must be finished before step J can begin.
Step N must be finished before step G can begin.
Step D must be finished before step M can begin.
Step L must be finished before step D can begin.
Step V must be finished before step T can begin.
Step I must be finished before step Y can begin.
Step S must be finished before step J can begin.
Step K must be finished before step Y can begin.
Step F must be finished before step R can begin.
Step U must be finished before step T can begin.
Step Z must be finished before step M can begin.
Step T must be finished before step Z can begin.
Step B must be finished before step I can begin.
Step E must be finished before step K can begin.
Step N must be finished before step J can begin.
Step X must be finished before step Q can begin.
Step F must be finished before step Y can begin.
Step H must be finished before step P can begin.
Step Z must be finished before step D can begin.
Step V must be finished before step O can begin.
Step E must be finished before step C can begin.
Step V must be finished before step C can begin.
Step P must be finished before step A can begin.
Step B must be finished before step N can begin.
Step S must be finished before step W can begin.
Step P must be finished before step D can begin.
Step L must be finished before step W can begin.
Step D must be finished before step W can begin.
Step K must be finished before step C can begin.
Step L must be finished before step M can begin.
Step R must be finished before step O can begin.
Step F must be finished before step L can begin.
Step R must be finished before step H can begin.
Step K must be finished before step O can begin.
Step T must be finished before step W can begin.
Step R must be finished before step K can begin.
Step C must be finished before step W can begin.
Step N must be finished before step T can begin.
Step R must be finished before step P can begin.
Step E must be finished before step M can begin.
Step G must be finished before step T can begin.
Step U must be finished before step K can begin.
Step Q must be finished before step D can begin.
Step U must be finished before step S can begin.
Step J must be finished before step V can begin.
Step P must be finished before step Y can begin.
Step X must be finished before step Z can begin.
Step U must be finished before step H can begin.
Step H must be finished before step M can begin.
Step I must be finished before step C can begin.
Step V must be finished before step M can begin.
Step N must be finished before step I can begin.
Step B must be finished before step K can begin.
Step R must be finished before step Q can begin.
Step O must be finished before step C can begin.

53
2018/d08/ex1/ex1.py Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env python
import itertools
import sys
from collections.abc import Iterator
from typing import NamedTuple
class Tree(NamedTuple):
children: list["Tree"]
metadata: list[int]
@classmethod
def from_raw(cls, raw: list[int]) -> "Tree":
def helper(offset: int) -> tuple[Tree, int]:
n_children, n_metadata = raw[offset], raw[offset + 1]
offset += 2
children: list[Tree] = []
for _ in range(n_children):
tree, offset = helper(offset)
children.append(tree)
metadata = raw[offset : offset + n_metadata]
offset += n_metadata
return cls(children, metadata), offset
tree, offset = helper(0)
assert offset == len(raw)
return tree
def preorder(self) -> Iterator["Tree"]:
yield self
for child in self.children:
yield from child.preorder()
def solve(input: str) -> int:
def parse(input: str) -> Tree:
raw = [int(n) for n in input.split()]
return Tree.from_raw(raw)
tree = parse(input)
return sum(itertools.chain.from_iterable(node.metadata for node in tree.preorder()))
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d08/ex1/input Normal file

File diff suppressed because one or more lines are too long

65
2018/d08/ex2/ex2.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
import dataclasses
import sys
from collections.abc import Iterator
@dataclasses.dataclass
class Tree:
children: list["Tree"]
metadata: list[int]
@classmethod
def from_raw(cls, raw: list[int]) -> "Tree":
def helper(offset: int) -> tuple[Tree, int]:
n_children, n_metadata = raw[offset], raw[offset + 1]
offset += 2
children: list[Tree] = []
for _ in range(n_children):
tree, offset = helper(offset)
children.append(tree)
metadata = raw[offset : offset + n_metadata]
offset += n_metadata
return cls(children, metadata), offset
tree, offset = helper(0)
assert offset == len(raw)
return tree
def preorder(self) -> Iterator["Tree"]:
yield self
for child in self.children:
yield from child.preorder()
def value(self) -> int:
if not self.children:
return sum(self.metadata, 0)
return sum(
(
self.children[i - 1].value()
for i in self.metadata
if i <= len(self.children)
),
0,
)
def solve(input: str) -> int:
def parse(input: str) -> Tree:
raw = [int(n) for n in input.split()]
return Tree.from_raw(raw)
tree = parse(input)
return tree.value()
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d08/ex2/input Normal file

File diff suppressed because one or more lines are too long

39
2018/d09/ex1/ex1.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python
import collections
import sys
def solve(input: str) -> int:
def parse(input: str) -> tuple[int, int]:
split = input.split()
return int(split[0]), int(split[6])
def play_game(players: int, last_marble: int) -> list[int]:
circle = collections.deque([0])
scores = [0] * players
for marble in range(1, last_marble + 1):
if marble % 23 == 0:
scores[marble % players] += marble
circle.rotate(7)
scores[marble % players] += circle.pop()
circle.rotate(-1)
else:
circle.rotate(-1)
circle.append(marble)
return scores
players, last_marble = parse(input)
scores = play_game(players, last_marble)
return max(scores)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d09/ex1/input Normal file
View file

@ -0,0 +1 @@
476 players; last marble is worth 71657 points

39
2018/d09/ex2/ex2.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python
import collections
import sys
def solve(input: str) -> int:
def parse(input: str) -> tuple[int, int]:
split = input.split()
return int(split[0]), int(split[6])
def play_game(players: int, last_marble: int) -> list[int]:
circle = collections.deque([0])
scores = [0] * players
for marble in range(1, last_marble + 1):
if marble % 23 == 0:
scores[marble % players] += marble
circle.rotate(7)
scores[marble % players] += circle.pop()
circle.rotate(-1)
else:
circle.rotate(-1)
circle.append(marble)
return scores
players, last_marble = parse(input)
scores = play_game(players, last_marble * 100)
return max(scores)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d09/ex2/input Normal file
View file

@ -0,0 +1 @@
476 players; last marble is worth 71657 points

65
2018/d10/ex1/ex1.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
import itertools
import sys
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Light(NamedTuple):
pos: Point
vel: Point
def at(self, time: int) -> Point:
return Point(self.pos.x + self.vel.x * time, self.pos.y + self.vel.y * time)
def solve(input: str) -> str:
def parse_light(input: str) -> Light:
pos, vel = map(lambda s: s.split("=<")[1].removesuffix(">"), input.split("> "))
return Light(
Point(*map(int, map(str.strip, pos.split(",")))),
Point(*map(int, map(str.strip, vel.split(",")))),
)
def parse(input: list[str]) -> list[Light]:
return [parse_light(line) for line in input]
def move_lights(lights: list[Light], time: int) -> set[Point]:
return {l.at(time) for l in lights}
def bbox_size(points: set[Point]) -> int:
min_x, max_x = min(p.x for p in points), max(p.x for p in points)
min_y, max_y = min(p.y for p in points), max(p.y for p in points)
return (max_x - min_x + 1) * (max_y - min_y + 1)
def write_message(message: set[Point]) -> str:
min_x, max_x = min(p.x for p in message), max(p.x for p in message)
min_y, max_y = min(p.y for p in message), max(p.y for p in message)
return "\n".join(
"".join(
"#" if Point(x, y) in message else "." for x in range(min_x, max_x + 1)
)
for y in range(min_y, max_y + 1)
)
lights = parse(input.splitlines())
max_time = max(
map(abs, itertools.chain.from_iterable((l.pos.x, l.pos.y) for l in lights))
)
message = min((move_lights(lights, t) for t in range(max_time)), key=bbox_size)
return write_message(message)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

318
2018/d10/ex1/input Normal file
View file

@ -0,0 +1,318 @@
position=<-41150, 41504> velocity=< 4, -4>
position=< 31211, -10213> velocity=<-3, 1>
position=<-51522, -41248> velocity=< 5, 4>
position=< 31227, -51593> velocity=<-3, 5>
position=< 31257, -20560> velocity=<-3, 2>
position=< 41558, 10468> velocity=<-4, -1>
position=< 10539, -30904> velocity=<-1, 3>
position=< 51919, -41253> velocity=<-5, 4>
position=< 31246, 10473> velocity=<-3, -1>
position=<-20471, 20813> velocity=< 2, -2>
position=< 51910, -10222> velocity=<-5, 1>
position=<-20503, 20816> velocity=< 2, -2>
position=< 20901, -41255> velocity=<-2, 4>
position=< 31249, -41249> velocity=<-3, 4>
position=<-10139, 10477> velocity=< 1, -1>
position=< 20888, -30903> velocity=<-2, 3>
position=< 31218, 31158> velocity=<-3, -3>
position=<-30802, 51848> velocity=< 3, -5>
position=< 51923, 51854> velocity=<-5, -5>
position=<-51503, 41504> velocity=< 5, -4>
position=<-10131, 31165> velocity=< 1, -3>
position=<-30809, -10222> velocity=< 3, 1>
position=< 10551, 20818> velocity=<-1, -2>
position=< 31236, 31158> velocity=<-3, -3>
position=<-51534, 20822> velocity=< 5, -2>
position=< 20920, -41250> velocity=<-2, 4>
position=<-30841, 41504> velocity=< 3, -4>
position=< 51932, -41256> velocity=<-5, 4>
position=< 31249, 10469> velocity=<-3, -1>
position=<-41198, 41505> velocity=< 4, -4>
position=<-30833, 41510> velocity=< 3, -4>
position=<-51494, 41507> velocity=< 5, -4>
position=< 10553, 10472> velocity=<-1, -1>
position=<-41174, -20564> velocity=< 4, 2>
position=< 41578, 20816> velocity=<-4, -2>
position=<-51503, -10214> velocity=< 5, 1>
position=<-30824, -30906> velocity=< 3, 3>
position=<-41172, 20816> velocity=< 4, -2>
position=<-41198, 41507> velocity=< 4, -4>
position=< 41578, -30911> velocity=<-4, 3>
position=< 20904, 20817> velocity=<-2, -2>
position=< 51901, 41503> velocity=<-5, -4>
position=< 20872, -30906> velocity=<-2, 3>
position=< 31265, -20560> velocity=<-3, 2>
position=<-10136, -10217> velocity=< 1, 1>
position=< 31246, 20817> velocity=<-3, -2>
position=< 51927, 20820> velocity=<-5, -2>
position=<-30821, -10222> velocity=< 3, 1>
position=<-20492, -10221> velocity=< 2, 1>
position=<-20488, -51596> velocity=< 2, 5>
position=< 41610, -41249> velocity=<-4, 4>
position=< 31257, -30908> velocity=<-3, 3>
position=< 41610, 31160> velocity=<-4, -3>
position=<-10151, 41503> velocity=< 1, -4>
position=<-51527, -10216> velocity=< 5, 1>
position=< 20867, -30903> velocity=<-2, 3>
position=< 31228, 41503> velocity=<-3, -4>
position=<-10160, -41253> velocity=< 1, 4>
position=< 41595, -30907> velocity=<-4, 3>
position=<-51506, 31158> velocity=< 5, -3>
position=<-10142, -10219> velocity=< 1, 1>
position=<-10115, -10222> velocity=< 1, 1>
position=< 10572, -51601> velocity=<-1, 5>
position=< 51899, 20818> velocity=<-5, -2>
position=<-20476, -30910> velocity=< 2, 3>
position=<-41174, 31162> velocity=< 4, -3>
position=< 31246, 41509> velocity=<-3, -4>
position=<-30801, -41253> velocity=< 3, 4>
position=<-10130, 41508> velocity=< 1, -4>
position=<-20473, 10470> velocity=< 2, -1>
position=<-41150, -20565> velocity=< 4, 2>
position=< 31246, 31165> velocity=<-3, -3>
position=< 41610, -41256> velocity=<-4, 4>
position=< 41574, 31164> velocity=<-4, -3>
position=<-41157, -30903> velocity=< 4, 3>
position=< 31241, 10469> velocity=<-3, -1>
position=< 31217, -30912> velocity=<-3, 3>
position=< 10529, 41503> velocity=<-1, -4>
position=< 20901, 51853> velocity=<-2, -5>
position=<-51551, 10472> velocity=< 5, -1>
position=< 41613, 41503> velocity=<-4, -4>
position=<-10134, -41251> velocity=< 1, 4>
position=< 41595, -41253> velocity=<-4, 4>
position=<-51511, 10477> velocity=< 5, -1>
position=<-30837, -10217> velocity=< 3, 1>
position=< 51950, -51602> velocity=<-5, 5>
position=<-51541, 41503> velocity=< 5, -4>
position=< 51907, 51857> velocity=<-5, -5>
position=<-41198, 10469> velocity=< 4, -1>
position=<-30834, 20817> velocity=< 3, -2>
position=< 51947, 31163> velocity=<-5, -3>
position=<-30804, -30912> velocity=< 3, 3>
position=<-30829, -20560> velocity=< 3, 2>
position=< 10547, -30908> velocity=<-1, 3>
position=<-51531, 31166> velocity=< 5, -3>
position=< 51955, 41510> velocity=<-5, -4>
position=<-30861, -30911> velocity=< 3, 3>
position=< 20872, -51596> velocity=<-2, 5>
position=<-30829, 51854> velocity=< 3, -5>
position=<-41150, -10218> velocity=< 4, 1>
position=< 31257, -20560> velocity=<-3, 2>
position=<-20476, 20816> velocity=< 2, -2>
position=<-20468, 10470> velocity=< 2, -1>
position=<-51519, -30911> velocity=< 5, 3>
position=< 51959, 10468> velocity=<-5, -1>
position=< 20888, -51593> velocity=<-2, 5>
position=< 51947, 10475> velocity=<-5, -1>
position=< 31246, 10468> velocity=<-3, -1>
position=<-20516, -41249> velocity=< 2, 4>
position=<-51511, -41248> velocity=< 5, 4>
position=<-41147, 20817> velocity=< 4, -2>
position=<-41150, -51594> velocity=< 4, 5>
position=<-10163, -30908> velocity=< 1, 3>
position=<-10152, -10213> velocity=< 1, 1>
position=<-20460, -30903> velocity=< 2, 3>
position=<-41169, -10219> velocity=< 4, 1>
position=<-51531, -30907> velocity=< 5, 3>
position=<-30812, 41503> velocity=< 3, -4>
position=<-41169, 10477> velocity=< 4, -1>
position=< 10546, 51848> velocity=<-1, -5>
position=<-10139, 20813> velocity=< 1, -2>
position=<-30861, 51851> velocity=< 3, -5>
position=<-20497, -51593> velocity=< 2, 5>
position=< 51949, -20558> velocity=<-5, 2>
position=< 41597, -51595> velocity=<-4, 5>
position=< 51940, 31163> velocity=<-5, -3>
position=< 41589, 10473> velocity=<-4, -1>
position=<-51495, -41257> velocity=< 5, 4>
position=< 31265, -41254> velocity=<-3, 4>
position=< 10527, 10473> velocity=<-1, -1>
position=< 51927, 41509> velocity=<-5, -4>
position=< 20884, 10471> velocity=<-2, -1>
position=<-51493, 41507> velocity=< 5, -4>
position=<-41182, -30912> velocity=< 4, 3>
position=< 10575, 41509> velocity=<-1, -4>
position=< 20884, -20567> velocity=<-2, 2>
position=< 10535, -20560> velocity=<-1, 2>
position=<-51543, 41512> velocity=< 5, -4>
position=<-30825, -10214> velocity=< 3, 1>
position=<-51535, -20559> velocity=< 5, 2>
position=< 10528, -10218> velocity=<-1, 1>
position=<-51538, 41506> velocity=< 5, -4>
position=< 20892, -30905> velocity=<-2, 3>
position=< 31249, -51596> velocity=<-3, 5>
position=< 10539, 20815> velocity=<-1, -2>
position=<-20464, -41248> velocity=< 2, 4>
position=<-41186, -20567> velocity=< 4, 2>
position=<-20492, 20820> velocity=< 2, -2>
position=<-30818, 31165> velocity=< 3, -3>
position=<-10131, -51596> velocity=< 1, 5>
position=< 51919, 20814> velocity=<-5, -2>
position=<-41166, -30905> velocity=< 4, 3>
position=<-10151, 31163> velocity=< 1, -3>
position=< 10522, 10468> velocity=<-1, -1>
position=< 51911, -30912> velocity=<-5, 3>
position=<-41173, 31160> velocity=< 4, -3>
position=< 31260, -41248> velocity=<-3, 4>
position=<-10139, -10220> velocity=< 1, 1>
position=<-30857, -20558> velocity=< 3, 2>
position=< 10531, -10222> velocity=<-1, 1>
position=<-30813, -20566> velocity=< 3, 2>
position=< 51944, -30903> velocity=<-5, 3>
position=<-41198, 20820> velocity=< 4, -2>
position=<-41150, -41252> velocity=< 4, 4>
position=<-20508, 20814> velocity=< 2, -2>
position=< 41554, -41256> velocity=<-4, 4>
position=< 31238, -41248> velocity=<-3, 4>
position=< 20906, -10219> velocity=<-2, 1>
position=< 51912, 41505> velocity=<-5, -4>
position=< 20866, -51602> velocity=<-2, 5>
position=< 10519, 31164> velocity=<-1, -3>
position=<-20479, 51856> velocity=< 2, -5>
position=<-30829, -51596> velocity=< 3, 5>
position=< 51943, -10221> velocity=<-5, 1>
position=< 20917, -41249> velocity=<-2, 4>
position=< 20888, 41507> velocity=<-2, -4>
position=< 51957, 41507> velocity=<-5, -4>
position=<-20499, -10213> velocity=< 2, 1>
position=< 51911, 41503> velocity=<-5, -4>
position=<-51551, -51595> velocity=< 5, 5>
position=<-10139, 51849> velocity=< 1, -5>
position=< 41582, -10222> velocity=<-4, 1>
position=<-41150, 31159> velocity=< 4, -3>
position=< 20888, 41504> velocity=<-2, -4>
position=< 10567, 20821> velocity=<-1, -2>
position=< 51900, -10222> velocity=<-5, 1>
position=<-51503, 20815> velocity=< 5, -2>
position=<-20488, -51596> velocity=< 2, 5>
position=< 51939, -10221> velocity=<-5, 1>
position=< 31270, 10468> velocity=<-3, -1>
position=<-51527, 20816> velocity=< 5, -2>
position=< 51926, 51853> velocity=<-5, -5>
position=<-51543, -20564> velocity=< 5, 2>
position=< 20883, -20558> velocity=<-2, 2>
position=<-41166, 20816> velocity=< 4, -2>
position=< 51936, 41511> velocity=<-5, -4>
position=<-10115, -10220> velocity=< 1, 1>
position=< 31210, -51593> velocity=<-3, 5>
position=<-10115, -41248> velocity=< 1, 4>
position=< 31233, -20559> velocity=<-3, 2>
position=< 20921, -51598> velocity=<-2, 5>
position=<-41185, -30912> velocity=< 4, 3>
position=<-51531, -51600> velocity=< 5, 5>
position=< 10543, -10220> velocity=<-1, 1>
position=< 51924, -30912> velocity=<-5, 3>
position=<-30821, 20813> velocity=< 3, -2>
position=< 10545, -51602> velocity=<-1, 5>
position=<-10126, -41248> velocity=< 1, 4>
position=<-30829, -30907> velocity=< 3, 3>
position=<-51495, 20817> velocity=< 5, -2>
position=< 10548, -41256> velocity=<-1, 4>
position=< 41578, -51599> velocity=<-4, 5>
position=<-41203, -51602> velocity=< 4, 5>
position=< 31253, -10214> velocity=<-3, 1>
position=<-20457, -30908> velocity=< 2, 3>
position=<-10147, 51848> velocity=< 1, -5>
position=<-51502, -20558> velocity=< 5, 2>
position=< 41615, -10222> velocity=<-4, 1>
position=<-41182, -30906> velocity=< 4, 3>
position=< 20912, -10216> velocity=<-2, 1>
position=< 20869, -20566> velocity=<-2, 2>
position=< 10555, 41510> velocity=<-1, -4>
position=<-30835, 10472> velocity=< 3, -1>
position=<-20487, 41505> velocity=< 2, -4>
position=<-20511, -41249> velocity=< 2, 4>
position=< 20913, 20822> velocity=<-2, -2>
position=<-30824, 10469> velocity=< 3, -1>
position=< 10567, 10472> velocity=<-1, -1>
position=< 31238, 31166> velocity=<-3, -3>
position=<-41186, -10219> velocity=< 4, 1>
position=<-41169, -51602> velocity=< 4, 5>
position=< 51947, -20564> velocity=<-5, 2>
position=< 41562, -10219> velocity=<-4, 1>
position=<-10155, 20821> velocity=< 1, -2>
position=< 20884, -51598> velocity=<-2, 5>
position=< 20907, 41505> velocity=<-2, -4>
position=< 10579, 51848> velocity=<-1, -5>
position=< 41586, -30905> velocity=<-4, 3>
position=<-30859, -41248> velocity=< 3, 4>
position=< 20864, 10475> velocity=<-2, -1>
position=< 20876, -30908> velocity=<-2, 3>
position=< 10543, -41248> velocity=<-1, 4>
position=< 31246, -51598> velocity=<-3, 5>
position=<-30833, 41503> velocity=< 3, -4>
position=<-20473, -51595> velocity=< 2, 5>
position=< 31265, -30904> velocity=<-3, 3>
position=<-51493, 31158> velocity=< 5, -3>
position=< 51947, 20821> velocity=<-5, -2>
position=<-41166, -30912> velocity=< 4, 3>
position=<-30813, 41508> velocity=< 3, -4>
position=<-41198, -51601> velocity=< 4, 5>
position=<-10151, 20819> velocity=< 1, -2>
position=< 51907, 51854> velocity=<-5, -5>
position=<-10158, -41254> velocity=< 1, 4>
position=<-41170, -41249> velocity=< 4, 4>
position=< 51907, 51856> velocity=<-5, -5>
position=< 41586, -10216> velocity=<-4, 1>
position=< 31246, -20560> velocity=<-3, 2>
position=<-30826, -10217> velocity=< 3, 1>
position=< 10569, 51848> velocity=<-1, -5>
position=<-20484, -30912> velocity=< 2, 3>
position=< 10559, 41510> velocity=<-1, -4>
position=< 41555, 31167> velocity=<-4, -3>
position=< 20877, -30911> velocity=<-2, 3>
position=<-51549, 20822> velocity=< 5, -2>
position=<-51551, 51856> velocity=< 5, -5>
position=<-10163, 10476> velocity=< 1, -1>
position=<-51530, -10222> velocity=< 5, 1>
position=< 31241, 20817> velocity=<-3, -2>
position=<-51514, 20822> velocity=< 5, -2>
position=<-41155, 31158> velocity=< 4, -3>
position=< 41565, 10468> velocity=<-4, -1>
position=< 51949, 41503> velocity=<-5, -4>
position=< 20874, 10468> velocity=<-2, -1>
position=< 41611, -41253> velocity=<-4, 4>
position=<-30813, -30906> velocity=< 3, 3>
position=< 41574, 41511> velocity=<-4, -4>
position=< 51931, 51856> velocity=<-5, -5>
position=<-20508, -10220> velocity=< 2, 1>
position=< 20891, -41253> velocity=<-2, 4>
position=< 31249, 31159> velocity=<-3, -3>
position=< 41579, 31162> velocity=<-4, -3>
position=<-51527, 51853> velocity=< 5, -5>
position=<-30826, 20819> velocity=< 3, -2>
position=<-20508, 10471> velocity=< 2, -1>
position=<-20472, 10476> velocity=< 2, -1>
position=< 51924, 41507> velocity=<-5, -4>
position=<-30837, -51600> velocity=< 3, 5>
position=<-20487, 41512> velocity=< 2, -4>
position=<-51499, 41512> velocity=< 5, -4>
position=<-51508, -30910> velocity=< 5, 3>
position=<-20483, -20566> velocity=< 2, 2>
position=<-20516, -51596> velocity=< 2, 5>
position=< 20924, -30908> velocity=<-2, 3>
position=<-10144, -41257> velocity=< 1, 4>
position=<-41154, -41257> velocity=< 4, 4>
position=< 51902, -20558> velocity=<-5, 2>
position=< 41610, -10216> velocity=<-4, 1>
position=< 51917, -41248> velocity=<-5, 4>
position=<-20459, 31158> velocity=< 2, -3>
position=< 20884, 31162> velocity=<-2, -3>
position=<-20506, -30908> velocity=< 2, 3>
position=<-51533, -30903> velocity=< 5, 3>
position=<-10168, -30912> velocity=< 1, 3>
position=< 20904, 41506> velocity=<-2, -4>
position=<-51551, 31163> velocity=< 5, -3>
position=<-20500, -51595> velocity=< 2, 5>
position=< 31251, -20561> velocity=<-3, 2>
position=<-41186, -51595> velocity=< 4, 5>
position=<-20496, 31163> velocity=< 2, -3>
position=<-20458, -41257> velocity=< 2, 4>
position=< 41554, -41255> velocity=<-4, 4>
position=<-51511, 31163> velocity=< 5, -3>
position=<-51535, 20821> velocity=< 5, -2>
position=<-30853, -41249> velocity=< 3, 4>
position=< 20912, -51597> velocity=<-2, 5>
position=< 10527, -30903> velocity=<-1, 3>
position=< 41587, 41505> velocity=<-4, -4>

65
2018/d10/ex2/ex2.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
import itertools
import sys
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Light(NamedTuple):
pos: Point
vel: Point
def at(self, time: int) -> Point:
return Point(self.pos.x + self.vel.x * time, self.pos.y + self.vel.y * time)
def solve(input: str) -> int:
def parse_light(input: str) -> Light:
pos, vel = map(lambda s: s.split("=<")[1].removesuffix(">"), input.split("> "))
return Light(
Point(*map(int, map(str.strip, pos.split(",")))),
Point(*map(int, map(str.strip, vel.split(",")))),
)
def parse(input: list[str]) -> list[Light]:
return [parse_light(line) for line in input]
def move_lights(lights: list[Light], time: int) -> set[Point]:
return {l.at(time) for l in lights}
def bbox_size(points: set[Point]) -> int:
min_x, max_x = min(p.x for p in points), max(p.x for p in points)
min_y, max_y = min(p.y for p in points), max(p.y for p in points)
return (max_x - min_x + 1) * (max_y - min_y + 1)
def write_message(message: set[Point]) -> str:
min_x, max_x = min(p.x for p in message), max(p.x for p in message)
min_y, max_y = min(p.y for p in message), max(p.y for p in message)
return "\n".join(
"".join(
"#" if Point(x, y) in message else "." for x in range(min_x, max_x + 1)
)
for y in range(min_y, max_y + 1)
)
lights = parse(input.splitlines())
max_time = max(
map(abs, itertools.chain.from_iterable((l.pos.x, l.pos.y) for l in lights))
)
time = min(range(max_time), key=lambda t: bbox_size(move_lights(lights, t)))
return time
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

318
2018/d10/ex2/input Normal file
View file

@ -0,0 +1,318 @@
position=<-41150, 41504> velocity=< 4, -4>
position=< 31211, -10213> velocity=<-3, 1>
position=<-51522, -41248> velocity=< 5, 4>
position=< 31227, -51593> velocity=<-3, 5>
position=< 31257, -20560> velocity=<-3, 2>
position=< 41558, 10468> velocity=<-4, -1>
position=< 10539, -30904> velocity=<-1, 3>
position=< 51919, -41253> velocity=<-5, 4>
position=< 31246, 10473> velocity=<-3, -1>
position=<-20471, 20813> velocity=< 2, -2>
position=< 51910, -10222> velocity=<-5, 1>
position=<-20503, 20816> velocity=< 2, -2>
position=< 20901, -41255> velocity=<-2, 4>
position=< 31249, -41249> velocity=<-3, 4>
position=<-10139, 10477> velocity=< 1, -1>
position=< 20888, -30903> velocity=<-2, 3>
position=< 31218, 31158> velocity=<-3, -3>
position=<-30802, 51848> velocity=< 3, -5>
position=< 51923, 51854> velocity=<-5, -5>
position=<-51503, 41504> velocity=< 5, -4>
position=<-10131, 31165> velocity=< 1, -3>
position=<-30809, -10222> velocity=< 3, 1>
position=< 10551, 20818> velocity=<-1, -2>
position=< 31236, 31158> velocity=<-3, -3>
position=<-51534, 20822> velocity=< 5, -2>
position=< 20920, -41250> velocity=<-2, 4>
position=<-30841, 41504> velocity=< 3, -4>
position=< 51932, -41256> velocity=<-5, 4>
position=< 31249, 10469> velocity=<-3, -1>
position=<-41198, 41505> velocity=< 4, -4>
position=<-30833, 41510> velocity=< 3, -4>
position=<-51494, 41507> velocity=< 5, -4>
position=< 10553, 10472> velocity=<-1, -1>
position=<-41174, -20564> velocity=< 4, 2>
position=< 41578, 20816> velocity=<-4, -2>
position=<-51503, -10214> velocity=< 5, 1>
position=<-30824, -30906> velocity=< 3, 3>
position=<-41172, 20816> velocity=< 4, -2>
position=<-41198, 41507> velocity=< 4, -4>
position=< 41578, -30911> velocity=<-4, 3>
position=< 20904, 20817> velocity=<-2, -2>
position=< 51901, 41503> velocity=<-5, -4>
position=< 20872, -30906> velocity=<-2, 3>
position=< 31265, -20560> velocity=<-3, 2>
position=<-10136, -10217> velocity=< 1, 1>
position=< 31246, 20817> velocity=<-3, -2>
position=< 51927, 20820> velocity=<-5, -2>
position=<-30821, -10222> velocity=< 3, 1>
position=<-20492, -10221> velocity=< 2, 1>
position=<-20488, -51596> velocity=< 2, 5>
position=< 41610, -41249> velocity=<-4, 4>
position=< 31257, -30908> velocity=<-3, 3>
position=< 41610, 31160> velocity=<-4, -3>
position=<-10151, 41503> velocity=< 1, -4>
position=<-51527, -10216> velocity=< 5, 1>
position=< 20867, -30903> velocity=<-2, 3>
position=< 31228, 41503> velocity=<-3, -4>
position=<-10160, -41253> velocity=< 1, 4>
position=< 41595, -30907> velocity=<-4, 3>
position=<-51506, 31158> velocity=< 5, -3>
position=<-10142, -10219> velocity=< 1, 1>
position=<-10115, -10222> velocity=< 1, 1>
position=< 10572, -51601> velocity=<-1, 5>
position=< 51899, 20818> velocity=<-5, -2>
position=<-20476, -30910> velocity=< 2, 3>
position=<-41174, 31162> velocity=< 4, -3>
position=< 31246, 41509> velocity=<-3, -4>
position=<-30801, -41253> velocity=< 3, 4>
position=<-10130, 41508> velocity=< 1, -4>
position=<-20473, 10470> velocity=< 2, -1>
position=<-41150, -20565> velocity=< 4, 2>
position=< 31246, 31165> velocity=<-3, -3>
position=< 41610, -41256> velocity=<-4, 4>
position=< 41574, 31164> velocity=<-4, -3>
position=<-41157, -30903> velocity=< 4, 3>
position=< 31241, 10469> velocity=<-3, -1>
position=< 31217, -30912> velocity=<-3, 3>
position=< 10529, 41503> velocity=<-1, -4>
position=< 20901, 51853> velocity=<-2, -5>
position=<-51551, 10472> velocity=< 5, -1>
position=< 41613, 41503> velocity=<-4, -4>
position=<-10134, -41251> velocity=< 1, 4>
position=< 41595, -41253> velocity=<-4, 4>
position=<-51511, 10477> velocity=< 5, -1>
position=<-30837, -10217> velocity=< 3, 1>
position=< 51950, -51602> velocity=<-5, 5>
position=<-51541, 41503> velocity=< 5, -4>
position=< 51907, 51857> velocity=<-5, -5>
position=<-41198, 10469> velocity=< 4, -1>
position=<-30834, 20817> velocity=< 3, -2>
position=< 51947, 31163> velocity=<-5, -3>
position=<-30804, -30912> velocity=< 3, 3>
position=<-30829, -20560> velocity=< 3, 2>
position=< 10547, -30908> velocity=<-1, 3>
position=<-51531, 31166> velocity=< 5, -3>
position=< 51955, 41510> velocity=<-5, -4>
position=<-30861, -30911> velocity=< 3, 3>
position=< 20872, -51596> velocity=<-2, 5>
position=<-30829, 51854> velocity=< 3, -5>
position=<-41150, -10218> velocity=< 4, 1>
position=< 31257, -20560> velocity=<-3, 2>
position=<-20476, 20816> velocity=< 2, -2>
position=<-20468, 10470> velocity=< 2, -1>
position=<-51519, -30911> velocity=< 5, 3>
position=< 51959, 10468> velocity=<-5, -1>
position=< 20888, -51593> velocity=<-2, 5>
position=< 51947, 10475> velocity=<-5, -1>
position=< 31246, 10468> velocity=<-3, -1>
position=<-20516, -41249> velocity=< 2, 4>
position=<-51511, -41248> velocity=< 5, 4>
position=<-41147, 20817> velocity=< 4, -2>
position=<-41150, -51594> velocity=< 4, 5>
position=<-10163, -30908> velocity=< 1, 3>
position=<-10152, -10213> velocity=< 1, 1>
position=<-20460, -30903> velocity=< 2, 3>
position=<-41169, -10219> velocity=< 4, 1>
position=<-51531, -30907> velocity=< 5, 3>
position=<-30812, 41503> velocity=< 3, -4>
position=<-41169, 10477> velocity=< 4, -1>
position=< 10546, 51848> velocity=<-1, -5>
position=<-10139, 20813> velocity=< 1, -2>
position=<-30861, 51851> velocity=< 3, -5>
position=<-20497, -51593> velocity=< 2, 5>
position=< 51949, -20558> velocity=<-5, 2>
position=< 41597, -51595> velocity=<-4, 5>
position=< 51940, 31163> velocity=<-5, -3>
position=< 41589, 10473> velocity=<-4, -1>
position=<-51495, -41257> velocity=< 5, 4>
position=< 31265, -41254> velocity=<-3, 4>
position=< 10527, 10473> velocity=<-1, -1>
position=< 51927, 41509> velocity=<-5, -4>
position=< 20884, 10471> velocity=<-2, -1>
position=<-51493, 41507> velocity=< 5, -4>
position=<-41182, -30912> velocity=< 4, 3>
position=< 10575, 41509> velocity=<-1, -4>
position=< 20884, -20567> velocity=<-2, 2>
position=< 10535, -20560> velocity=<-1, 2>
position=<-51543, 41512> velocity=< 5, -4>
position=<-30825, -10214> velocity=< 3, 1>
position=<-51535, -20559> velocity=< 5, 2>
position=< 10528, -10218> velocity=<-1, 1>
position=<-51538, 41506> velocity=< 5, -4>
position=< 20892, -30905> velocity=<-2, 3>
position=< 31249, -51596> velocity=<-3, 5>
position=< 10539, 20815> velocity=<-1, -2>
position=<-20464, -41248> velocity=< 2, 4>
position=<-41186, -20567> velocity=< 4, 2>
position=<-20492, 20820> velocity=< 2, -2>
position=<-30818, 31165> velocity=< 3, -3>
position=<-10131, -51596> velocity=< 1, 5>
position=< 51919, 20814> velocity=<-5, -2>
position=<-41166, -30905> velocity=< 4, 3>
position=<-10151, 31163> velocity=< 1, -3>
position=< 10522, 10468> velocity=<-1, -1>
position=< 51911, -30912> velocity=<-5, 3>
position=<-41173, 31160> velocity=< 4, -3>
position=< 31260, -41248> velocity=<-3, 4>
position=<-10139, -10220> velocity=< 1, 1>
position=<-30857, -20558> velocity=< 3, 2>
position=< 10531, -10222> velocity=<-1, 1>
position=<-30813, -20566> velocity=< 3, 2>
position=< 51944, -30903> velocity=<-5, 3>
position=<-41198, 20820> velocity=< 4, -2>
position=<-41150, -41252> velocity=< 4, 4>
position=<-20508, 20814> velocity=< 2, -2>
position=< 41554, -41256> velocity=<-4, 4>
position=< 31238, -41248> velocity=<-3, 4>
position=< 20906, -10219> velocity=<-2, 1>
position=< 51912, 41505> velocity=<-5, -4>
position=< 20866, -51602> velocity=<-2, 5>
position=< 10519, 31164> velocity=<-1, -3>
position=<-20479, 51856> velocity=< 2, -5>
position=<-30829, -51596> velocity=< 3, 5>
position=< 51943, -10221> velocity=<-5, 1>
position=< 20917, -41249> velocity=<-2, 4>
position=< 20888, 41507> velocity=<-2, -4>
position=< 51957, 41507> velocity=<-5, -4>
position=<-20499, -10213> velocity=< 2, 1>
position=< 51911, 41503> velocity=<-5, -4>
position=<-51551, -51595> velocity=< 5, 5>
position=<-10139, 51849> velocity=< 1, -5>
position=< 41582, -10222> velocity=<-4, 1>
position=<-41150, 31159> velocity=< 4, -3>
position=< 20888, 41504> velocity=<-2, -4>
position=< 10567, 20821> velocity=<-1, -2>
position=< 51900, -10222> velocity=<-5, 1>
position=<-51503, 20815> velocity=< 5, -2>
position=<-20488, -51596> velocity=< 2, 5>
position=< 51939, -10221> velocity=<-5, 1>
position=< 31270, 10468> velocity=<-3, -1>
position=<-51527, 20816> velocity=< 5, -2>
position=< 51926, 51853> velocity=<-5, -5>
position=<-51543, -20564> velocity=< 5, 2>
position=< 20883, -20558> velocity=<-2, 2>
position=<-41166, 20816> velocity=< 4, -2>
position=< 51936, 41511> velocity=<-5, -4>
position=<-10115, -10220> velocity=< 1, 1>
position=< 31210, -51593> velocity=<-3, 5>
position=<-10115, -41248> velocity=< 1, 4>
position=< 31233, -20559> velocity=<-3, 2>
position=< 20921, -51598> velocity=<-2, 5>
position=<-41185, -30912> velocity=< 4, 3>
position=<-51531, -51600> velocity=< 5, 5>
position=< 10543, -10220> velocity=<-1, 1>
position=< 51924, -30912> velocity=<-5, 3>
position=<-30821, 20813> velocity=< 3, -2>
position=< 10545, -51602> velocity=<-1, 5>
position=<-10126, -41248> velocity=< 1, 4>
position=<-30829, -30907> velocity=< 3, 3>
position=<-51495, 20817> velocity=< 5, -2>
position=< 10548, -41256> velocity=<-1, 4>
position=< 41578, -51599> velocity=<-4, 5>
position=<-41203, -51602> velocity=< 4, 5>
position=< 31253, -10214> velocity=<-3, 1>
position=<-20457, -30908> velocity=< 2, 3>
position=<-10147, 51848> velocity=< 1, -5>
position=<-51502, -20558> velocity=< 5, 2>
position=< 41615, -10222> velocity=<-4, 1>
position=<-41182, -30906> velocity=< 4, 3>
position=< 20912, -10216> velocity=<-2, 1>
position=< 20869, -20566> velocity=<-2, 2>
position=< 10555, 41510> velocity=<-1, -4>
position=<-30835, 10472> velocity=< 3, -1>
position=<-20487, 41505> velocity=< 2, -4>
position=<-20511, -41249> velocity=< 2, 4>
position=< 20913, 20822> velocity=<-2, -2>
position=<-30824, 10469> velocity=< 3, -1>
position=< 10567, 10472> velocity=<-1, -1>
position=< 31238, 31166> velocity=<-3, -3>
position=<-41186, -10219> velocity=< 4, 1>
position=<-41169, -51602> velocity=< 4, 5>
position=< 51947, -20564> velocity=<-5, 2>
position=< 41562, -10219> velocity=<-4, 1>
position=<-10155, 20821> velocity=< 1, -2>
position=< 20884, -51598> velocity=<-2, 5>
position=< 20907, 41505> velocity=<-2, -4>
position=< 10579, 51848> velocity=<-1, -5>
position=< 41586, -30905> velocity=<-4, 3>
position=<-30859, -41248> velocity=< 3, 4>
position=< 20864, 10475> velocity=<-2, -1>
position=< 20876, -30908> velocity=<-2, 3>
position=< 10543, -41248> velocity=<-1, 4>
position=< 31246, -51598> velocity=<-3, 5>
position=<-30833, 41503> velocity=< 3, -4>
position=<-20473, -51595> velocity=< 2, 5>
position=< 31265, -30904> velocity=<-3, 3>
position=<-51493, 31158> velocity=< 5, -3>
position=< 51947, 20821> velocity=<-5, -2>
position=<-41166, -30912> velocity=< 4, 3>
position=<-30813, 41508> velocity=< 3, -4>
position=<-41198, -51601> velocity=< 4, 5>
position=<-10151, 20819> velocity=< 1, -2>
position=< 51907, 51854> velocity=<-5, -5>
position=<-10158, -41254> velocity=< 1, 4>
position=<-41170, -41249> velocity=< 4, 4>
position=< 51907, 51856> velocity=<-5, -5>
position=< 41586, -10216> velocity=<-4, 1>
position=< 31246, -20560> velocity=<-3, 2>
position=<-30826, -10217> velocity=< 3, 1>
position=< 10569, 51848> velocity=<-1, -5>
position=<-20484, -30912> velocity=< 2, 3>
position=< 10559, 41510> velocity=<-1, -4>
position=< 41555, 31167> velocity=<-4, -3>
position=< 20877, -30911> velocity=<-2, 3>
position=<-51549, 20822> velocity=< 5, -2>
position=<-51551, 51856> velocity=< 5, -5>
position=<-10163, 10476> velocity=< 1, -1>
position=<-51530, -10222> velocity=< 5, 1>
position=< 31241, 20817> velocity=<-3, -2>
position=<-51514, 20822> velocity=< 5, -2>
position=<-41155, 31158> velocity=< 4, -3>
position=< 41565, 10468> velocity=<-4, -1>
position=< 51949, 41503> velocity=<-5, -4>
position=< 20874, 10468> velocity=<-2, -1>
position=< 41611, -41253> velocity=<-4, 4>
position=<-30813, -30906> velocity=< 3, 3>
position=< 41574, 41511> velocity=<-4, -4>
position=< 51931, 51856> velocity=<-5, -5>
position=<-20508, -10220> velocity=< 2, 1>
position=< 20891, -41253> velocity=<-2, 4>
position=< 31249, 31159> velocity=<-3, -3>
position=< 41579, 31162> velocity=<-4, -3>
position=<-51527, 51853> velocity=< 5, -5>
position=<-30826, 20819> velocity=< 3, -2>
position=<-20508, 10471> velocity=< 2, -1>
position=<-20472, 10476> velocity=< 2, -1>
position=< 51924, 41507> velocity=<-5, -4>
position=<-30837, -51600> velocity=< 3, 5>
position=<-20487, 41512> velocity=< 2, -4>
position=<-51499, 41512> velocity=< 5, -4>
position=<-51508, -30910> velocity=< 5, 3>
position=<-20483, -20566> velocity=< 2, 2>
position=<-20516, -51596> velocity=< 2, 5>
position=< 20924, -30908> velocity=<-2, 3>
position=<-10144, -41257> velocity=< 1, 4>
position=<-41154, -41257> velocity=< 4, 4>
position=< 51902, -20558> velocity=<-5, 2>
position=< 41610, -10216> velocity=<-4, 1>
position=< 51917, -41248> velocity=<-5, 4>
position=<-20459, 31158> velocity=< 2, -3>
position=< 20884, 31162> velocity=<-2, -3>
position=<-20506, -30908> velocity=< 2, 3>
position=<-51533, -30903> velocity=< 5, 3>
position=<-10168, -30912> velocity=< 1, 3>
position=< 20904, 41506> velocity=<-2, -4>
position=<-51551, 31163> velocity=< 5, -3>
position=<-20500, -51595> velocity=< 2, 5>
position=< 31251, -20561> velocity=<-3, 2>
position=<-41186, -51595> velocity=< 4, 5>
position=<-20496, 31163> velocity=< 2, -3>
position=<-20458, -41257> velocity=< 2, 4>
position=< 41554, -41255> velocity=<-4, 4>
position=<-51511, 31163> velocity=< 5, -3>
position=<-51535, 20821> velocity=< 5, -2>
position=<-30853, -41249> velocity=< 3, 4>
position=< 20912, -51597> velocity=<-2, 5>
position=< 10527, -30903> velocity=<-1, 3>
position=< 41587, 41505> velocity=<-4, -4>

42
2018/d11/ex1/ex1.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import functools
import itertools
import sys
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> str:
def cell_power(cell: Point, serial: int) -> int:
rack_id = cell.x + 10
power = rack_id * cell.y
power += serial
power *= rack_id
return ((power // 100) % 10) - 5
def total_power(top_left: Point, serial: int) -> int:
return sum(
cell_power(Point(top_left.x + dx, top_left.y + dy), serial)
for dx, dy in itertools.product(range(3), repeat=2)
)
serial = int(input)
cell = max(
map(Point._make, itertools.product(range(1, 300 - 3 + 1), repeat=2)),
key=functools.partial(total_power, serial=serial),
)
return f"{cell.x},{cell.y}"
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d11/ex1/input Normal file
View file

@ -0,0 +1 @@
1788

70
2018/d11/ex2/ex2.py Executable file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
import itertools
import sys
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
def solve(input: str) -> str:
def cell_power(cell: Point, serial: int) -> int:
rack_id = cell.x + 10
power = rack_id * cell.y
power += serial
power *= rack_id
return ((power // 100) % 10) - 5
def summed_area(power_map: dict[Point, int]) -> dict[Point, int]:
res: dict[Point, int] = {}
for p in map(Point._make, itertools.product(range(1, 300 + 1), repeat=2)):
res[p] = (
power_map[p]
+ res.get(Point(p.x - 1, p.y), 0)
+ res.get(Point(p.x, p.y - 1), 0)
- res.get(Point(p.x - 1, p.y - 1), 0)
)
assert len(res) == len(power_map) # Sanity check
return res
def sum_square(top_left: Point, size: int, summed_table: dict[Point, int]) -> int:
bot_right = Point(top_left.x + size - 1, top_left.y + size - 1)
return (
summed_table[bot_right]
- summed_table.get(Point(top_left.x - 1, bot_right.y), 0)
- summed_table.get(Point(bot_right.x, top_left.y - 1), 0)
+ summed_table.get(Point(top_left.x - 1, top_left.y - 1), 0)
)
def best_square(summed_table: dict[Point, int]) -> tuple[Point, int]:
return max(
(
(top_left, size)
for size in range(1, 300 + 1)
for top_left in map(
Point._make, itertools.product(range(1, 300 + 1 - size), repeat=2)
)
),
key=lambda t: sum_square(t[0], t[1], summed_table),
)
serial = int(input)
power_map = {
p: cell_power(p, serial)
for p in map(Point._make, itertools.product(range(1, 300 + 1), repeat=2))
}
summed_table = summed_area(power_map)
top_left, size = best_square(summed_table)
return f"{top_left.x},{top_left.y},{size}"
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

1
2018/d11/ex2/input Normal file
View file

@ -0,0 +1 @@
1788

38
2018/d12/ex1/ex1.py Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
import sys
def solve(input: str) -> int:
def parse_rules(input: list[str]) -> dict[str, str]:
return {key: val for key, val in map(lambda s: s.split(" => "), input)}
def parse(input: str) -> tuple[str, dict[str, str]]:
init, rules = input.split("\n\n")
return init.split(": ")[1], parse_rules(rules.splitlines())
def step(state: str, rules: dict[str, str]) -> str:
state = "...." + state + "...."
return "".join(
rules.get(state[i - 2 : i + 2 + 1], ".") for i in range(2, len(state) - 2)
)
def count_pots(state: str) -> int:
return sum(
i for i, c in enumerate(state, start=-(len(state) - 100) // 2) if c == "#"
)
state, rules = parse(input)
assert len(state) == 100 # Sanity check for `count_pots`
for _ in range(20):
state = step(state, rules)
return count_pots(state)
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

34
2018/d12/ex1/input Normal file
View file

@ -0,0 +1,34 @@
initial state: #.#.#..##.#....#.#.##..##.##..#..#...##....###..#......###.#..#.....#.###.#...#####.####...#####.#.#
..#.. => .
#...# => .
.#... => #
#.##. => .
..#.# => #
#.#.# => .
###.. => #
###.# => #
..... => .
....# => .
.##.. => #
##### => .
####. => .
..##. => .
##.#. => #
.#..# => #
##..# => .
.##.# => .
.#### => #
..### => .
...## => #
#..## => #
#.... => .
##.## => .
#.#.. => .
##... => .
.#.## => #
.###. => #
...#. => .
#.### => .
#..#. => #
.#.#. => .

51
2018/d12/ex2/ex2.py Executable file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python
import sys
def solve(input: str) -> int:
def parse_rules(input: list[str]) -> dict[str, str]:
return {key: val for key, val in map(lambda s: s.split(" => "), input)}
def parse(input: str) -> tuple[str, dict[str, str]]:
init, rules = input.split("\n\n")
return init.split(": ")[1], parse_rules(rules.splitlines())
def step(state: str, rules: dict[str, str]) -> str:
state = "...." + state + "...."
return "".join(
rules.get(state[i - 2 : i + 2 + 1], ".") for i in range(2, len(state) - 2)
)
def count_pots(state: str) -> int:
return sum(
i for i, c in enumerate(state, start=-(len(state) - 100) // 2) if c == "#"
)
def get_diffs(state: str, rules: dict[str, str], iterations: int) -> list[int]:
res: list[int] = [count_pots(state)]
prev_count = res[0]
for _ in range(iterations):
state = step(state, rules)
delta = count_pots(state) - prev_count
prev_count += delta
res.append(delta)
return res
state, rules = parse(input)
assert len(state) == 100 # Sanity check for `count_pots`
STEPS = 1000
deltas = get_diffs(state, rules, STEPS)
# Change in deltas gets into a steady state after some time
average_delta = sum(deltas[-100:]) // 100
return sum(deltas) + (50000000000 - STEPS) * average_delta
def main() -> None:
input = sys.stdin.read()
print(solve(input))
if __name__ == "__main__":
main()

34
2018/d12/ex2/input Normal file
View file

@ -0,0 +1,34 @@
initial state: #.#.#..##.#....#.#.##..##.##..#..#...##....###..#......###.#..#.....#.###.#...#####.####...#####.#.#
..#.. => .
#...# => .
.#... => #
#.##. => .
..#.# => #
#.#.# => .
###.. => #
###.# => #
..... => .
....# => .
.##.. => #
##### => .
####. => .
..##. => .
##.#. => #
.#..# => #
##..# => .
.##.# => .
.#### => #
..### => .
...## => #
#..## => #
#.... => .
##.## => .
#.#.. => .
##... => .
.#.## => #
.###. => #
...#. => .
#.### => .
#..#. => #
.#.#. => .