mirror of
https://github.com/sstent/Advent2021.git
synced 2026-01-27 09:32:03 +00:00
first
This commit is contained in:
120
Day4_1/main.py
Normal file
120
Day4_1/main.py
Normal 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
23
Day4_1/promblem.txt
Normal 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?
|
||||
Reference in New Issue
Block a user