This commit is contained in:
2021-12-06 07:37:45 -05:00
commit 6bff0ee551
22 changed files with 5034 additions and 0 deletions

38
Day1_1/main.py Normal file
View File

@@ -0,0 +1,38 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=1, year=2021)
test_data = """199
200
208
210
200
207
240
269
260
263"""
lines = input_data.split("\n")
count = 0
down_count = 0
for index, value in enumerate(lines):
# if index != 0:
if int(value) > int(lines[index - 1]):
print(str(index)+ ": " + str(value) + " + "+ str(lines[index - 1]))
# print("1")
count += 1
else:
print(str(index)+ ": " + str(value) + " < "+ str(lines[index - 1]))
# print("0")
down_count += 1
print("Input length:" + str(len(lines)))
print(count)
print(down_count)
#submit(count)

2003
Day1_1/output.txt Normal file

File diff suppressed because it is too large Load Diff

43
Day1_1/problem.txt Normal file
View File

@@ -0,0 +1,43 @@
--- Day 1: Sonar Sweep ---
You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!
Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars.
Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.
For example, suppose you had the following report:
199
200
208
210
200
207
240
269
260
263
This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.
The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.
To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:
199 (N/A - no previous measurement)
200 (increased)
208 (increased)
210 (increased)
200 (decreased)
207 (increased)
240 (increased)
269 (increased)
260 (decreased)
263 (increased)
In this example, there are 7 measurements that are larger than the previous measurement.
How many measurements are larger than the previous measurement?

41
Day1_2/main.py Normal file
View File

@@ -0,0 +1,41 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=1, year=2021)
test_data = """199
200
208
210
200
207
240
269
260
263"""
lines = input_data.split("\n")
# lines = test_data.split("\n")
count = 0
window_list = []
for index, value in enumerate(lines):
if index <= len(lines) - 3:
window = int(lines[index]) + int(lines[index + 1]) + int(lines[index + 2])
window_list.append(window)
for index, value in enumerate(window_list):
if index != 0:
if int(value) > int(window_list[index - 1]):
print(str(index)+ ": " + str(value) + " + "+ str(window_list[index - 1]))
# print("1")
count += 1
print("Input length:" + str(len(lines)))
print(count)
print(window_list)
submit(count)

2003
Day1_2/output.txt Normal file

File diff suppressed because it is too large Load Diff

32
Day1_2/problem.txt Normal file
View File

@@ -0,0 +1,32 @@
--- Part Two ---
Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.
Instead, consider sums of a three-measurement sliding window. Again considering the above example:
199 A
200 A B
208 A B C
210 B C D
200 E C D
207 E F D
240 E F G
269 F G H
260 G H
263 H
Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (199, 200, 208); their sum is 199 + 200 + 208 = 607. The second window is marked B (200, 208, 210); its sum is 618. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased.
Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.
In the above example, the sum of each three-measurement window is as follows:
A: 607 (N/A - no previous sum)
B: 618 (increased)
C: 618 (no change)
D: 617 (decreased)
E: 647 (increased)
F: 716 (increased)
G: 769 (increased)
H: 792 (increased)
In this example, there are 5 sums that are larger than the previous sum.
Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?

39
Day2_1/main.py Normal file
View File

@@ -0,0 +1,39 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=2, year=2021)
test_data = """forward 5
down 5
forward 8
up 3
down 8
forward 2"""
lines = input_data.split("\n")
# lines = test_data.split("\n")
def split_line(line):
return line.split(" ")
# print(split_line(lines[0]))
horizontal = 0
depth = 0
for line in lines:
command = split_line(line)
# print(command)
if command[0] == "up":
depth = depth - int(command[1])
if command[0] == "down":
depth = depth + int(command[1])
if command[0] == "forward":
horizontal = horizontal + int(command[1])
print(horizontal)
print(depth)
print(depth*horizontal)
submit(depth*horizontal)

0
Day2_1/promblem.txt Normal file
View File

41
Day2_2/main.py Normal file
View File

@@ -0,0 +1,41 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=2, year=2021)
test_data = """forward 5
down 5
forward 8
up 3
down 8
forward 2"""
lines = input_data.split("\n")
# lines = test_data.split("\n")
def split_line(line):
return line.split(" ")
# print(split_line(lines[0]))
horizontal = 0
depth = 0
aim = 0
for line in lines:
command = split_line(line)
# print(command)
if command[0] == "up":
aim = aim - int(command[1])
if command[0] == "down":
aim = aim + int(command[1])
if command[0] == "forward":
horizontal = horizontal + int(command[1])
depth = depth + (aim * int(command[1]) )
print(horizontal)
print(depth)
print(depth*horizontal)
submit(depth*horizontal)

23
Day2_2/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

60
Day3_1/main.py Normal file
View File

@@ -0,0 +1,60 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=3, year=2021)
test_data = """00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""
lines = input_data.split("\n")
# lines = test_data.split("\n")
line_len = len(lines[0])
counts_ones = [0] * line_len
counts_zeroes = [0] * line_len
for line in lines:
power = list(line)
power = [int(i) for i in power]
for index, bit in enumerate(power):
if bit == 1:
counts_ones[index] += 1
elif bit == 0:
counts_zeroes[index] += 1
print(counts_ones)
print(counts_zeroes)
#loop through bits and find highest lowest
gamma = [0] * line_len
epsilon = [0] * line_len
for i in range(line_len):
print(i)
if counts_ones[i] > counts_zeroes[i]:
gamma[i] = 1
epsilon[i] = 0
elif counts_ones[i] < counts_zeroes[i]:
gamma[i] = 0
epsilon[i] = 1
# print(gamma)
# print(epsilon)
gamma = ''.join(str(i) for i in gamma)
epsilon = ''.join(str(i) for i in epsilon)
powerconsumption = int(gamma, 2) * int(epsilon, 2)
print(powerconsumption)
# submit(depth*horizontal)

23
Day3_1/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

107
Day3_2/main.py Normal file
View File

@@ -0,0 +1,107 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=3, year=2021)
test_data = """00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""
c02 = input_data.split("\n")
# c02 = test_data.split("\n")
line_len = len(c02[0])
oxygen = c02.copy()
c02 = c02.copy()
for i in range(line_len):
if len(oxygen) == 1:
print("only 1 left")
break
#get count for this column
counts_ones = 0
counts_zeroes = 0
for line in oxygen:
line = list(line)
bit = line[i]
if bit == "1":
counts_ones += 1
elif bit == "0":
counts_zeroes += 1
print("Zeros:" + str(counts_zeroes) + "Ones:" + str(counts_ones))
if counts_ones >= counts_zeroes:
for index, line in reversed(list(enumerate(oxygen))):
# print(line)
if line[i] == "0":
print("drop zeros in col " + str(i)+ ":" + ''.join(str(i) for i in line))
oxygen.remove(line)
elif counts_ones < counts_zeroes:
for index, line in reversed(list(enumerate(oxygen))):
if line[i] == "1":
print("drop ones in col" + str(i)+ ":" + ''.join(str(i) for i in line))
oxygen.remove(line)
print("remaining oxygen")
print(oxygen)
print(oxygen)
###CO2
for i in range(line_len):
if len(c02) == 1:
print("only 1 left")
break
#get count for this column
counts_ones = 0
counts_zeroes = 0
for line in c02:
line = list(line)
bit = line[i]
if bit == "1":
counts_ones += 1
elif bit == "0":
counts_zeroes += 1
print("Zeros:" + str(counts_zeroes) + "Ones:" + str(counts_ones))
if counts_ones < counts_zeroes:
for index, line in reversed(list(enumerate(c02))):
# print(line)
if line[i] == "0":
print("drop zeros in col " + str(i)+ ":" + ''.join(str(i) for i in line))
c02.remove(line)
elif counts_ones >= counts_zeroes:
for index, line in reversed(list(enumerate(c02))):
if line[i] == "1":
print("drop ones in col" + str(i)+ ":" + ''.join(str(i) for i in line))
c02.remove(line)
print("remaining c02")
print(c02)
print(c02)
# print(epsilon)
c02 = ''.join(str(i) for i in c02)
oxygen = ''.join(str(i) for i in oxygen)
print(str(int(c02, 2)))
print(str(int(oxygen, 2)))
powerconsumption = int(c02, 2) * int(oxygen, 2)
print(powerconsumption)
submit(powerconsumption)

23
Day3_2/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

120
Day4_1/main.py Normal file
View File

@@ -0,0 +1,120 @@
from aocd import get_data
from aocd import submit
from pprint import pprint
from itertools import *
input_data = get_data(day=4, year=2021)
test_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7"""
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
lines = input_data.split("\n")
# lines = test_data.split("\n")
blocks_in = input_data.split("\n\n")
number_draw = lines[0].split(",")
number_draw = [int(item) for item in number_draw]
number_draw = grouper(5, number_draw)
print(number_draw)
blocks_in.pop(0)
blocks = []
# print(blocks)
for index, block in enumerate(blocks_in):
print("------------")
print(index)
rows = block.split("\n")
new_block = []
for row in rows:
new_block.append(row.split())
blocks.append(new_block)
print(blocks)
def check_for_winner(blocks):
winner = []
for block in blocks:
if winner == []:
for row in block:
if row == ["X","X","X","X","X"]:
print("ROW WINNER")
# print(block)
winner = block
if winner == []:
for column, num in enumerate(block[0]):
block_column = [ block[0][column], block[1][column],block[2][column],block[3][column],block[4][column] ]
# print(block_column)
if block_column == ["X","X","X","X","X"]:
print("COL WINNER")
winner = block
break
return winner
last_draw = 0
last_drawblock = 0
for draw in number_draw:
print("====================================================================================================")
print("DrawBlock:" + str(draw))
last_drawblock = draw[4]
for number in draw:
winner = check_for_winner(blocks)
if winner != []:
#winner
print("WINNER")
print(winner)
break
else:
print("Draw:" + str(number))
last_draw = number
for b, block in enumerate(blocks):
for r, row in enumerate(block):
for n, num in enumerate(row):
if num == str(number):
blocks[b][r][n] = "X"
# pprint(blocks)
print("---------------------------------")
else:
continue
break
print("FINAL WINNER")
print(winner)
sum = 0
for row in winner:
for number in row:
if number != "X":
sum += int(number)
print(sum)
print(last_draw)
print(sum*last_draw)
submit(sum*last_draw)
# pprint(blocks)

23
Day4_1/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

120
Day4_2/Day4_1/main.py Normal file
View File

@@ -0,0 +1,120 @@
from aocd import get_data
from aocd import submit
from pprint import pprint
from itertools import *
input_data = get_data(day=4, year=2021)
test_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7"""
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
lines = input_data.split("\n")
# lines = test_data.split("\n")
blocks_in = input_data.split("\n\n")
number_draw = lines[0].split(",")
number_draw = [int(item) for item in number_draw]
number_draw = grouper(5, number_draw)
print(number_draw)
blocks_in.pop(0)
blocks = []
# print(blocks)
for index, block in enumerate(blocks_in):
print("------------")
print(index)
rows = block.split("\n")
new_block = []
for row in rows:
new_block.append(row.split())
blocks.append(new_block)
print(blocks)
def check_for_winner(blocks):
winner = []
for block in blocks:
if winner == []:
for row in block:
if row == ["X","X","X","X","X"]:
print("ROW WINNER")
# print(block)
winner = block
if winner == []:
for column, num in enumerate(block[0]):
block_column = [ block[0][column], block[1][column],block[2][column],block[3][column],block[4][column] ]
# print(block_column)
if block_column == ["X","X","X","X","X"]:
print("COL WINNER")
winner = block
break
return winner
last_draw = 0
last_drawblock = 0
for draw in number_draw:
print("====================================================================================================")
print("DrawBlock:" + str(draw))
last_drawblock = draw[4]
for number in draw:
winner = check_for_winner(blocks)
if winner != []:
#winner
print("WINNER")
print(winner)
break
else:
print("Draw:" + str(number))
last_draw = number
for b, block in enumerate(blocks):
for r, row in enumerate(block):
for n, num in enumerate(row):
if num == str(number):
blocks[b][r][n] = "X"
# pprint(blocks)
print("---------------------------------")
else:
continue
break
print("FINAL WINNER")
print(winner)
sum = 0
for row in winner:
for number in row:
if number != "X":
sum += int(number)
print(sum)
print(last_draw)
print(sum*last_draw)
submit(sum*last_draw)
# pprint(blocks)

View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

130
Day4_2/main.py Normal file
View File

@@ -0,0 +1,130 @@
from aocd import get_data
from aocd import submit
from pprint import pprint
from itertools import *
input_data = get_data(day=4, year=2021)
test_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7"""
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
lines = input_data.split("\n")
blocks_in = input_data.split("\n\n")
# lines = test_data.split("\n")
# blocks_in = test_data.split("\n\n")
number_draw = lines[0].split(",")
number_draw = [int(item) for item in number_draw]
number_draw = grouper(5, number_draw)
print(number_draw)
blocks_in.pop(0)
blocks = []
# print(blocks)
for index, block in enumerate(blocks_in):
print("------------")
print(index)
rows = block.split("\n")
new_block = []
for row in rows:
new_block.append(row.split())
blocks.append(new_block)
print(blocks)
def check_for_winner(blocks):
l_winner = []
for index, block in reversed(list(enumerate(blocks))):
winner = "XX"
if winner == "XX":
for row in block:
if row == ["X","X","X","X","X"]:
print("ROW WINNER")
# print(block)
winner = "YY"
if winner == "XX":
for column, num in enumerate(block[0]):
block_column = [ block[0][column], block[1][column],block[2][column],block[3][column],block[4][column] ]
# print(block_column)
if block_column == ["X","X","X","X","X"]:
print("COL WINNER")
winner = "YY"
if winner == "YY":
l_winner = block.copy()
print("Del")
print(l_winner)
blocks.remove(block)
return l_winner
last_draw = 0
last_drawblock = 0
last_winner = []
for draw in number_draw:
print("====================================================================================================")
print("DrawBlock:" + str(draw))
last_drawblock = draw[4]
for number in draw:
last_winner = check_for_winner(blocks)
print("blocks len:" + str(len(blocks)))
if len(blocks) == 0:
print("WINNER")
print(last_winner)
winner = last_winner
break
else:
print("Draw:" + str(number))
last_draw = number
for b, block in enumerate(blocks):
for r, row in enumerate(block):
for n, num in enumerate(row):
if num == str(number):
blocks[b][r][n] = "X"
# pprint(blocks)
print("---------------------------------")
else:
continue
break
print("FINAL WINNER")
print(blocks)
print(winner)
sum = 0
for row in winner:
for number in row:
if number != "X":
sum += int(number)
print(sum)
print(last_draw)
print(sum*last_draw)
# print(blocks)
submit(sum*last_draw)
# pprint(blocks)

23
Day4_2/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

96
Day5/main.py Normal file
View File

@@ -0,0 +1,96 @@
from aocd import get_data
from aocd import submit
input_data = get_data(day=5, year=2021)
test_data = """0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2"""
lines = input_data.split("\n")
# lines = test_data.split("\n")
import re
import numpy as np
lineSegments = []
def readDataLine(line):
m = re.search('([0-9]*),([0-9]*)\s->\s([0-9]*),([0-9]*)', line)
if m:
segment = [int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))]
return segment
else:
return []
for line in lines:
lineSegment = readDataLine(line)
if lineSegment != []:
lineSegments.append(lineSegment)
print(lineSegments)
largestVal = np.amax(lineSegments,axis=0)
print(largestVal)
### create 2d array
MainArray = np.zeros((largestVal[0] + 10,largestVal[3] + 10 ), dtype = int)
print(MainArray)
for line in lineSegments:
print("LINE---------")
print(line)
X_start = line[0]
Y_start = line[1]
X_end = line[2]
Y_end = line[3]
Y_max = max(Y_start, Y_end)
Y_min = min(Y_start, Y_end)
X_max = max(X_start, X_end)
X_min = min(X_start, X_end)
###ROWS
if X_start == X_end:
for j in range(Y_min,Y_max + 1):
MainArray[j,X_start] += 1
###COLUMNS
elif Y_start == Y_end:
for i in range(X_min,X_max + 1):
# print("X:" + str(i))
MainArray[Y_start,i] += 1
###DIAGS
else:
if Y_start < Y_end:
j_array = list(range(Y_start,Y_end + 1,1))
else:
j_array = list(range(Y_start,Y_end - 1, -1))
if X_start < X_end:
print("B")
i_array = list(range(X_start,X_end + 1,1))
else:
print(X_end, X_start)
print("C")
i_array = list(range(X_start,X_end - 1, -1))
print("XXXXXXXXXXXXXXXXX===================================")
print(j_array)
print(i_array)
for index, j in enumerate(j_array):
# print(j,i_array[index])
MainArray[j,i_array[index]] += 1
print(MainArray)
print( (MainArray>1).sum() )
submit( (MainArray>1).sum() )

23
Day5/promblem.txt Normal file
View File

@@ -0,0 +1,23 @@
--- Part Two ---
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:
down X increases your aim by X units.
up X decreases your aim by X units.
forward X does two things:
It increases your horizontal position by X units.
It increases your depth by your aim multiplied by X.
Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.
Now, the above example does something different:
forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
down 5 adds 5 to your aim, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
up 3 decreases your aim by 3, resulting in a value of 2.
down 8 adds 8 to your aim, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.
After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?