Merge remote-tracking branch '2018/master'

This commit is contained in:
Kienan Stewart 2022-05-16 23:23:43 -04:00
commit 2e1614a5dc
10 changed files with 4099 additions and 0 deletions

31
1/frequency.py Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
import sys
def delta_frequency(f = sys.stdin):
df = 0
frequency_count = {0: 1}
frequency_list = []
for l in f.readlines():
frequency_list.append(l)
first = None
iteration = 1
df_first_iteration = None
while first is None:
for l in frequency_list:
df += int(l)
if df not in frequency_count:
frequency_count[df] = 1
else:
frequency_count[df] = frequency_count[df] + 1
if not first:
if frequency_count[df] >= 2:
first = df
if df_first_iteration is None:
df_first_iteration = df
print(df_first_iteration)
if first is not None:
print("First duplicate frequency: {}".format(first))
if __name__ == '__main__':
delta_frequency()

957
1/input Normal file
View File

@ -0,0 +1,957 @@
-5
-2
+1
+14
+7
+5
-14
-4
-5
-12
+7
-5
+17
+5
-13
-12
+15
+22
-5
-6
-12
+20
+4
+2
+17
-1
+18
-7
-1
-17
+11
-12
-5
-2
+9
+2
-6
-17
-1
+2
-3
+15
+19
+9
-8
+13
+1
+11
+16
+3
-16
-7
-15
-15
+12
+16
+18
+1
-9
+16
-9
-19
+17
+1
-15
+13
-9
-8
-1
+7
+17
+13
+15
-17
-3
+12
-10
+5
+4
-16
+15
+3
+19
+1
-2
+19
-16
-11
+4
-10
-8
+13
+13
+19
-6
-19
+1
-4
+18
+15
+16
-18
+12
+3
-9
+8
+3
-9
+11
+4
+8
+1
+6
-10
-3
+15
+16
+15
+12
-14
+4
-14
-7
-3
+14
+4
+3
-6
-7
+11
-2
-18
+17
-3
+14
+18
+6
+8
+18
-13
-7
+17
-18
-10
-15
+13
-16
+5
-10
+15
-8
-4
+14
+13
-10
-14
+7
+5
+13
-4
+12
+3
+14
-7
+6
+12
+17
-18
+3
-6
-3
-14
-1
+13
+19
+3
+9
+4
+11
+12
+3
+1
+14
+7
-17
+12
+11
+6
-7
+4
-11
-1
+6
-19
-10
-16
-2
-19
+7
+9
-3
+5
+12
+15
+8
+11
+1
+8
+15
-9
-9
-8
-10
-3
-16
+7
-17
+9
-5
+16
-15
-4
+15
-5
-25
-6
+1
+4
+17
+19
-13
+17
+7
+19
+2
+4
+10
+16
-9
+19
+13
+3
-10
+9
+5
+1
+18
-11
-14
-4
-5
-13
-7
-12
+2
+3
+6
-16
-1
+13
-10
-4
-1
-3
+9
+22
+4
-18
+17
+11
-21
-17
-18
-8
+12
+6
+15
+12
+10
-7
+18
+10
-8
-10
-18
+11
-17
+25
+15
-9
-19
+38
-3
-6
-23
+34
-25
-5
-12
+25
+14
+17
+30
+3
+9
-8
+16
+21
+21
+4
-12
+23
-13
+9
+3
+6
+13
+15
+6
-7
+15
+12
-10
+13
+12
-7
+13
+4
-9
+18
-10
+5
+8
-7
+2
-14
-12
-1
-6
-16
-18
-3
+1
+12
-6
+15
-17
+15
+13
+6
-15
+26
+1
-21
-3
-21
-4
+14
-1
-19
-11
+13
-10
-14
+2
-17
-23
+25
-16
+8
-30
+17
-44
+13
-19
+5
-9
-12
+10
+14
+17
+24
-15
+7
-61
-103
-18
+21
-22
-6
-9
-6
-9
-7
-15
-17
+4
-1
+10
+8
+14
-4
+15
-16
-6
-16
-15
-12
+15
+10
-15
+3
+15
-10
-17
+3
-5
-12
+4
+3
-37
-7
+14
-18
-8
+6
-11
+14
+30
+7
+23
+39
-12
+11
-8
+11
-1
+56
-28
-12
+7
+34
+47
+21
+88
+61
+18
+10
-99
-287
+26
-858
-209
-61255
+2
-7
-8
+12
+18
+17
-14
-2
+14
+4
+3
+3
-15
+21
+3
+20
+1
+3
-1
-25
-2
+7
-9
+17
-19
-6
-13
+2
-4
-3
-14
-8
+12
-20
+17
-40
+11
+3
-20
-16
-18
-3
-6
-10
+4
+20
-12
-3
+11
+16
-2
+4
-9
+28
-12
+5
+17
-13
+35
+25
+2
+35
-17
-8
+31
+3
-39
-46
+3
-96
-18
-14
-14
-12
+9
+15
+3
+18
-8
+1
-2
-15
+13
+14
-13
-3
+7
-11
-15
-17
-12
-15
-17
+9
+7
-10
-8
-3
+9
+16
+3
+15
-14
-5
-9
+1
-3
+10
+3
-18
-21
-11
+2
-17
-17
-7
-11
+8
+11
-2
-7
-1
-19
-2
-16
+1
+5
+11
+18
+5
+17
+2
-5
-2
+19
-4
+12
+2
+20
-13
-20
-12
+10
+4
+14
-1
+12
+13
-10
-13
-8
-1
+14
+16
+6
+10
+18
+19
+14
-1
+6
-13
-9
+10
-20
+1
+1
-12
+6
+7
-15
-13
-2
+14
-19
-5
-10
-17
+15
-5
+8
+20
+9
+14
-6
+4
-3
+19
+18
-14
-3
-8
+4
+16
+7
-3
-5
+19
-2
+12
+19
+12
-10
-17
-40
+1
-11
+19
-27
-9
+45
-27
+31
+1
-25
+41
+58
-5
-39
-112
+1
-8
-3
-6
-15
-3
-9
+13
+13
-24
+3
-4
+13
-19
-21
+16
-20
-10
-15
-16
-1
-13
-12
-17
+16
+17
+16
-2
-9
-1
-19
+8
+5
-18
+17
-19
-19
-12
+14
-18
-12
+10
+9
-13
-8
+19
+16
+13
-5
+2
+16
-15
+17
+9
+10
+2
-5
+20
+16
-8
-4
+16
+6
-7
-2
-11
+18
+11
-1
+2
+5
+3
+40
-38
+22
-11
-15
-27
+7
-25
+4
-11
-15
-12
+15
-20
-13
+9
-13
+7
-5
+10
+18
+24
+8
+15
-6
-10
-10
-1
-4
-3
-17
+7
-15
+20
-19
-18
-14
-4
-26
-22
-5
+17
+6
+13
+9
+7
+28
-11
-8
+57
-23
-37
-11
-43
+3
-14
+3
-14
+13
-15
+6
-23
+8
+12
-8
+15
+16
+2
-9
-14
+18
+19
-1
-43
-2
-3
-13
+5
-6
-13
-20
+15
+13
-12
+3
+2
+3
+11
+7
-17
-8
-13
+18
-17
-2
-13
+5
+3
+1
-11
+20
+4
+2
-1
-19
-8
-19
+1
-6
-12
+8
-6
-5
+1
+6
+9
-12
+13
+10
-14
+20
-11
+9
+9
-3
-18
+9
-5
+9
+2
-9
-19
-4
-10
+7
-8
+68
+30
+2
-9
+12
-65
+15
-26
-9
-15
-67
-38
-36
-37
-7
+18
-20
-44
+35
-70
-85
+14
+18
-499
+209
-61690
-5
+15
-4
+3
+5
+6
+15
+16
+3
-4
+12
-14
-7
-9
-15
+2
-1
-11
-21
-8
-2
-15
+19
+7
-10
-19
-3
-14
+4
+9
+13
-16
-14
+2
+10
-19
+6
+16
+11
+7
-14
-9
-21
-7
-12
+2
+12
-6
+125503

65
2/checksum.py Executable file
View File

@ -0,0 +1,65 @@
#!/usr/bin/env python3
import sys
def checksum(l):
double = 0
triple = 0
for item in l:
d, t = count_duplicate(item)
double += d
triple += t
return double * triple
def count_duplicate(item):
double = 0
triple = 0
c = dict()
for i in item:
if i in c:
c[i] += 1
else:
c[i] = 1
for k,v in c.items():
if v == 2:
double = 1
if v == 3:
triple = 1
#print(c)
#print("{}: {}, {}".format(item, double, triple))
return (double, triple)
def compare(item, l):
for candidate in l:
if candidate == item:
continue
diff = 0
x = 0
while x < len(item):
if item[x] != candidate[x]:
diff += 1
x += 1
if diff > 1:
break
#if diff > 1:
# print("{} does not match {}: {} different".format(item, candidate, diff))
if diff == 1:
return candidate
def common(x, y):
i = 0
common = ""
while i < len(x):
if x[i] == y[i]:
common += x[i]
i += 1
return common
if __name__ == '__main__':
l = sys.stdin.readlines()
print(checksum(l))
for item in l:
z = compare(item, l)
if z is not None:
print("{} / {}".format(item, z))
print(common(item, z))
break

250
2/input Normal file
View File

@ -0,0 +1,250 @@
qywzphxoiseldjrbaoagvkmanu
qywzphxeisulpjrbtfcgvkmanu
qywzxhooiseldjrbtfcgvcmanu
qywzphjojseldjubtfcgvkmanu
qtwzphxoieeldjrbtfcgvrmanu
tywzphzoiseldjritfcgvkmanu
qyuzphxoiseldjrbtfcgykbanu
qswzmhxoiseldjrbtfcgvkaanu
qyczqhxoiseldjrbtfcgvkbanu
qybzpqxooseldjrbtfcgvkmanu
qywzpoxoxseldjrbtfcgvpmanu
qywzphxbipeldjrbtfcgvkmaru
qywzpmxoiseldjrutqcgvkmanu
qywzphxoisesdjrrtfchvkmanu
qywzphxoiseldhrbtfcgukyanu
jywzphyoiseldjrbtfclvkmanu
qywzvhxoiselrjrbtfcgvkmanl
qywzplxojseldjrbtfcgvkmamu
qywzphxoiseldjrbtacgvkmvnd
qywpphxoiseldjrbtfcgvkvenu
qqazphxoiselqjrbtfcgvkmanu
qyozvhxoiseldjrbtfcgvkmadu
xywzphxliseldjhbtfcgvkmanu
qdwzphioiseldqrbtfcgvkmanu
qswyphxoiseldjrbtfcgvkmanx
qowzshxoiseldjrbtfigvkmanu
qywzphxoiseldjjbtfcgvkmawq
qyhzphxoiseldjrbtfvgvkmynu
qxwzphxoiselmjrbtfcgvqmanu
qywzphxoiceldjrbtfcgikmgnu
qywephxoiseldjrbthcgvkmane
yywzphxoisejdjrotfcgvkmanu
qywzxhxoisyldyrbtfcgvkmanu
qywzpjxoiseqdjrbtfcgvwmanu
qywzphxoqweldprbtfcgvkmanu
qywzphnoiseldjrbtfcamkmanu
qylgphxoiseldjrbtfcgvkmauu
qysdphxomseldjrbtfcgvkmanu
lywzpoxoikeldjrbtfcgvkmanu
qywzphxgiseldjrbtfeqvkmanu
qywzdhxozseldjcbtfcgvkmanu
qywzphxeiskedjrbtfcgvkmanu
qmwzphxoispldjrbtfcgvkmany
qywzphxoiselzcrbtfcgvkmanz
qywzphxoisxodjrbtscgvkmanu
qywzphxoiseldjrrtfcgmkmaeu
aywzphxoiseldjrbtfmjvkmanu
qywzthxoiiewdjrbtfcgvkmanu
qywzpsxoisrzdjrbtfcgvkmanu
qywzphxoiseldjkbtqcgvkmank
tywzphxoisewdjkbtfcgvkmanu
qywkchxoiseldjrbtfcghkmanu
qywzphxoiseldjoftfrgvkmanu
qywzpixoisehdjrbtfcgvkmacu
qywzchooiseldarbtfcgvkmanu
qywyphtoaseldjrbtfcgvkmanu
qywyphxotsbldjrbtfcgvkmanu
qywzphxoiseldjrbhsggvkmanu
dywzphxoiseldjrktfegvkmanu
qywzphxoiseldjrmtfcgvkcenu
qywcphxjiseldjybtfcgvkmanu
qywzphxoiseldhrbsfogvkmanu
qmwzpheoiseldjrrtfcgvkmanu
qywzrhxoiselpjrbtfcgvemanu
qyezphpoiseldjrbtfcgvdmanu
qywzphxvisewdjrbtfcgvkmdnu
qywzphkoiseldjrbtfcgvkmdnh
qywzehxoiseldfrbtpcgvkmanu
qywzphxoiseldjrrtfcgvrdanu
qpwzphxoizeldjqbtfcgvkmanu
qywzphxojseldjrbtmcgvkmvnu
vywzphxoiseldjrbtfcgvkmaop
sywzphxoiselbjrttfcgvkmanu
qywwphkoiseldjrbtfcivkmanu
qlwkpcxoiseldjrbtfcgvkmanu
qywzphxoiyesdjrbtfcgvkmvnu
qywzphxoiseldjrbofcgrkmrnu
qywzphxyiseldjrstfcgvkmjnu
qywzphaoiseldtrbnfcgvkmanu
qywzphxhisuldurbtfcgvkmanu
qywzphxdiseldjrbtvugvkmanu
qywzpzxbiseldjrbtfcgukmanu
qyrzphxoixeldjrbtfcgvumanu
qywzphxoiberdjrptfcgvkmanu
qywzphxfiskldjrbtfcgvkmdnu
qyxzphxoiseldjrdhfcgvkmanu
qywzphrqiseldjrbtfcgvbmanu
qyezphxviseldjrbtfcgvkmani
qywgphxoiseldgkbtfcgvkmanu
qywzphxoileldjrbtgcgvkdanu
qywzphxoiseldnrbtfcwvkmpnu
qywzphxoiseidjrbmfcqvkmanu
qywzpkxoiselijrbtfcgvlmanu
mywzphxoiveldjrbtfcgvkmunu
qywzphxooseddjrbtfcgpkmanu
qywzphxokseldjritfcyvkmanu
qywzxhxoiseldjrbtfqgvcmanu
qywzphxoisfldjrbpfcgvkmaju
qywuphxgiseldjrbffcgvkmanu
qywzphociseldjrbtfcgvkuanu
qywzphxoiseldvrbtftgckmanu
qywzpbxoisrldjrbtfngvkmanu
qywzphxoiseldjrntfygvdmanu
qywzphxviseldkrbtfcgvkianu
qywpphxgiseldjrbtfctvkmanu
qywzphxoicewdjrbtfcgvsmanu
qywzpcxoiseldjmbtfcgvcmanu
qrwzphxoiseldjrbtfcgjumanu
qywzphxoiselojrbtfcgxkmaau
qywzphxojsbldjrbtfcgykmanu
oywzphxoiseldjrbtfqgvkmvnu
qywfphxpiseldjrbtfckvkmanu
qyyzwhxwiseldjrbtfcgvkmanu
qywzphxgiseldjrbtfchvkmabu
qywzphxfiseldjrbtfcgukoanu
qywzpdxoisyldjrbtfcgvkxanu
dyuzphxoiseldjrbtfcgvkmamu
qywzphxoiseldjrbifcgvkmnnp
qywzpyxoiseldjrbtfcgvklano
dywzphxoiieldjrbtfcgvwmanu
qywzphxoihemdjrbtfcgvdmanu
gywzphxoxseldvrbtfcgvkmanu
qywzqhxoissldjwbtfcgvkmanu
eywzphxoiieldjrbtfcgekmanu
qyizprxciseldjrbtfcgvkmanu
qywjphxoiseldjrbtfcgckmano
qywznhxoiseldjrbrfcgvkmagu
qywzphxoisrldjdbvfcgvkmanu
qyyxphxoiseldjrbtwcgvkmanu
qywzphxoiseldjdbtfcpvkmjnu
qywzvhxqiseldjrbofcgvkmanu
vywzphxoiseldjrbtfcgckwanu
qywzphgbiseldjrbtfcgvkmazu
qcwzphxoiseldjrbqfcgvkmdnu
qywzphxoismldjrbtfcgkkmznu
qywhphxoiseldjrbtccgvkmane
qywzphzoiseldjrbtfcgvqmauu
hywzphxoiseldjrbtfcuvkmanc
qywzphxozsejdgrbtfcgvkmanu
qyszphxoiseldjrntfygvdmanu
qywzphxoisgldjrbtfcgvklaru
qywzhhxoiseldjrbtscgvkmqnu
qywjphxpiqeldjrbtfcgvkmanu
qywzphxoiseldxrptfclvkmanu
qywlphxoisehdjrbtfcgvkmanc
qydzpfxoiseldjrwtfcgvkmanu
qywzphxoiseldjrbtxcgqkfanu
qywophxoiselfjrbtfcgvkmani
qywzyhxoiszldtrbtfcgvkmanu
qywzphxoxseldfrntfcgvkmanu
qywzphloiseldjqbtfcgvkmtnu
qywzpuxoiseldorbtfcgvkeanu
qywzphxoiueldjrwdfcgvkmanu
qgwzphxoiseldjmbtncgvkmanu
qywzphtggseldjrbtfcgvkmanu
qywzphxoisrldjrbtfmgvhmanu
qfszphxoiseldjqbtfcgvkmanu
qywzphxpisjldjrbxfcgvkmanu
qywznhxoisepdjrbtfqgvkmanu
qywzphioiseldjabtfcgxkmanu
qyizphxaiseldjrbtfcgvkmaxu
xywzphxoiqelvjrbtfcgvkmanu
quwzphxoiseldjretfcgvkmaau
bywzphxoiseldjrbtucgckmanu
jywzphxoiseldjrbofcgvkmani
qywzphxoiseltjkbtfcgvkmabu
eywzphxoiselgjrbtfkgvkmanu
qywzphxoisengjrttfcgvkmanu
qywzphzoiseldjrbtfcgvkmknk
quwdphxoiseldjrbtfcxvkmanu
qcwzzhxoiseldjrgtfcgvkmanu
qywgphxdiseldjrbtfcgjkmanu
qywzpdxoivefdjrbtfcgvkmanu
qywzphxoiseldjrbtfdgvjmpnu
qfwzphxoiseydkrbtfcgvkmanu
qywzphxdiqelqjrbtfcgvkmanu
qywzvhxoiseldjrbtfognkmanu
qywzphgoiseldjrbcfcgvtmanu
qywophxoiseldjrbtpcgvkmank
qywzphxoiszldjretfcgvkmabu
qywzphxoiseldjhbtfcgvxmawu
qcgzphxoiselejrbtfcgvkmanu
qywzphxoisepdjrbtfcfvkcanu
qivzphxniseldjrbtfcgvkmanu
qywzhhxoiseldjrftxcgvkmanu
qyazphxciseldjrbtfcgskmanu
qywzphxoisoldgrbtfczvkmanu
qywzmhxoiseldurbwfcgvkmanu
qywzphxoistldjrbwfcgvkranu
qywzphxoistedjrbtfcgokmanu
qywzqhxodsecdjrbtfcgvkmanu
qywzphxtisxldjrbtfcgvkhanu
qywzphxoeseldjrtrfcgvkmanu
qdwzphxoioeldjrbtfigvkmanu
qjwzphxoisbldjrbtfcgvkmanz
qywzphxoiseldbrbtfdgvlmanu
qywzphxoiselddrbhvcgvkmanu
zywzppxoiseldjrdtfcgvkmanu
qywzppxqiselkjrbtfcgvkmanu
qywzphxoihelbjrbtfcgvkmabu
qywzphxoiseldjreyfcgvknanu
qywzphxxrseldjrbtfcgvkmagu
qywhpfxoiseldjrbtfdgvkmanu
qywzphxoisxldjrdtfagvkmanu
oywzphxxiseldjrbtfcgvkmaeu
qywzphxoiselqirbtfvgvkmanu
qywzphxoqseldhrbtfcgvkhanu
qywzphxokseldjrbtfxgvkmaju
qywzphtoiseldurbtfcfvkmanu
qywzphxoiheudjrrtfcgvkmanu
qrwzphxzigeldjrbtfcgvkmanu
qywzphxoiseldorbtfcgvxmvnu
qywzphxoisaldjpbqfcgvkmanu
qywuphxoiselljrbtfchvkmanu
qywzphxoisaldjrbefcgvkmsnu
qywzphxoiteldjrbnfcgvkmanp
qywhphxoiselqjrbtfcgvkmagu
qywzjhxoisejdjrbtfcgvkmanr
qywephaoiseldjrbtfcavkmanu
qywcphxoireldjqbtfcgvkmanu
qywzphxoiseldirbuicgvkmanu
qywzphxoisecvnrbtfcgvkmanu
qcwzphxoiseldjrbtfcgvrmaiu
qywnphxoiseldjrntftgvkmanu
qywzphxhisyldjrbtfcgvkmafu
qyhzphxoiseldjrytfcgvkmanq
vjwzbhxoiseldjrbtfcgvkmanu
qyvzpsxoisuldjrbtfcgvkmanu
qywzphxaiseldcrbefcgvkmanu
qywzphxoiseldjgbtfsgvkfanu
oiwzphxoiseldjrbtfcgvkmcnu
qyezphxoiseldjrbtfcqvkmjnu
tywzphxriseldzrbtfcgvkmanu
yywzphxoiseldjbbtfugvkmanu
qywzpfxviseldjrbttcgvkmanu
qywzphxoiceldcrbtfugvkmanu
qymzphxoiseldjratfcsvkmanu
qywzphxoiselxjrbdfcgvkpanu
qywzphxoiselujrbtfkgvimanu
qywzshyoiseldjrbtfcgpkmanu
qywzphxoiselfjrbtfsgvkmant
qywpphxoiseldjxbtfcyvkmanu
qywzfhxoiselqjrptfcgvkmanu
qewzphxoiseldprbtfcgvkmand
qywfphxoiseldlrbtfcgvkmgnu
qywzphxoiseldjhbtqcovkmanu
fywzphxoiseldlrbtfcgvkjanu
sywzphxoiseldjrbhfccvkmanu
qywzphxoiseldjfbtfcrvkmpnu
sywzphxoisrldjrbtfczvkmanu

145
3/area.py Executable file
View File

@ -0,0 +1,145 @@
#!/usr/bin/env python3
import collections
import numpy
import re
import sys
matrix_size = [1000, 1000]
class Claim:
id = None
# Vector2 ish
origin = None
# Vector2 ish
size = None
matrix = None
def __init__(self, id, origin, size):
self.id = id
self.origin = origin
self.size = size
# Not great to stop the matrix for the entire field in memory,
# but it's small enough in this case.
self.matrix = numpy.zeros(matrix_size, dtype = numpy.bool_)
# Set '1' for all positions of
x_offset = 0
while x_offset < int(size[0]):
y_offset = 0
while y_offset < int(size[1]):
position = (int(origin[0]) + x_offset, int(origin[1]) + y_offset)
#print(position)
self.matrix.itemset(position, True)
y_offset += 1
x_offset += 1
def all_points(self):
d = {}
x = int(self.origin[0])
x_max = x + int(self.size[0])
y_max = int(self.origin[1]) + int(self.size[1])
while x < x_max:
y = int(self.origin[1])
while y < y_max:
d[(x,y)] = 1
y += 1
x += 1
return d
def does_overlap(self, other):
other_start = other.vertices()[0]
other_end = other.vertices()[3]
# If any vertice is in the area defined by the other start and end,
# the claims overlap in some way.
for v in self.vertices():
#print("Checking if {} is inside {}, {}".format(v, other_start, other_end))
if (v[0] >= other_start[0] and v[0] <= other_end[0]) and (v[1] >= other_start[1] and v[1] <= other_end[1]):
return True
return False
def vertices(self):
return [
[self.origin[0], self.origin[1]],
[self.origin[0], self.origin[1] + self.size[1]],
[self.origin[0] + self.size[0], self.origin[1]],
[self.origin[0] + self.size[0], self.origin[1] + self.size[1]]
]
def overlap(self, other):
return numpy.transpose(numpy.nonzero(numpy.logical_and(self.matrix, other.matrix)))
def __str__(self):
return "#{} {},{}: {}x{}".format(self.id, int(self.origin[0]),
int(self.origin[1]), int(self.size[0]),
int(self.size[1]))
def read_input(lines, pattern):
r = []
for l in lines:
m = pattern.match(l)
if m is None:
print('Warning: did not match line "{}"'.format(l))
continue
r.append(
Claim(m.group('id'),
numpy.array([m.group('origin_x'), m.group('origin_y')], dtype = int),
numpy.array([m.group('size_x'), m.group('size_y')], dtype = int)
)
)
return r
if __name__ == '__main__':
p = re.compile('^#(?P<id>\d+) @ (?P<origin_x>\d+),(?P<origin_y>\d+): (?P<size_x>\d+)x(?P<size_y>\d+)')
lines = sys.stdin.readlines()
claims = read_input(lines, p)
print("{} claims".format(len(claims)))
other_claims = claims.copy()
checked = 0
check_count = len(claims) * ((len(claims)-1)/2)
overlapping_points = set()
single_points = set()
counter = collections.Counter()
claimPoints = {}
for claim in claims:
#print(claim.all_points())
p = set(claim.all_points())
claimPoints[claim] = p
counter.update(p)
for k, v in counter.items():
if v >= 2:
overlapping_points.add(k)
else:
single_points.add(k)
print("{} overlapping points, {} single points".format(len(overlapping_points), len(single_points)))
potential_claims = set()
for claim in claims:
pts = claimPoints[claim].intersection(overlapping_points)
print("{}: {} intersecting points".format(claim, len(pts)))
if len(pts) == 0:
potential_claims.add(claim)
for c in potential_claims:
print(c)
#overlaps = {}
#for claim in claims:
# other_claims.remove(claim)
# if claim in overlaps.keys():
# continue
# for other in other_claims:
# if claim in overlaps.keys():
# break
# if other in overlaps.keys():
# continue
# print("Checking {} vs {} ({} current overlaps)".format(claim, other, len(overlaps.keys())))
# if numpy.any(claim.overlap(other)):
# if claim in overlaps:
# overlaps[claim].append(other)
# else:
# overlaps[claim] = [other]
# if other in overlaps:
# overlaps[other].append(claim)
# else:
# overlaps[other] = [claim]
#for claim in claims:
# if claim not in overlaps.keys():
# print(claim)
# #unOverlapped = [x for x in claims and x not in overlaps.keys()]
# #print(unOverlapped)

1335
3/input Normal file

File diff suppressed because it is too large Load Diff

118
4/guard.py Executable file
View File

@ -0,0 +1,118 @@
#!/usr/bin/env python3
import collections
import datetime
import re
import sys
class Event:
def __init__(self, date, event):
m = re.match('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}) (?P<hour>\d{2}):(?P<minute>\d{2})', date)
self.date = datetime.datetime(int(m.group('year')), int(m.group('month')),
int(m.group('day')), int(m.group('hour')),
int(m.group('minute')))
self.event = event
def __str__(self):
return "[{}] {}".format(self.date, self.event)
class Guard:
def __init__(self, id):
self.id = id
self.midnight_sleep = collections.Counter()
self.on_shift = False
self.sleep = False
def start_shift(self, date):
self.on_shift = date
def end_shift(self, date):
self.on_shift = False
if self.sleep:
self.wake_up(date)
def fall_asleep(self, date):
self.sleep = date
def wake_up(self, date):
self.process_sleep(self.sleep, date)
self.sleep = False
def process_sleep(self, start, end):
current_datetime = start
# If the end is outside the start hour, clip it
clip_end = datetime.datetime(start.year, start.month, start.day, start.hour + 1, 0)
clip = False
if clip_end < end:
clip = True
end = clip_end
#print("Processing sleep for guard {} from {} to {} ({})".format(
# self.id, start, end, clip))
#print(self.midnight_sleep)
while current_datetime < end:
if current_datetime.hour != 0:
current_datetime += datetime.timedelta(minutes = 1)
continue
self.midnight_sleep.update({current_datetime.minute: 1})
current_datetime += datetime.timedelta(minutes = 1)
#print(self.midnight_sleep)
def total_sleep(self):
return sum(self.midnight_sleep.values())
def common_minute(self):
r = self.midnight_sleep.most_common(1)
if not r:
r = [(-1, -1)]
return r
def replay(events):
guards = {}
current_guard = None
p = re.compile('Guard #(?P<id>\d+) begins shift')
for e in events:
m = p.match(e.event)
if m:
if current_guard:
current_guard.end_shift(e.date)
# Guard is coming on shift
if m.group('id') in guards.keys():
current_guard = guards[m.group('id')]
else:
current_guard = Guard(m.group('id'))
guards[m.group('id')] = current_guard
current_guard.start_shift(e.date)
continue
if e.event == 'falls asleep':
current_guard.fall_asleep(e.date)
if e.event == 'wakes up':
current_guard.wake_up(e.date)
return guards
if __name__ == '__main__':
lines = sys.stdin.readlines()
p = re.compile('\[(?P<date>\d{4}-\d{2}-\d{2} \d{2}:\d{2})\] (?P<event>.*)')
events = []
for line in lines:
m = p.match(line)
if not m:
print("Warning line '{}' did not match".format(line))
else:
events.append(Event(m.group('date'), m.group('event')))
events.sort(key=lambda e: e.date)
guards = list(replay(events).values())
#print(guards)
guards.sort(key=lambda g: g.total_sleep())
for g in guards:
print("{}: {} minutes asleep, most common minute {}".format(
g.id, g.total_sleep(), g.common_minute()))
print('------------------------------------')
guards.sort(key=lambda g: g.common_minute()[0][1] if g.common_minute() else 0)
for g in guards:
print("{}: {} minutes asleep, most common minute {}".format(
g.id, g.total_sleep(), g.common_minute()))
#print(guards[-1].id * guards[-1].common_minute()[0])
#for e in events:
# print(e)

1129
4/input Normal file

File diff suppressed because it is too large Load Diff

68
5/code.py Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/env python3
import string
import sys
def react(code, offset = 0):
reacted = False
current = None
previous = None
index = 0
for c in code[offset:]:
if index < 1:
index += 1
continue
try:
current = code[index]
except IndexError:
break
previous = code[index-1]
#print("Checking {} & {}".format(previous, current))
if previous.upper() == current.upper():
if previous != current:
#print("{} and {} reacted around index {} ({})".format(previous, current, index, code[index-2:index+2]))
reacted = True
break
index += 1
if reacted:
#start = index - 10
#if start < 0:
start = 0
return (True, start, code[:index-1] + code[index+1:])
else:
return (False, index, code)
if __name__ == '__main__':
lines = sys.stdin.readlines()
# Part 1
#for l in lines:
# reacted = True
# code = l.strip()
# index = 0
# iteration = 0
# while reacted:
# (reacted, index, code) = react(code, index)
# #print("{}, {}, iteration {}".format(reacted, index, iteration))
# iteration += 1
# print("Resulting code: '{}' ({} iterations) has {} units".format(code, iteration, len(code)))
permutations = {}
for l in lines:
for character in string.ascii_lowercase:
reacted = True
code = l.strip()
code = code.replace(character, '')
code = code.replace(character.upper(), '')
index = 0
iteration = 0
while reacted:
(reacted, index, code) = react(code, index)
iteration += 1
permutations[character] = {
'code': code,
'size': len(code),
'iterations': iteration,
}
for character in string.ascii_lowercase:
print("Option {}{} result in code length {} ({} iterations)".format(
character, character.upper(), permutations[character]['size'],
permutations[character]['iterations']))

1
5/input Normal file

File diff suppressed because one or more lines are too long