Move python solutions to separate directory

This commit is contained in:
Joscha 2019-12-05 08:45:38 +00:00
parent 47e97f4533
commit 3903907973
70 changed files with 0 additions and 0 deletions

965
py/2018/01/input.txt Normal file
View file

@ -0,0 +1,965 @@
-4
+7
+3
+1
-9
-14
+18
-7
-5
-18
+11
-8
+17
-16
-19
+14
+11
-8
+14
+22
+13
+14
-18
+8
-16
+10
-12
+9
-19
-12
-6
+10
+2
-14
+18
+17
+11
-5
+6
+9
+16
-3
+12
+5
+15
+7
+2
-5
-13
+7
+19
+10
-2
+3
-5
-7
-11
+14
+13
+3
+11
+15
-19
-1
-5
+15
+14
-16
+8
+9
-11
-7
+15
+4
+7
+11
-2
+17
-8
-14
+3
-4
+18
+1
+6
-5
+17
+13
-14
-15
-9
+16
+14
+12
-14
-14
+4
-19
+11
+5
+7
+1
+13
-7
+19
+12
+10
+13
-3
+6
-17
+13
-8
+16
+4
-15
+14
-1
+15
-19
+15
-19
+17
-6
+11
-10
+9
+17
+6
+15
-18
+2
-8
+11
-6
-7
+9
+16
-13
-18
-1
+5
-16
+9
-6
+11
-16
+3
-9
+18
+5
+7
+15
+12
+19
-17
-4
-15
-13
+5
-11
+25
+14
+5
+5
+4
-2
-4
-14
+7
+16
+6
+11
-8
+13
+8
+3
-12
+8
+7
-5
+14
+5
+8
-3
-14
-14
-7
-18
-12
+18
+4
-14
-17
-17
+12
-9
+3
-18
+27
+26
-9
-7
+12
+9
+6
+7
+1
-13
+4
-8
-3
-12
+21
+13
+6
+7
+9
+18
-17
+4
+10
-11
-17
-16
+18
+9
-2
+3
-13
-9
+3
-10
-15
+11
+24
+2
+16
+11
+18
+14
-5
-3
+18
+4
+4
+5
-2
-10
+8
+19
+4
+14
+8
+1
-14
-7
+15
-4
-5
+20
+15
+2
-14
-12
-16
+4
-9
+18
+16
+7
+7
+10
+17
-18
+3
+10
-11
-21
-9
-2
+12
-17
+13
+19
-18
-4
-19
+10
-11
-18
-4
+6
+1
+2
-4
-28
-28
-19
+13
+18
-15
-6
+5
+9
-26
-6
+19
-6
+1
-11
-11
+25
-26
+19
+10
+2
+9
-22
-66
-8
+1
-14
-2
-5
+19
-55
+6
-19
-4
-13
+21
-13
-3
-10
-7
+15
-5
-4
-10
+5
-11
-3
+16
+6
+10
+3
-1
-5
-21
-6
+9
-4
-6
-17
-10
+14
-11
+17
+16
-19
+6
+1
-2
+8
-15
+17
-14
-16
+7
-5
-24
-7
-20
+5
-4
+11
-1
+12
-9
-11
-9
-9
-11
-3
-15
-21
-12
+21
+3
+12
+2
+2
+21
-9
+20
+4
+11
-1
+4
-20
+4
+19
+12
+4
-15
+14
+11
+21
-29
-53
+10
-11
+8
-10
-11
+12
-17
-25
-35
+1
-2
+14
+36
+70
+14
+50
+3
+6
+23
-20
-6
-66
-39
-14
+191
-17
+24
+155
-6
+61
+59623
-4
+7
+19
+5
-1
-8
-8
-15
+16
+8
-7
+19
-3
+14
+14
+6
+17
-15
+3
+11
+12
-5
+19
+2
+1
+16
+13
+6
+1
+18
+16
-4
+11
-1
+19
-7
-19
-10
+19
+7
+4
-17
-4
-4
+9
-8
-17
-9
-14
+16
+4
-19
+11
+1
+2
-19
+7
-8
-7
-18
-6
+4
-5
-15
+2
+7
-18
-8
+14
-20
+1
+17
-2
+3
+17
-1
+7
+10
+10
-1
+19
+3
-16
-10
+1
+10
+6
+20
-17
+19
+14
-18
-11
+22
-3
-16
+15
+12
+14
-12
-1
+22
+10
-7
-8
+2
-1
-6
-7
+19
+7
+12
-14
+18
+7
-17
-9
+10
+14
-1
-17
+8
+4
+2
+16
+3
+16
+4
-2
+18
-19
-15
-1
-10
-5
+8
-4
-13
+18
+15
-5
+10
-6
+13
-11
+12
+2
+4
-2
-13
-7
-11
-13
-5
+4
+10
+12
-15
-16
-10
-9
-27
-4
-9
-21
-13
-4
-1
-10
-10
+14
+12
-29
-16
-12
-1
-12
+18
-20
-11
-5
+12
-2
-6
+18
-2
-1
+6
-18
-12
+20
-4
+6
-14
+6
-17
-6
+8
-14
-18
-4
-8
+2
+7
+20
-10
-15
+18
-12
+13
-18
+9
+11
-19
+9
+3
+10
-17
-16
-4
-11
-5
-9
-12
+4
-13
+14
+13
-17
-21
+7
+19
+10
-6
+9
+13
-5
-13
+19
+7
-4
-6
+17
-16
-8
-14
-11
-3
-16
+10
-1
-2
-19
+3
+13
-15
+8
+6
-19
+2
-12
-6
-19
+17
-8
+11
+2
-18
-6
-10
+7
+4
+12
-3
+8
+7
+18
-8
+10
-15
-2
-12
+20
-13
-16
-11
+9
-1
+11
+19
-5
+20
-2
+17
+17
+17
+18
+17
+25
-31
+20
-7
+21
+15
+15
-13
+18
+14
+28
-10
+16
-17
-16
-2
-7
-8
-23
-16
+11
-20
+11
-3
-31
-4
-20
+18
-8
+3
+6
-15
-35
+19
+2
-4
+9
-12
-15
-9
-3
+15
+38
-6
+11
-36
-11
-14
-1
-5
+2
+23
-12
-33
-18
+1
+5
+3
+16
+7
-28
+8
-22
-7
-18
-17
+16
+11
+16
+4
-15
+7
-10
-7
-9
-4
+9
+6
-21
-11
-20
-19
-5
+3
+27
+9
+23
+16
-19
-2
+20
+13
-2
-16
+11
+2
-10
-11
-21
-6
-8
-10
-20
+35
+46
+2
-19
-13
+23
-2
+56
+7
+53
+5
-64
-62
+103
+162
+4
+10
-27
-15
-14
+22
+9
+1
+38
-9
+8
+9
+34
+1
-11
+4
-19
-29
+16
+52
-16
+27
+188
-83
+191
+59071
+19
-10
-6
-16
-18
+14
-10
+18
+4
-10
+3
+16
+6
-18
-3
+1
+4
-20
+4
-16
-7
-6
+19
+12
-3
-7
-13
-7
-9
+19
-20
+12
+12
+18
-15
-8
-11
-9
-7
+13
+16
+11
+4
-13
-10
+1
-11
-14
+13
+4
+10
-5
+7
-24
-7
+11
-22
-20
+15
-10
+13
-7
-25
-2
-4
+45
-1
+16
+11
+24
+4
+4
+22
-17
+13
-16
+5
+16
+16
+15
-119289

33
py/2018/01/solve.py Normal file
View file

@ -0,0 +1,33 @@
import sys
def load_freqs(filename):
freqs = []
with open(filename, "r") as f:
for line in f:
n = int(line[:-1])
freqs.append(n)
return freqs
# PART 2
def find_repeat(freqs):
total = 0
found = {total}
while True:
for n in freqs:
total += n
if total in found:
return total
else:
found.add(total)
def main(filename):
freqs = load_freqs(filename)
print(f"Solutions for {filename}")
print(f"Part 1: {sum(freqs)}")
print(f"Part 2: {find_repeat(freqs)}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -0,0 +1,5 @@
+1
+2
-3
-4
+5

250
py/2018/02/input.txt Normal file
View file

@ -0,0 +1,250 @@
icxjubroqtunzeyzpomfksahgw
ibxjvbroqtunleyzjdmfksahow
icxjvbroqtinleyzpdmflsahnw
icxjvbnoqtunleyvpgmfksahgw
wcxjvbroqrunleyzpdmfksahge
icxjtbroqtjzleyzpdmfksahgw
icxjvbrohtunleyzpdmfkbahsw
xcxjvbroqcunleyzpdmfksaxgw
ycxjvbroqtunleyzpowfksahgw
icxfvbroqtunleyzpdmfksncgw
ixxjvbuoqtunleyzpdvfksahgw
icfjvbroqtunleyzpdmfksadgt
icxjvbroqdunleyzpdafksqhgw
icxjvbrobtunlelzpdmfkuahgs
ujxjvbroqtunleyzpdmqksahgw
icqjvsroqtunleyzpdmfksahuw
icxjvbroptpnleyzpdmfksangw
ipxjvbroqtunleyzpdmfesahgi
icajvbroqtunltyzpdqfksahgw
ickjvbroqtuzleyzpdmfgsahgw
icxjvbroqtunledzpdmwksahgz
icxjvlroqtsnleyzpdmfksvhgw
icxjvbroqtunleyzpdsfysahvw
icxjvbroqtunwnyzydmfksahgw
ionjvbroqtunleyzpdmfksahgj
icxjvwriqtunleyzpdmfksahgi
ocxjvbroztunleyzpdmfksapgw
icxjvbroqtmnlewzpumfksahgw
ucxjvbroqtunleyzpdmzktahgw
icxgvbroqtunleyztdmfktahgw
icxhvbroqttnleybpdmfksahgw
icxjvbroqtugleyzpdxfkeahgw
acxjvbroqvunlexzpdmfksahgw
icxjvbroqglnleyzpbmfksahgw
icxjvbriqtnvleyzpdmfksahgw
icxjvbreqtunlakzpdmfksahgw
gcxjvbuoqtunleyzpdmfksawgw
icxjvbroqtunleyzpddfkyzhgw
icxjvbjoqtunleyzpdmfqsahhw
icxjvjroqtunleyzpnmfksajgw
ycxjvbroqtunmeyzcdmfksahgw
irxkvbryqtunleyzpdmfksahgw
isxjvbrlqtunleyzpdmsksahgw
icxjvbcoqtunleyzpdfkksahgw
ixnjvbroqtunleyzpdmfkqahgw
wcxjvbroqhunleyzqdmfksahgw
icljvurmqtunleyzpdmfksahgw
ibxjvbroqtayleyzpdmfksahgw
arxjvbroqiunleyzpdmfksahgw
iuxjvbroqtunluyzpdmoksahgw
icxjvbrmqtunleyzpdmfosahew
isxjvbroqtunleyrpdmfksrhgw
icxjvxrpqtunleyzpdmfkdahgw
ichjvbrogtunllyzpdmfksahgw
icxjvbeoqtunlryzpdmfksakgw
icxjvbroqtcnemyzpdmfksahgw
icxjvbroqtybledzpdmfksahgw
icxjvbrqqtunleyzpdmfksgngw
icgjvbroqtunleyzmdmfksabgw
icxjtbroqtunleyzkdmfksahww
icxjvbfoqtunleyzpamfqsahgw
icxjvbroknuyleyzpdmfksahgw
icxjvbroqtujleyzpdmaksaigw
icxjvbroqtnnlmyzpdmflsahgw
icxjvbroqtunlefzpdmfsfahgw
icxjvdroqtusleyzpdzfksahgw
icxjvbioqtunlsyzpdmfkshhgw
icxbvbrodtunleyzpdmoksahgw
icxjvbroqtuvleyzpdmfkbahmw
iyxjvbroqtunljvzpdmfksahgw
icxjvbroqtudleynddmfksahgw
icxjvwroqtnnleyzpdmfksahgz
ichjvbroqtunleyzjdmeksahgw
icxjvbrostunluyrpdmfksahgw
icfjvbroqtunleyxpdgfksahgw
nhxjvbroqtunlerzpdmfksahgw
icxjvbrothunlexzpdmfksahgw
icxjvbrzltqnleyzpdmfksahgw
icxjvbrhqtunleyzpdmfksajgy
vcxjvjroqiunleyzpdmfksahgw
icxjfbroltunleyzpdmqksahgw
icxbvbroqtunleyzpdofasahgw
icxjvbkoqtunveyzpdmfksaqgw
icxsebroqtunleyzpdmuksahgw
icxjvbroquunlpyrpdmfksahgw
icxhvbroqtunjeyzpdmrksahgw
icdjvbroqtunlzyzpdmfksangw
jcxqvbroqtvnleyzpdmfksahgw
icxjvxroqtunleyrpdmfxsahgw
icxjvnroqtunleyzpdmfssyhgw
icxjvbraptunleyzpdofksahgw
icxjvbroatunleyjpdmfbsahgw
icxjvbroytlnlryzpdmfksahgw
iaxjvbroqkunleyzpdmfcsahgw
ucxjvbroqtuileyzzdmfksahgw
icxjqbroqtcnleyzpgmfksahgw
icxjvbloqtunleyzadmfksaqgw
icxjvbroqtunleyzkdmnksakgw
icxjvbroqtunleyjpdxfksahvw
iqxjvbroqtujleyzpdmfklahgw
icgjvbroqtunleyzpdmfksbhgb
icxjzbroqtunleyzpdmfkdahgg
icxjvbrobtunloywpdmfksahgw
icxavbroqtunleyfpdmfksahgd
icxjvbroqtunleyophmfksahkw
icxjndroqtunlyyzpdmfksahgw
icxjvbroqtjnleyppdmvksahgw
icxjvbroonfnleyzpdmfksahgw
icxjvbrqqtlnljyzpdmfksahgw
icxjvbrzqtunlelspdmfksahgw
icxjvbooqtunleyztdmfkfahgw
icajvbroltunlnyzpdmfksahgw
icxjvbroqtunleyzidmdkschgw
icxjvbroktupleyzpdmfksahyw
icxjcbroyeunleyzpdmfksahgw
icxjvbroqtunlezkpdmfksahsw
icxjvbroqtunlejzpcmfksrhgw
icxjvvroqtunlsyzkdmfksahgw
icxjnbroqtunbeyzpdmfpsahgw
itxjbbroqtunleyzpemfksahgw
icxjvbroqtunlrkzpdmfksshgw
rcxjvbroqtujlbyzpdmfksahgw
icxjvmroqtugleazpdmfksahgw
icxjvbfoqtunleylpdmfkeahgw
icnjvoroktunleyzpdmfksahgw
icxjvbroqtunlvyzpdmfkfahgr
icxjvbroqtgnseyzpdmfxsahgw
scxjvbroqtunleycpdmfksdhgw
icxhvbxoqtunleuzpdmfksahgw
icxjvbruqtunleyzpnmfksfhgw
icdjvbroqtunleyzpdmfksahup
ihxjvbroqtunleovpdmfksahgw
icxjvbroqtunleyzxdmfksahzv
ocxjvbioqtunleyzpdmfzsahgw
idxjvbroqtunlyyzpdofksahgw
izdjvbroqtunleyzpdtfksahgw
icxjvbrnqtunleyzpdmfksbhgb
icxjvbrjqtunleyhpdmrksahgw
icxjvbroqtunleyzpdbflsahgg
icxjvbmfqtunleyzpdmfkaahgw
idxjvbroqtunlsyzpdffksahgw
bcxjvbroqtunleyzpkmfkswhgw
ivxjvbroqtdnleyzpdmbksahgw
icxpvbboqtunleyzpdmfksahtw
ibxjvbroqtunlehzpdmfkmahgw
icxjvbboqtunleyzpdmfkaahgv
icxjlaroqtuileyzpdmfksahgw
icxjvbroftunleyzpdmfqsahew
ichjvbroqtunleyzpdmiwsahgw
icxrvbvoqtunleyzpdmiksahgw
icxjvbroqtunldydpdmfksahgl
icogvbroqtunleyzpdmfnsahgw
icxjvbroqtunleszodmfkswhgw
icxjvbrontunleyztemfksahgw
icxjvbrovtunleyzpdvkksahgw
icxjvbroqqucteyzpdmfksahgw
icmovbroptunleyzpdmfksahgw
icxjvbqoftunleyzvdmfksahgw
icxjvbdoqtunleyzpdmfkadhgw
icxjvbroqtunlgnzpdmfksaqgw
icxjvbroqtunieygpdyfksahgw
acdjvbroqtunleyzpdmfkwahgw
icxjvbroqtunleyzpdmfkfahlj
icxjvbgoqtunleyepdmvksahgw
icxjvbpobbunleyzpdmfksahgw
icxjvbroqtunleurpdmfktahgw
ipxjvbzoqtunleyzpdmffsahgw
icxjtbroqtunllyzpdmuksahgw
icxjvbroqtunbsyzadmfksahgw
ihxjvoroqtuqleyzpdmfksahgw
idxjmbroqqunleyzpdmfksahgw
wcxjvbdoqtunleyzpdmfksahgr
icxjvbroqtunleygptmfksahgj
ipxjvbrsqtunleyzpdmfksghgw
ycxjvbroqtunluyzkdmfksahgw
icxjvbroxtuulejzpdmfksahgw
icqjvbroqtunlwyypdmfksahgw
ioxjhbroqtunleyzphmfksahgw
icxjvbgoqnunleyzpdmfksahaw
mcxjvbroqtunleyzpdmfksihgh
icxjsbroqtunlqyzpdmfksawgw
icxjvbroqtuoleycpdmftsahgw
icxjvbroqtunleyzgdifksahlw
icxjvbmoqtunleyzjfmfksahgw
icxjvbroqtunlezopdmfksahge
icxjvbroqtbnlefzpdmfosahgw
tcxjvbromtunlevzpdmfksahgw
irxjgbroqtunleyzpdmfksthgw
icxjvbrojtunleyxpdmoksahgw
icxrvbroytpnleyzpdmfksahgw
icxjvbroqtunfeyupdmfksasgw
ihqjvbroqtunleyzpdmftsahgw
icxjobroqkunleozpdmfksahgw
icjjjbroqtualeyzpdmfksahgw
icxjvbroqtunaeytpdmfksahvw
icxjvbroqtunzeyzpdmfkshhxw
icxqvbroqtucleyzxdmfksahgw
icxjvbrogturleyzxdmfksahgw
icxjvoqoqtunleyzpdcfksahgw
iuxjvbroqtunleyzpdmfksopgw
icxjveroqtunleyzptmfksalgw
icxjvbroqtunleyzpdmfwcahhw
iwxjvbroqtlnleyzpdmfksyhgw
ectjvbroqtanleyzpdmfksahgw
icxjvnroqtyhleyzpdmfksahgw
icvjvhboqtunleyzpdmfksahgw
icxjtbroqtuzleyupdmfksahgw
icjjvproqtunleyzpsmfksahgw
icdjvbroqtutleyzpdmiksahgw
icxjvwroqtujleyzpdmfksahgc
icxjxbroqtunleyzpdwhksahgw
icxjvbqoqtunleyzpdmvfsahgw
icajvbroqtusleyzpdmfksaagw
icxjvbroqtunbtyzpdmfksmhgw
kcxjvbroqtxnleyzpdmfkskhgw
icxjvbqogfunleyzpdmfksahgw
icxjvbroqtubleyzpdmfdswhgw
icxjvprlqtunleyzpdmffsahgw
icxjxbroqtucleyzpdmfksakgw
dcxrvbroqtunleycpdmfksahgw
icxjvbrobtunleyzpomfksahgu
ocxrvbroqtunleyzpdmfssahgw
icxjvbroktunlejzpdmfksahzw
icxjvbrovtunleyzmdmfkhahgw
icxjvbroqtudleygpdmfksfhgw
bcxjvbroqtubllyzpdmfksahgw
icxwvbrontunzeyzpdmfksahgw
icxjvbroqtunleysjbmfksahgw
icxjvvroztunleyzpdmfksjhgw
ivxjxbroqtunleyzpdmfksahew
icxjvbroqtunleyupqufksahgw
icxjvmrcqtunleyzpdmxksahgw
icxjvgroqtunleyzpdgfkuahgw
icxjvbroqthnqeyfpdmfksahgw
icxjsbuodtunleyzpdmfksahgw
iuxjzbroqtunleyzpdrfksahgw
icxjvbrobtunlelzpdmfksahgs
icxjvbroqtzhljyzpdmfksahgw
inxtvbroqtunleyzpdmeksahgw
icgjvbroqtunleyztdmfksahgq
icxjvagoqtugleyzpdmfksahgw
icxuvbroqtunleyzpimfkyahgw
icxzvbroqtfhleyzpdmfksahgw
icxjjbroqtqnleyzpdmnksahgw
icjrvbroqtunleszpdmfksahgw
iexjvbroqtunlgyzpdmfksacgw
rcxjvbkoqtuoleyzpdmfksahgw
icxjvbroqgunlwyzpdmfksqhgw
icxjvbroqtunleqzpsmfksqhgw
icxjvbroqtubaeyzpdmfksaugw

63
py/2018/02/solve.py Normal file
View file

@ -0,0 +1,63 @@
import sys
def load_words(filename):
words = []
with open(filename, "r") as f:
for line in f:
words.append(line[:-1])
return words
# PART 1
def has_n_chars(word, n):
count = {}
for char in word:
count[char] = count.get(char, 0) + 1
return n in count.values()
def count_words(words):
twice = 0
thrice = 0
for word in words:
if has_n_chars(word, 2):
twice += 1
if has_n_chars(word, 3):
thrice += 1
return twice, thrice
def checksum(words):
twice, thrice = count_words(words)
return twice * thrice
# PART 2
def differ_by(a, b):
count = 0
for x, y in zip(a, b):
if x != y:
count += 1
return count
def find_ids(words):
for i, a in enumerate(words):
for b in words[i:]:
if differ_by(a, b) == 1:
return a, b
def common_chars(a, b):
result = []
for x, y in zip(a, b):
if x == y:
result.append(x)
return "".join(result)
def main(filename):
words = load_words(filename)
print(f"Solutions for {filename}")
print(f"Part 1: {checksum(words)}")
a, b = find_ids(words)
print(f"Part 2: {common_chars(a, b)}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

1237
py/2018/03/input.txt Normal file

File diff suppressed because it is too large Load diff

56
py/2018/03/solve.py Normal file
View file

@ -0,0 +1,56 @@
import re
import sys
CLAIM_RE = r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)\n"
def load_claims(filename):
claims = {}
with open(filename, "r") as f:
for line in f:
match = re.fullmatch(CLAIM_RE, line)
elf, x, y, w, h = match.groups()
elf, x, y, w, h = int(elf), int(x), int(y), int(w), int(h)
claims[elf] = (x, y, w, h)
return claims
# PART 1
def count_claims(claims):
squares = {}
for x, y, w, h in claims.values():
for dw in range(w):
for dh in range(h):
coords = (x + dw, y + dh)
squares[coords] = squares.get(coords, 0) + 1
return squares
def find_doubles(squares):
return len(list(filter(lambda x: x > 1, squares.values())))
# PART 2
def is_intact(squares, x, y, w, h):
for dw in range(w):
for dh in range(h):
coords = (x + dw, y + dh)
if squares.get(coords, 0) > 1:
return False
return True
def find_intact_claim(claims, squares):
for elf, (x, y, w, h) in claims.items():
if is_intact(squares, x, y, w, h):
return elf
def main(filename):
claims = load_claims(filename)
print(f"Solutions for {filename}")
squares = count_claims(claims)
doubles = find_doubles(squares)
print(f"Part 1: {doubles}")
intact = find_intact_claim(claims, squares)
print(f"Part 2: {intact}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

1045
py/2018/04/input.txt Normal file

File diff suppressed because it is too large Load diff

115
py/2018/04/solve.py Normal file
View file

@ -0,0 +1,115 @@
import re
import sys
# PART 1
ACTION_RE = r"\[(\d+)-(\d+)-(\d+) (\d+):(\d+)\] (.*)\n"
GUARD_RE = r"Guard #(\d+) begins shift"
def load_actions(filename):
actions = []
with open(filename, "r") as f:
for line in f:
match = re.fullmatch(ACTION_RE, line)
y, m, d, hour, minute, action = match.groups()
y, m, d, hour, minute = int(y), int(m), int(d), int(hour), int(minute)
actions.append((y, m, d, hour, minute, action))
return actions
def calculate_guards(actions):
guards = {}
guard = None
asleep_since = None
for action in sorted(actions):
_, _, _, _, m, text = action
match = re.fullmatch(GUARD_RE, text)
if match:
guard = int(match.group(1))
elif text == "falls asleep":
asleep_since = m
elif text == "wakes up":
l = guards.get(guard, [])
guards[guard] = l
l.append((asleep_since, m))
return guards
def sleeps_longest(guards):
sleepiest_guard = None
sleepy_time = -1
for guard, sleep_times in guards.items():
total = 0
for start, end in sleep_times:
total += end - start
if total > sleepy_time:
sleepiest_guard = guard
sleepy_time = total
return sleepiest_guard
def sleepiest_minute(times):
counter = {}
for start, end in times:
for m in range(start, end):
counter[m] = counter.get(m, 0) + 1
max_minute = None
amount = -1
for m, n in sorted(counter.items()):
if n > amount:
max_minute = m
amount = n
return max_minute
# PART 2
def sleep_times(times):
minutes = {}
for start, end in times:
for m in range(start, end):
minutes[m] = minutes.get(m, 0) + 1
return minutes
def sleepy_minutes(guards):
minutes = {}
for guard, times in guards.items():
for m, n in sleep_times(times).items():
md = minutes.get(m, {})
minutes[m] = md
md[guard] = n
sleepy_minute = m
sleepy_guard = None
sleep_time = -1
for m, md in minutes.items():
for guard, n in md.items():
if n > sleep_time:
sleepy_minute = m
sleepy_guard = guard
sleep_time = n
return sleepy_guard, sleepy_minute
def main(filename):
actions = load_actions(filename)
guards = calculate_guards(actions)
print(f"Solutions for {filename}")
guard = sleeps_longest(guards)
minute = sleepiest_minute(guards[guard])
print(f"Part 1: {guard * minute} - Guard {guard} slept most on minute {minute}.")
guard_2, minute_2 = sleepy_minutes(guards)
print(f"Part 2: {guard_2 * minute_2} - Guard {guard_2} slept most on minute {minute_2}.")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

17
py/2018/04/test_input.txt Normal file
View file

@ -0,0 +1,17 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up

1
py/2018/05/input.txt Normal file

File diff suppressed because one or more lines are too long

84
py/2018/05/solve.py Normal file
View file

@ -0,0 +1,84 @@
import sys
# PART 1
def load_polymer(filename):
pols = []
with open(filename, "r") as f:
for line in f:
pols.append(list(line[:-1]))
return pols
def naive_react(pol):
for i in range(len(pol) - 1):
if pol[i].lower() == pol[i+1].lower() and pol[i] != pol[i+1]:
print("Naive react says: More reacting is possible.")
def react(pol):
while True:
i = 0
while i < len(pol) - 1:
pol_cur, pol_next = pol[i], pol[i+1]
if pol_cur.lower() == pol_next.lower() and pol_cur != pol_next:
del pol[i]
del pol[i]
i = max(0, i - 1)
else:
i += 1
else:
return
#def react(pol):
# start = 0
# while True:
# i = start
# while i < len(pol) - 1:
# if pol[i].lower() == pol[i+1].lower() and pol[i] != pol[i+1]:
# del pol[i]
# del pol[i]
# start = max(0, start - 1)
# break
# elif i == start + 1:
# start = i
# i += 1
# else:
# return
def result(pol):
l = pol.copy()
#print("".join(l))
react(l)
#print("->", "".join(l))
naive_react(l)
return len(l)
# PART 2
def removable_chars(pol):
return set(c.lower() for c in pol)
def remove(pol, char):
return [c for c in pol if c.lower() != char.lower()]
def find_obstructing(pol):
results = []
chars = removable_chars(pol)
for c in sorted(chars):
l = remove(pol, c)
n = result(l)
print("Removed", c, "-> length", n)
results.append(n)
return min(results)
def main(filename):
print(f"Solutions for {filename}")
pols = load_polymer(filename)
for pol in pols:
length = result(pol)
print(f"Part 1: {length}")
best_result = find_obstructing(pol)
print(f"Part 2: {best_result}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -0,0 +1 @@
dabAcCaCBAcCcaDA

View file

@ -0,0 +1,5 @@
aA
abBA
abAB
aabAAB
dabAcCaCBAcCcaDA

50
py/2018/06/input.txt Normal file
View file

@ -0,0 +1,50 @@
83, 153
201, 74
291, 245
269, 271
222, 337
291, 271
173, 346
189, 184
170, 240
127, 96
76, 46
92, 182
107, 160
311, 142
247, 321
303, 295
141, 310
147, 70
48, 41
40, 276
46, 313
175, 279
149, 177
181, 189
347, 163
215, 135
103, 159
222, 304
201, 184
272, 354
113, 74
59, 231
302, 251
127, 312
259, 259
41, 244
43, 238
193, 172
147, 353
332, 316
353, 218
100, 115
111, 58
210, 108
101, 175
185, 98
256, 311
142, 41
68, 228
327, 194

124
py/2018/06/solve.py Normal file
View file

@ -0,0 +1,124 @@
import sys
# PART 1
def load_coords(filename):
coords = []
with open(filename, "r") as f:
for line in f:
x, y = line[:-1].split(", ")
x, y = int(x), int(y)
coords.append((x, y))
return coords
class Voronoi:
def __init__(self, min_x, min_y, max_x, max_y):
self.min_x = min_x
self.min_y = min_y
self.max_x = max_x
self.max_y = max_y
self.cells = {}
def add(self, pos_x, pos_y):
for x in range(self.min_x, self.max_x + 1):
for y in range(self.min_y, self.max_y + 1):
self.add_to_cell(pos_x, pos_y, x, y)
print(f"Added ({pos_x}, {pos_y})")
def add_to_cell(self, pos_x, pos_y, cell_x, cell_y):
pos = (pos_x, pos_y)
cell = (cell_x, cell_y)
dist = abs(pos_x - cell_x) + abs(pos_y - cell_y)
if cell in self.cells:
cdist, cpos = self.cells[cell]
if dist < cdist:
self.cells[cell] = (dist, pos)
elif dist == cdist and cpos is not None:
self.cells[cell] = (dist, None)
# else: pass
else:
self.cells[cell] = (dist, pos)
@classmethod
def from_coords(cls, coords):
xs = [x for x, _ in coords]
ys = [y for _, y in coords]
voro = cls(min(xs), min(ys), max(xs), max(ys))
for x, y in coords:
voro.add(x, y)
return voro
def find_ignore(self):
ignore = set()
for x in range(self.min_x, self.max_x + 1):
_, pos1 = self.cells.get((x, self.min_y))
_, pos2 = self.cells.get((x, self.max_y))
ignore.add(pos1)
ignore.add(pos2)
for y in range(self.min_y, self.max_y + 1):
_, pos1 = self.cells.get((self.min_x, y))
_, pos2 = self.cells.get((self.max_x, y))
ignore.add(pos1)
ignore.add(pos2)
return ignore
def find_largest(self):
ignore = self.find_ignore()
count = {}
for x in range(self.min_x + 1, self.max_x):
for y in range(self.min_y + 1, self.max_y):
_, pos = self.cells.get((x, y))
if pos not in ignore:
count[pos] = count.get(pos, 0) + 1
return max(count.values())
# PART 2
class Nearest:
def __init__(self, coords):
self.coords = coords
xs = [x for x, _ in self.coords]
ys = [y for _, y in self.coords]
self.min_x = min(xs)
self.min_y = min(ys)
self.max_x = max(xs)
self.max_y = max(ys)
def find(self):
cells = set()
for x in range(self.min_x, self.max_x + 1):
for y in range(self.min_y, self.max_y + 1):
if self.evaluate(x, y):
cells.add((x, y))
return len(cells)
def evaluate(self, pos_x, pos_y):
pos = (pos_x, pos_y)
dist = 0
for x, y in self.coords:
dist += abs(pos_x - x) + abs(pos_y - y)
return dist < 10000
def main(filename):
print(f"Solutions for {filename}")
coords = load_coords(filename)
voro = Voronoi.from_coords(coords)
largest = voro.find_largest()
print(f"Part 1: {largest}")
nearest = Nearest(coords)
largest_2 = nearest.find()
print(f"Part 2: {largest_2}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -0,0 +1,6 @@
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9

101
py/2018/07/input.txt Normal file
View file

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

115
py/2018/07/solve.py Normal file
View file

@ -0,0 +1,115 @@
import re
import sys
# PART 1
STEP_RE = r"Step (\S+) must be finished before step (\S+) can begin.\n"
def load_steps(filename):
steps = {}
with open(filename, "r") as f:
for line in f:
match = re.fullmatch(STEP_RE, line)
step, before = match.groups()
steps[step] = steps.get(step, set()) | {before}
return steps
def reverse_steps(steps):
reverse = {}
for step, befores in steps.items():
# Make sure that step exists in reverse
reverse[step] = reverse.get(step, set())
for before in befores:
reverse[before] = reverse.get(before, set()) | {step}
return reverse
def duration_of(step):
return ord(step) - ord("A") + 61
class Tree:
def __init__(self, steps, workers=1):
self.workers = {i: None for i in range(workers)}
self.steps = reverse_steps(steps) # Warning: Steps are reversed in Trees.
self.result = []
self.duration = 0
def working(self):
return {worker: work for worker, work in self.workers.items() if work is not None}
def find_free(self):
return {step for step, afters in self.steps.items() if len(afters) == 0}
def find_working(self):
return {step for (step, _) in self.working().values()}
def find_available(self):
return self.find_free() - self.find_working()
def remove_step(self, step):
try:
del self.steps[step]
except KeyError:
pass
for s in self.steps.values():
try:
s.remove(step)
except KeyError:
pass
def update_workers(self):
min_duration = min(duration for (_, duration) in self.working().values())
self.duration += min_duration
finished_steps = set()
# Subtract min_duration from all workers
for w, s in self.workers.items():
if s is not None:
step, duration = s
duration -= min_duration
if duration <= 0:
finished_steps.add(step)
self.remove_step(step)
self.workers[w] = None
else:
self.workers[w] = (step, duration)
self.result += list(finished_steps)
def distribute_jobs(self):
available = list(reversed(sorted(self.find_available())))
for w, s in self.workers.items():
if not available:
break
if s is None:
step = available.pop()
duration = duration_of(step)
self.workers[w] = (step, duration)
def run(self):
while self.steps:
self.distribute_jobs()
self.update_workers()
def main(filename):
print(f"Solutions for {filename}")
steps = load_steps(filename)
tree = Tree(steps, workers=1)
tree.run()
sequence = "".join(tree.result)
print(f"Part 1: {sequence}")
tree = Tree(steps, workers=5)
tree.run()
duration = tree.duration
print(f"Part 2: {duration}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -0,0 +1,7 @@
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.

1
py/2018/08/input.txt Normal file

File diff suppressed because one or more lines are too long

53
py/2018/08/solve.py Normal file
View file

@ -0,0 +1,53 @@
import sys
# PART 1
def load_stream(filename):
with open(filename) as f:
# Too lazy to do this properly
return list(reversed(list(map(int, f.read()[:-1].split(" ")))))
def parse_node(stream):
amt_nodes = stream.pop()
amt_meta = stream.pop()
nodes = []
for _ in range(amt_nodes):
nodes.append(parse_node(stream))
meta = []
for _ in range(amt_meta):
meta.append(stream.pop())
return (nodes, meta)
def sum_of_meta(node):
nodes, meta = node
return sum(meta) + sum(map(sum_of_meta, nodes))
# PART 2
def value_of(node):
nodes, meta = node
if nodes:
total = 0
for i in meta:
i -= 1
if i >= 0 and i < len(nodes):
total += value_of(nodes[i])
return total
else:
return sum(meta)
def main(filename):
print(f"Solutions for {filename}")
stream = load_stream(filename)
node = parse_node(stream)
meta = sum_of_meta(node)
print(f"Part 1: {meta}")
value = value_of(node)
print(f"Part 2: {value}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -0,0 +1 @@
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2

87
py/2018/09/solve.py Normal file
View file

@ -0,0 +1,87 @@
import sys
import collections
# PART 1
class Circle:
def __init__(self):
self.marbles = collections.deque([0])
#self.marbles = [0]
self.next = 1
def move_focus(self, steps):
self.marbles.rotate(steps)
#self.marbles = self.marbles[steps:] + self.marbles[:steps]
def insert(self):
self.marbles.append(self.next)
#self.marbles = [self.next] + self.marbles
self.next += 1
def remove(self):
return self.marbles.pop()
#n = self.marbles[0]
#self.marbles = self.marbles[1:]
#return n
def insert_marble(self):
# returns the two marbles removed in a tuple, or None
if self.next % 23 == 0:
return self.insert_multiple()
else:
self.insert_normal()
return None
def insert_normal(self):
self.move_focus(2)
self.insert()
def insert_multiple(self):
cur = self.next
self.next += 1
self.move_focus(-7)
left = self.remove()
return (cur, left)
class Game:
def __init__(self, elves, until):
self.circle = Circle()
self.until = until
self.elves = [0 for _ in range(elves)]
def play(self):
while True:
for elf in range(len(self.elves)):
if self.circle.next % 10000 == 0:
print(f"{self.circle.next:5} of {self.until:5} - {100 * self.circle.next / self.until:.04}% {len(self.circle.marbles):8}")
if self.circle.next > self.until:
return
result = self.circle.insert_marble()
if result is not None:
fst, snd = result
self.elves[elf] += fst + snd
def highscore(self):
return max(self.elves)
# PART 2
def main(elves, until):
game = Game(elves, until)
game.play()
score = game.highscore()
print(score)
if __name__ == "__main__":
#main( 9, 25)
#main(10, 1618)
#main(13, 7999)
#main(17, 1104)
#main(21, 6111)
#main(30, 5807)
main(429, 70901)
main(429, 70901*100)

359
py/2018/10/input.txt Normal file
View file

@ -0,0 +1,359 @@
position=< 41710, 52012> velocity=<-4, -5>
position=<-20558, -20616> velocity=< 2, 2>
position=<-41271, 52017> velocity=< 4, -5>
position=< 31365, -41361> velocity=<-3, 4>
position=< 20944, 41633> velocity=<-2, -4>
position=< 10588, -20613> velocity=<-1, 2>
position=<-20510, -10238> velocity=< 2, 1>
position=< 52099, 41642> velocity=<-5, -4>
position=< 10584, 10510> velocity=<-1, -1>
position=< 41734, 20883> velocity=<-4, -2>
position=<-51679, -51742> velocity=< 5, 5>
position=<-20542, 52017> velocity=< 2, -5>
position=< 41732, -10237> velocity=<-4, 1>
position=<-30892, 10517> velocity=< 3, -1>
position=<-51663, -30984> velocity=< 5, 3>
position=< 41733, 10512> velocity=<-4, -1>
position=< 41735, -10238> velocity=<-4, 1>
position=< 20990, 52017> velocity=<-2, -5>
position=< 31370, -10233> velocity=<-3, 1>
position=< 52096, 20883> velocity=<-5, -2>
position=<-51631, 31266> velocity=< 5, -3>
position=< 41720, 41641> velocity=<-4, -4>
position=< 31325, -51739> velocity=<-3, 5>
position=<-30884, 41634> velocity=< 3, -4>
position=<-41257, -51736> velocity=< 4, 5>
position=<-51643, 41634> velocity=< 5, -4>
position=< 41748, -41359> velocity=<-4, 4>
position=<-10130, 52009> velocity=< 1, -5>
position=< 10588, 31261> velocity=<-1, -3>
position=< 10569, -20613> velocity=<-1, 2>
position=<-10130, -51738> velocity=< 1, 5>
position=<-10155, 31258> velocity=< 1, -3>
position=< 10567, 31265> velocity=<-1, -3>
position=<-51675, 20883> velocity=< 5, -2>
position=<-30909, 10515> velocity=< 3, -1>
position=<-10155, 31259> velocity=< 1, -3>
position=< 52108, -30988> velocity=<-5, 3>
position=<-20497, 31260> velocity=< 2, -3>
position=< 10588, 52010> velocity=<-1, -5>
position=< 41708, 20888> velocity=<-4, -2>
position=< 52107, -20611> velocity=<-5, 2>
position=< 10571, -30988> velocity=<-1, 3>
position=< 20975, 20892> velocity=<-2, -2>
position=< 10620, 20889> velocity=<-1, -2>
position=< 52096, 41633> velocity=<-5, -4>
position=< 20984, 10512> velocity=<-2, -1>
position=< 31327, 20887> velocity=<-3, -2>
position=<-30920, -20615> velocity=< 3, 2>
position=< 31370, 10512> velocity=<-3, -1>
position=<-41290, 10512> velocity=< 4, -1>
position=< 52118, 41638> velocity=<-5, -4>
position=< 52083, 31259> velocity=<-5, -3>
position=<-51630, -20612> velocity=< 5, 2>
position=< 41711, 31264> velocity=<-4, -3>
position=<-30912, -10233> velocity=< 3, 1>
position=<-51646, -51742> velocity=< 5, 5>
position=< 52123, 10515> velocity=<-5, -1>
position=< 52120, 31267> velocity=<-5, -3>
position=< 20998, -30983> velocity=<-2, 3>
position=< 10601, 52017> velocity=<-1, -5>
position=<-30933, 52012> velocity=< 3, -5>
position=<-51667, -20617> velocity=< 5, 2>
position=< 20975, -20617> velocity=<-2, 2>
position=<-41308, 41633> velocity=< 4, -4>
position=<-51655, -51739> velocity=< 5, 5>
position=<-30893, -10238> velocity=< 3, 1>
position=<-51630, -10239> velocity=< 5, 1>
position=< 52067, 10513> velocity=<-5, -1>
position=< 41742, -41364> velocity=<-4, 4>
position=<-51683, 52015> velocity=< 5, -5>
position=<-10122, 52009> velocity=< 1, -5>
position=<-20549, -20613> velocity=< 2, 2>
position=<-51627, 31262> velocity=< 5, -3>
position=<-41256, 10515> velocity=< 4, -1>
position=< 20950, 31258> velocity=<-2, -3>
position=< 41697, -10241> velocity=<-4, 1>
position=<-41247, 52013> velocity=< 4, -5>
position=<-20530, 41639> velocity=< 2, -4>
position=<-30893, 20890> velocity=< 3, -2>
position=<-20505, 52015> velocity=< 2, -5>
position=< 41702, 52008> velocity=<-4, -5>
position=<-10179, 41637> velocity=< 1, -4>
position=< 41703, 20887> velocity=<-4, -2>
position=<-10139, -20613> velocity=< 1, 2>
position=< 41748, -51738> velocity=<-4, 5>
position=< 10618, -30986> velocity=<-1, 3>
position=< 31329, 20887> velocity=<-3, -2>
position=<-41259, 31260> velocity=< 4, -3>
position=< 10615, 10513> velocity=<-1, -1>
position=< 31345, 20890> velocity=<-3, -2>
position=< 52070, -20617> velocity=<-5, 2>
position=<-30892, -30992> velocity=< 3, 3>
position=< 41748, -51735> velocity=<-4, 5>
position=<-51650, -20617> velocity=< 5, 2>
position=< 10567, 20883> velocity=<-1, -2>
position=<-30893, -10234> velocity=< 3, 1>
position=<-20554, 20887> velocity=< 2, -2>
position=< 41753, -20614> velocity=<-4, 2>
position=<-51649, 10512> velocity=< 5, -1>
position=< 10594, -20608> velocity=<-1, 2>
position=<-10130, 41635> velocity=< 1, -4>
position=< 41708, -41359> velocity=<-4, 4>
position=<-10127, 41637> velocity=< 1, -4>
position=<-30877, -30989> velocity=< 3, 3>
position=<-20558, -10234> velocity=< 2, 1>
position=< 10567, -30991> velocity=<-1, 3>
position=< 31357, 31267> velocity=<-3, -3>
position=<-51622, -41363> velocity=< 5, 4>
position=<-20522, -41358> velocity=< 2, 4>
position=<-20502, 10513> velocity=< 2, -1>
position=< 41718, 31267> velocity=<-4, -3>
position=<-30928, 52009> velocity=< 3, -5>
position=<-10174, 10508> velocity=< 1, -1>
position=<-20550, 10517> velocity=< 2, -1>
position=< 10607, 10516> velocity=<-1, -1>
position=< 31373, -51734> velocity=<-3, 5>
position=< 41708, 41636> velocity=<-4, -4>
position=< 20978, 31258> velocity=<-2, -3>
position=< 52108, 41642> velocity=<-5, -4>
position=< 10623, -51741> velocity=<-1, 5>
position=<-51675, 52009> velocity=< 5, -5>
position=< 20977, -30983> velocity=<-2, 3>
position=<-41268, 52008> velocity=< 4, -5>
position=< 41713, 31258> velocity=<-4, -3>
position=<-10138, -30983> velocity=< 1, 3>
position=<-51635, -51738> velocity=< 5, 5>
position=< 52120, 52014> velocity=<-5, -5>
position=<-41300, 31265> velocity=< 4, -3>
position=< 41713, -51741> velocity=<-4, 5>
position=<-30905, 20888> velocity=< 3, -2>
position=<-30898, 10508> velocity=< 3, -1>
position=<-20523, -51742> velocity=< 2, 5>
position=< 31336, -20612> velocity=<-3, 2>
position=<-51622, 52012> velocity=< 5, -5>
position=< 10595, -20613> velocity=<-1, 2>
position=<-51635, 52010> velocity=< 5, -5>
position=< 20976, -51742> velocity=<-2, 5>
position=<-20510, 31259> velocity=< 2, -3>
position=< 31320, 41633> velocity=<-3, -4>
position=< 31351, -30992> velocity=<-3, 3>
position=< 41705, -41366> velocity=<-4, 4>
position=< 41741, -30991> velocity=<-4, 3>
position=< 20995, -51735> velocity=<-2, 5>
position=<-51675, -10239> velocity=< 5, 1>
position=< 31349, -30990> velocity=<-3, 3>
position=<-30896, -30992> velocity=< 3, 3>
position=< 20963, 10517> velocity=<-2, -1>
position=< 52128, -20610> velocity=<-5, 2>
position=<-20522, -41358> velocity=< 2, 4>
position=<-51683, -51739> velocity=< 5, 5>
position=< 10610, 52017> velocity=<-1, -5>
position=< 31361, 41633> velocity=<-3, -4>
position=< 31334, -41365> velocity=<-3, 4>
position=< 20958, 52017> velocity=<-2, -5>
position=<-10146, -20617> velocity=< 1, 2>
position=<-51643, 31258> velocity=< 5, -3>
position=<-10155, -30987> velocity=< 1, 3>
position=<-20550, -20611> velocity=< 2, 2>
position=< 31349, 31264> velocity=<-3, -3>
position=< 10620, 20888> velocity=<-1, -2>
position=<-10183, -10236> velocity=< 1, 1>
position=<-10162, -51734> velocity=< 1, 5>
position=<-41266, -10242> velocity=< 4, 1>
position=<-30933, 10510> velocity=< 3, -1>
position=<-30872, 10516> velocity=< 3, -1>
position=< 20976, -10238> velocity=<-2, 1>
position=<-30877, -20614> velocity=< 3, 2>
position=<-41247, 52011> velocity=< 4, -5>
position=<-10151, 10509> velocity=< 1, -1>
position=< 10600, -41363> velocity=<-1, 4>
position=<-20510, 41639> velocity=< 2, -4>
position=<-30914, 31263> velocity=< 3, -3>
position=< 52083, 41637> velocity=<-5, -4>
position=<-10143, -30986> velocity=< 1, 3>
position=<-51675, -41365> velocity=< 5, 4>
position=<-41287, 41640> velocity=< 4, -4>
position=< 41705, 10510> velocity=<-4, -1>
position=< 10595, -41364> velocity=<-1, 4>
position=<-10122, 20891> velocity=< 1, -2>
position=<-51657, -10233> velocity=< 5, 1>
position=< 20970, 20887> velocity=<-2, -2>
position=< 20960, 52011> velocity=<-2, -5>
position=< 10588, 10515> velocity=<-1, -1>
position=< 10607, -20609> velocity=<-1, 2>
position=<-41295, 20884> velocity=< 4, -2>
position=< 52075, -51738> velocity=<-5, 5>
position=< 52115, 20890> velocity=<-5, -2>
position=<-41268, 41635> velocity=< 4, -4>
position=< 41705, -41366> velocity=<-4, 4>
position=<-41287, -51740> velocity=< 4, 5>
position=< 20977, -51738> velocity=<-2, 5>
position=< 20960, -10239> velocity=<-2, 1>
position=<-41265, 31267> velocity=< 4, -3>
position=<-20524, -30983> velocity=< 2, 3>
position=<-30898, -51742> velocity=< 3, 5>
position=<-51666, -30990> velocity=< 5, 3>
position=<-20497, -41366> velocity=< 2, 4>
position=< 10595, 10508> velocity=<-1, -1>
position=< 41752, -20613> velocity=<-4, 2>
position=<-30893, 41633> velocity=< 3, -4>
position=< 41692, -51742> velocity=<-4, 5>
position=< 31338, -30987> velocity=<-3, 3>
position=< 41692, -51741> velocity=<-4, 5>
position=<-10172, -10242> velocity=< 1, 1>
position=< 41728, 31262> velocity=<-4, -3>
position=< 20970, -10234> velocity=<-2, 1>
position=<-30872, -10241> velocity=< 3, 1>
position=< 31359, -10238> velocity=<-3, 1>
position=<-10175, -30992> velocity=< 1, 3>
position=< 41740, 52016> velocity=<-4, -5>
position=< 10599, -20612> velocity=<-1, 2>
position=<-20534, -30985> velocity=< 2, 3>
position=<-30893, -30991> velocity=< 3, 3>
position=< 10568, -10242> velocity=<-1, 1>
position=< 10603, 52017> velocity=<-1, -5>
position=<-20537, 52011> velocity=< 2, -5>
position=<-10174, 20887> velocity=< 1, -2>
position=<-30904, -20617> velocity=< 3, 2>
position=< 20982, -51741> velocity=<-2, 5>
position=< 31373, 20883> velocity=<-3, -2>
position=<-10143, 10514> velocity=< 1, -1>
position=<-41292, 10515> velocity=< 4, -1>
position=< 10599, -30990> velocity=<-1, 3>
position=< 52128, 41637> velocity=<-5, -4>
position=< 52115, 10517> velocity=<-5, -1>
position=<-30893, -51739> velocity=< 3, 5>
position=< 52123, -10237> velocity=<-5, 1>
position=<-10175, -20616> velocity=< 1, 2>
position=< 20974, 20891> velocity=<-2, -2>
position=< 21003, 52014> velocity=<-2, -5>
position=<-41255, -41359> velocity=< 4, 4>
position=<-41268, -30989> velocity=< 4, 3>
position=< 41700, -30989> velocity=<-4, 3>
position=< 10599, 31261> velocity=<-1, -3>
position=< 31343, -51733> velocity=<-3, 5>
position=<-20497, -51735> velocity=< 2, 5>
position=<-20517, -30983> velocity=< 2, 3>
position=< 52102, -20608> velocity=<-5, 2>
position=<-30900, 20883> velocity=< 3, -2>
position=<-51651, 20883> velocity=< 5, -2>
position=<-30898, -41358> velocity=< 3, 4>
position=< 52084, -10241> velocity=<-5, 1>
position=< 41708, 10512> velocity=<-4, -1>
position=<-20556, 52008> velocity=< 2, -5>
position=< 31325, -30986> velocity=<-3, 3>
position=<-30925, 20888> velocity=< 3, -2>
position=<-10151, -20616> velocity=< 1, 2>
position=< 52101, -20617> velocity=<-5, 2>
position=< 52072, -20615> velocity=<-5, 2>
position=< 21003, 10517> velocity=<-2, -1>
position=<-51658, -30983> velocity=< 5, 3>
position=<-30933, 52013> velocity=< 3, -5>
position=<-51630, 10516> velocity=< 5, -1>
position=<-20531, -10233> velocity=< 2, 1>
position=<-30933, -10238> velocity=< 3, 1>
position=< 10584, 10509> velocity=<-1, -1>
position=< 20994, 41641> velocity=<-2, -4>
position=< 52091, 31265> velocity=<-5, -3>
position=<-10130, -51741> velocity=< 1, 5>
position=<-30885, 31261> velocity=< 3, -3>
position=<-41276, -30985> velocity=< 4, 3>
position=< 41751, -20613> velocity=<-4, 2>
position=<-41272, -20613> velocity=< 4, 2>
position=<-30901, -51739> velocity=< 3, 5>
position=<-20501, 31262> velocity=< 2, -3>
position=<-10130, -51742> velocity=< 1, 5>
position=<-41266, 20883> velocity=< 4, -2>
position=<-30877, 10516> velocity=< 3, -1>
position=<-10156, -20617> velocity=< 1, 2>
position=< 10609, 31262> velocity=<-1, -3>
position=< 41695, 41637> velocity=<-4, -4>
position=<-41252, 20892> velocity=< 4, -2>
position=< 52123, -41365> velocity=<-5, 4>
position=< 41740, -30987> velocity=<-4, 3>
position=<-41275, 20892> velocity=< 4, -2>
position=< 20970, -30990> velocity=<-2, 3>
position=< 10569, 52008> velocity=<-1, -5>
position=<-41295, -51739> velocity=< 4, 5>
position=<-20541, -51741> velocity=< 2, 5>
position=<-30921, 20883> velocity=< 3, -2>
position=<-20502, 41639> velocity=< 2, -4>
position=<-30930, 20883> velocity=< 3, -2>
position=< 20975, -10233> velocity=<-2, 1>
position=< 31362, -51742> velocity=<-3, 5>
position=<-20505, -41366> velocity=< 2, 4>
position=< 41753, 52008> velocity=<-4, -5>
position=<-41252, -41361> velocity=< 4, 4>
position=<-41276, 20886> velocity=< 4, -2>
position=<-30892, 31262> velocity=< 3, -3>
position=< 52099, -30986> velocity=<-5, 3>
position=<-30912, 31261> velocity=< 3, -3>
position=< 31365, 41636> velocity=<-3, -4>
position=<-41292, -10242> velocity=< 4, 1>
position=< 10583, 41639> velocity=<-1, -4>
position=< 52123, 10510> velocity=<-5, -1>
position=<-20553, 10509> velocity=< 2, -1>
position=<-41257, -30987> velocity=< 4, 3>
position=< 52083, -41358> velocity=<-5, 4>
position=<-30885, -51741> velocity=< 3, 5>
position=< 41713, -41365> velocity=<-4, 4>
position=< 31360, 31258> velocity=<-3, -3>
position=< 52099, -30992> velocity=<-5, 3>
position=<-41255, 41637> velocity=< 4, -4>
position=<-30932, 20887> velocity=< 3, -2>
position=< 10599, 41633> velocity=<-1, -4>
position=<-51683, 52010> velocity=< 5, -5>
position=< 41745, 31258> velocity=<-4, -3>
position=< 20991, 31260> velocity=<-2, -3>
position=<-30925, -10234> velocity=< 3, 1>
position=< 20974, 20889> velocity=<-2, -2>
position=<-30921, -20617> velocity=< 3, 2>
position=<-30893, 41635> velocity=< 3, -4>
position=< 41736, -41358> velocity=<-4, 4>
position=<-51627, -41358> velocity=< 5, 4>
position=< 10595, 41633> velocity=<-1, -4>
position=<-20498, 20887> velocity=< 2, -2>
position=<-10167, 10510> velocity=< 1, -1>
position=<-51675, 10510> velocity=< 5, -1>
position=<-41287, 10514> velocity=< 4, -1>
position=< 20992, -51738> velocity=<-2, 5>
position=< 31321, -10242> velocity=<-3, 1>
position=< 20990, -51741> velocity=<-2, 5>
position=< 10585, -20613> velocity=<-1, 2>
position=<-10151, 10510> velocity=< 1, -1>
position=<-51643, -10237> velocity=< 5, 1>
position=<-10183, 20892> velocity=< 1, -2>
position=< 31317, 20892> velocity=<-3, -2>
position=<-30905, -10236> velocity=< 3, 1>
position=<-51627, 20885> velocity=< 5, -2>
position=< 31341, 41641> velocity=<-3, -4>
position=< 52109, 52017> velocity=<-5, -5>
position=<-30893, 31265> velocity=< 3, -3>
position=< 41724, 31262> velocity=<-4, -3>
position=<-30933, -51740> velocity=< 3, 5>
position=<-10163, -41360> velocity=< 1, 4>
position=<-20542, 20885> velocity=< 2, -2>
position=< 52123, -41362> velocity=<-5, 4>
position=< 10615, 10508> velocity=<-1, -1>
position=< 52086, -10237> velocity=<-5, 1>
position=< 10588, 41633> velocity=<-1, -4>
position=<-41268, -41358> velocity=< 4, 4>
position=<-41255, 20888> velocity=< 4, -2>
position=< 41753, 20889> velocity=<-4, -2>
position=<-41260, -20611> velocity=< 4, 2>
position=<-41308, -10233> velocity=< 4, 1>
position=<-10162, 10512> velocity=< 1, -1>
position=< 10594, -51733> velocity=<-1, 5>
position=< 20978, -20617> velocity=<-2, 2>
position=< 10576, -41367> velocity=<-1, 4>
position=< 31338, -30988> velocity=<-3, 3>
position=<-30917, 10508> velocity=< 3, -1>
position=< 41732, 20885> velocity=<-4, -2>
position=< 10595, 52010> velocity=<-1, -5>
position=< 52125, -41363> velocity=<-5, 4>
position=< 20974, -20610> velocity=<-2, 2>
position=< 10572, 52011> velocity=<-1, -5>
position=< 20958, 10514> velocity=<-2, -1>
position=<-30885, -10234> velocity=< 3, 1>
position=< 31320, 52012> velocity=<-3, -5>

86
py/2018/10/solve.py Normal file
View file

@ -0,0 +1,86 @@
import re
import sys
# PART 1
POINT_RE = r"position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>\n"
class Point:
def __init__(self, pos, vel):
self.pos = pos
self.vel = vel
@classmethod
def from_line(cls, line):
match = re.fullmatch(POINT_RE, line)
x, y, dx, dy = map(int, match.groups())
return cls((x, y), (dx, dy))
def step(self):
x, y = self.pos
dx, dy = self.vel
self.pos = (x + dx, y + dy)
class Field:
def __init__(self, points=None):
self.points = points or []
self.steps = 0
@classmethod
def from_file(cls, filename):
points = []
with open(filename, "r") as f:
for line in f:
points.append(Point.from_line(line))
return cls(points=points)
def step(self):
for point in self.points:
point.step()
self.steps += 1
def step_until_in_limits(self):
while True:
coords = set(point.pos for point in self.points)
xs = set(x for (x, _) in coords)
ys = set(y for (_, y) in coords)
if max(xs) - min(xs) < 500:
return
self.step()
print(min(xs), max(xs), min(ys), max(ys))
def render(self):
coords = set(point.pos for point in self.points)
xs = set(x for (x, _) in coords)
ys = set(y for (_, y) in coords)
for y in range(min(ys), max(ys) + 1):
for x in range(min(xs), max(xs) + 1):
if (x, y) in coords:
print("#", end="")
else:
print(".", end="")
print()
print(self.steps)
# PART 2
def main(filename):
print(f"Solutions for {filename}")
print("Part 1:")
field = Field.from_file(filename)
field.step_until_in_limits()
while input() == "":
field.render()
field.step()
print("Part 2: NYI")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

79
py/2018/11/solve.py Normal file
View file

@ -0,0 +1,79 @@
import sys
# PART 1
def nth_digit(num, pos):
# nth_digit(1234, 0) = 123[4] = 4
# nth_digit(1234, 2) = 1[2]34 = 2
# nth_digit(1234, 4) = [0]1234 = 0
pos += 1
s = ("{:0" + str(pos) + "}").format(num) # UGLY!
n = s[-pos]
return int(n)
def power_level(number, x, y):
rack_id = x + 10
level = rack_id * y
level += number
level *= rack_id
level = nth_digit(level, 2) # 100-er digit
level -= 5
return level
def init_grid(number):
grid = {}
for x in range(1, 300 + 1):
for y in range(1, 300 + 1):
grid[(x, y)] = power_level(number, x, y)
return grid
def power_of_square(grid, x, y):
power = 0
for dx in range(3):
for dy in range(3):
power += grid[(x+dx, y+dy)]
return power
def find_max_power(grid):
max_power = 0
max_coords = None
for x in range(1, 300 - 2 + 1):
for y in range(1, 300 - 2 + 1):
power = power_of_square(grid, x, y)
if power > max_power:
max_power = power
max_coords = (x, y)
return max_coords
# PART 2
def square_power(grid, x, y, size):
pass
def max_square(grid):
max_power = 0
max_coords = None
max_size = None
for size in range(1, 300 + 1):
for x in range(1, 300 + 1 - (size - 1)):
for y in range(1, 300 + 1 - (size - 1)):
pass
def main(number):
print(f"Solutions for {number}")
grid = init_grid(number)
x, y = find_max_power(grid)
print(f"Part 1: {x},{y}")
print(f"Part 2: NYI")
if __name__ == "__main__":
print(power_level( 8, 3, 5))
print(power_level(57, 122, 79))
print(power_level(39, 217, 196))
print(power_level(71, 101, 153))
main(5153)