From 20eff3feba4c6e2b279316cdf66c97575d0b7fd5 Mon Sep 17 00:00:00 2001 From: Mudith Daga Date: Sat, 16 Jul 2022 23:45:56 +0530 Subject: [PATCH] Update 5.py --- 2020/5.py | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/2020/5.py b/2020/5.py index 8842b83..d0832ea 100644 --- a/2020/5.py +++ b/2020/5.py @@ -1,4 +1,7 @@ # If you already have the data as an input use: +from typing import Type + + data = [] # Your Advent Input # However, it is highly recommended to run this: location = "C:\\Users\\dagam\\Downloads\\input.txt" # Path of the input.txt provided from Advent Calendar @@ -13,11 +16,48 @@ del data[-1] def get_row_column(string): - row_pos = string[:8] - print(row_pos) + row_pos, col_pos = string[:7], string[-3:] + row_start, col_start = 0, 0 + row_end, col_end = 127, 7 + row, col = 0, 0 + for i in row_pos: + if i == "F": + row_end -= round((row_end-row_start)/2) + elif i == "B": + row_start += round((row_end-row_start)/2) + if row_pos[-1] == "F": + row = row_start + else: + row = row_end + for i in col_pos: + if i == "L": + col_end -= round((col_end-col_start)/2) + elif i == "R": + col_start += round((col_end-col_start)/2) + if col_pos[-1] == "L": + col = col_start + else: + col = col_end + return row, col def day5(part=1): if part not in [1,2]: part = 1 + row_and_column = [] + for string in data: + row_and_column.append(get_row_column(string)) if part == 1: - for i in data: - get_row_column(i) \ No newline at end of file + for i in row_and_column: + row_and_column[row_and_column.index(i)] = i[0]*8 + i[1] + return max(row_and_column) + elif part == 2: #Part 2 + for i in row_and_column: + try: + row, col = i[0], i[1] + except TypeError: + pass + row_and_column[row_and_column.index(i)] = row*8 + col + 1 + row_and_column.append(row*8 + col - 1) + return max(row_and_column) + +print("Part 1: "+str(day5(part=1))) +print("Part 2: "+str(day5(part=2))) \ No newline at end of file