Advent of Code 2022¶
Day 1 through Day 6¶
AOC Day 1 Task 1¶
In [1]:
fin = open ('AOC1_input.txt', 'r')
elf_totals = []
current_elf = 0
for line in fin:
if (line!='\n'):
current_elf += int(line.strip())
else:
elf_totals.append(current_elf)
current_elf = 0
elf_totals.sort()
answer = elf_totals[-1]
print(answer)
68775
AOC Day 1 Task 2¶
In [2]:
fin = open ('AOC1_input.txt', 'r')
elf_totals = []
current_elf = 0
for line in fin:
if (line!='\n'):
current_elf += int(line.strip())
else:
elf_totals.append(current_elf)
current_elf = 0
elf_totals.sort()
answer = elf_totals[-3:]
print(answer)
print(sum(answer))
[66765, 67045, 68775] 202585
AOC Day 2 Task 1¶
In [3]:
fin = open ('AOC2_input.txt', 'r')
score_dict = {"A X":4,"A Y":8,"A Z":3,"B X":1,"B Y":5,"B Z":9,"C X":7,"C Y":2,"C Z":6}
print(score_dict)
all_scores_list = []
for line in fin:
#print(line.strip())
#print(score_dict[line.strip()])
all_scores_list.append(score_dict[line.strip()])
print(sum(all_scores_list))
{'A X': 4, 'A Y': 8, 'A Z': 3, 'B X': 1, 'B Y': 5, 'B Z': 9, 'C X': 7, 'C Y': 2, 'C Z': 6}
9651
AOC Day 2 Task 2¶
In [4]:
fin = open ('AOC2_input.txt', 'r')
score_dict = {"A X":3,"A Y":4,"A Z":8,"B X":1,"B Y":5,"B Z":9,"C X":2,"C Y":6,"C Z":7}
print(score_dict)
all_scores_list = []
for line in fin:
#print(line.strip())
#print(score_dict[line.strip()])
all_scores_list.append(score_dict[line.strip()])
print(sum(all_scores_list))
{'A X': 3, 'A Y': 4, 'A Z': 8, 'B X': 1, 'B Y': 5, 'B Z': 9, 'C X': 2, 'C Y': 6, 'C Z': 7}
10560
AOC Day 3 Task 1¶
In [5]:
fin = open ('AOC3_input.txt', 'r')
all_scores_list = []
for line in fin:
line0 = line.strip()
halflen = int(len(line0)/2)
set1 = set(line0[:halflen])
set2 = set(line0[halflen:])
item = list(set1.intersection(set2))[0]
score0=ord(item)
if score0-96 > 0:
score = score0-96
else:
score=score0-38
all_scores_list.append(score)
#print(line0, line0[:halflen], line0[halflen:],item,score)
#print(all_scores_list)
print(sum(all_scores_list))
8053
AOC Day 3 Task 2¶
In [6]:
fin = open ('AOC3_input.txt', 'r')
list_of_sets = []
all_scores_list = []
for line in fin:
line0 = line.strip()
list_of_sets.append(set(line0))
for i in range(0,len(list_of_sets),3):
set1 = set(list_of_sets[i])
set2 = set(list_of_sets[i+1])
set3 = set(list_of_sets[i+2])
item = list(set1.intersection(set2.intersection(set3)))[0]
score0=ord(item)
if score0-96 > 0:
score = score0-96
else:
score=score0-38
all_scores_list.append(score)
print(sum(all_scores_list))
2425
AOC Day 4 Task 1¶
In [7]:
fin = open ('AOC4_input.txt', 'r')
overlaps = 0
for line in fin:
line0 = line.strip()
A,B = line0.split(',')
A1,A2 = A.split('-')
B1,B2 = B.split('-')
#print(line0,A,A1,A2, B, B1, B2)
ArangeSET = set(x for x in range(int(A1),int(A2)+1))
BrangeSET = set(x for x in range(int(B1),int(B2)+1))
unionSET = ArangeSET.union(BrangeSET)
if len(unionSET)==len(ArangeSET) or len(unionSET)==len(BrangeSET):
# one list was fully contained in the other
overlaps +=1
print(overlaps)
644
AOC Day 4 Task 2¶
In [8]:
fin = open ('AOC4_input.txt', 'r')
overlaps = 0
for line in fin:
line0 = line.strip()
A,B = line0.split(',')
A1,A2 = A.split('-')
B1,B2 = B.split('-')
#print(line0,A,A1,A2, B, B1, B2)
ArangeSET = set(x for x in range(int(A1),int(A2)+1))
BrangeSET = set(x for x in range(int(B1),int(B2)+1))
intersectSET = ArangeSET.intersection(BrangeSET)
if len(intersectSET) > 0:
overlaps +=1
print(overlaps)
926
AOC Day 5 Task 1¶
In [9]:
fin = open ('AOC5_input.txt', 'r')
template9 = "[0] [0] [0] [0] [0] [0] [0] [0] [0]"
rev1=[]
rev2=[]
rev3=[]
rev4=[]
rev5=[]
rev6=[]
rev7=[]
rev8=[]
rev9=[]
instructionList = []
for line in fin:
if line!='\n':
if line[1]=='o':
#print("INSTRUCTION")
insline = line.strip().split(' ')
instriple = (int(insline[1]),int(insline[3]),int(insline[5]))
instructionList.append(instriple)
elif line[1]=='1':
#print("INDEX")
continue
else:
#print("DATA")
newline = ""
for i in range(len(template9)):
if line[i]==' ':
newline += template9[i]
else:
newline += line[i]
rev1.append(newline[1])
rev2.append(newline[5])
rev3.append(newline[9])
rev4.append(newline[13])
rev5.append(newline[17])
rev6.append(newline[21])
rev7.append(newline[25])
rev8.append(newline[29])
rev9.append(newline[33])
#print(line)
#print(line)
#print(newline)
stack1 = rev1[::-1]
while stack1[len(stack1)-1]=='0':
stack1.pop()
stack2 = rev2[::-1]
while stack2[len(stack2)-1]=='0':
stack2.pop()
stack3 = rev3[::-1]
while stack3[len(stack3)-1]=='0':
stack3.pop()
stack4 = rev4[::-1]
while stack4[len(stack4)-1]=='0':
stack4.pop()
stack5 = rev5[::-1]
while stack5[len(stack5)-1]=='0':
stack5.pop()
stack6 = rev6[::-1]
while stack6[len(stack6)-1]=='0':
stack6.pop()
stack7 = rev7[::-1]
while stack7[len(stack7)-1]=='0':
stack7.pop()
stack8 = rev8[::-1]
while stack8[len(stack8)-1]=='0':
stack8.pop()
stack9 = rev9[::-1]
while stack9[len(stack9)-1]=='0':
stack9.pop()
stackList = [stack1,stack2,stack3,stack4,stack5,stack6,stack7,stack8,stack9]
#for stack in stackList:
# print(stack)
for count,F,T in instructionList:
#print(count,F,T)
for i in range(count):
item = stackList[F-1].pop()
stackList[T-1].append(item)
answer = ""
for stack in stackList:
answer+=stack[-1]
print(stack)
print(answer)
['F', 'W', 'H', 'G', 'Z', 'H', 'V', 'C', 'S', 'Z', 'N', 'S', 'Q'] ['D', 'W', 'P'] ['N', 'M', 'F', 'J'] ['P'] ['M', 'M', 'V', 'W', 'N', 'T', 'W', 'L'] ['N', 'M'] ['F', 'L', 'W', 'J', 'L', 'T', 'F', 'R', 'N'] ['G', 'P', 'N'] ['B', 'R', 'C', 'Z', 'F', 'B', 'D', 'B', 'S', 'N', 'R', 'H', 'R'] QPJPLMNNR
AOC Day 5 Task 2¶
In [10]:
fin = open ('AOC5_input.txt', 'r')
template9 = "[0] [0] [0] [0] [0] [0] [0] [0] [0]"
rev1=[]
rev2=[]
rev3=[]
rev4=[]
rev5=[]
rev6=[]
rev7=[]
rev8=[]
rev9=[]
instructionList = []
for line in fin:
if line!='\n':
if line[1]=='o':
#print("INSTRUCTION")
insline = line.strip().split(' ')
instriple = (int(insline[1]),int(insline[3]),int(insline[5]))
instructionList.append(instriple)
elif line[1]=='1':
#print("INDEX")
continue
else:
#print("DATA")
newline = ""
for i in range(len(template9)):
if line[i]==' ':
newline += template9[i]
else:
newline += line[i]
rev1.append(newline[1])
rev2.append(newline[5])
rev3.append(newline[9])
rev4.append(newline[13])
rev5.append(newline[17])
rev6.append(newline[21])
rev7.append(newline[25])
rev8.append(newline[29])
rev9.append(newline[33])
#print(line)
#print(line)
#print(newline)
stack1 = rev1[::-1]
while stack1[len(stack1)-1]=='0':
stack1.pop()
stack2 = rev2[::-1]
while stack2[len(stack2)-1]=='0':
stack2.pop()
stack3 = rev3[::-1]
while stack3[len(stack3)-1]=='0':
stack3.pop()
stack4 = rev4[::-1]
while stack4[len(stack4)-1]=='0':
stack4.pop()
stack5 = rev5[::-1]
while stack5[len(stack5)-1]=='0':
stack5.pop()
stack6 = rev6[::-1]
while stack6[len(stack6)-1]=='0':
stack6.pop()
stack7 = rev7[::-1]
while stack7[len(stack7)-1]=='0':
stack7.pop()
stack8 = rev8[::-1]
while stack8[len(stack8)-1]=='0':
stack8.pop()
stack9 = rev9[::-1]
while stack9[len(stack9)-1]=='0':
stack9.pop()
stackList = [stack1,stack2,stack3,stack4,stack5,stack6,stack7,stack8,stack9]
#for stack in stackList:
# print(stack)
for count,F,T in instructionList:
#print(count,F,T)
tempStack=[]
for i in range(count):
item = stackList[F-1].pop()
tempStack.append(item)
for i in range(count):
item = tempStack.pop()
stackList[T-1].append(item)
answer = ""
for stack in stackList:
answer+=stack[-1]
print(stack)
print(answer)
['L', 'R', 'W', 'N', 'W', 'S', 'G', 'S', 'V', 'M', 'F', 'H', 'B'] ['N', 'N', 'Q'] ['C', 'C', 'T', 'D'] ['N'] ['Z', 'H', 'P', 'N', 'L', 'B', 'W', 'W'] ['S', 'J'] ['T', 'R', 'F', 'F', 'P', 'H', 'M', 'R', 'P'] ['L', 'N', 'V'] ['W', 'D', 'Z', 'M', 'M', 'N', 'G', 'R', 'F', 'F', 'B', 'Z', 'J'] BQDNWJPVJ
AOC Day 6 Task 1¶
In [11]:
fin = open ('AOC6_input.txt', 'r')
for line in fin:
#print(line)
last = len(line.strip())-4
for i in range(last):
test = set([line[i],line[i+1],line[i+2],line[i+3]])
#print(test,len(test))
if len(test)==4:
print(i+4)
break
1109
AOC Day 6 Task 2¶
In [12]:
fin = open ('AOC6_input.txt', 'r')
for line in fin:
#print(line)
last = len(line.strip())-14
for i in range(last):
test = set([line[i],line[i+1],line[i+2],line[i+3],line[i+4],line[i+5],line[i+6],line[i+7],
line[i+8],line[i+9],line[i+10],line[i+11],line[i+12],line[i+13]])
#print(test,len(test))
if len(test)==14:
print(i+14)
break
3965