From 54610bdd238b9289b7c130e7c40e33b2b48de7c1 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Wed, 7 Dec 2022 22:16:38 -0800 Subject: [PATCH 1/5] day8 part1 --- tests/y_2022/test_2022_day8.py | 22 +++++++++ y_2022/day8.py | 86 ++++++++++++++++++++++++++++++++++ y_2022/input_day8_test.txt | 5 ++ 3 files changed, 113 insertions(+) create mode 100644 tests/y_2022/test_2022_day8.py create mode 100644 y_2022/day8.py create mode 100644 y_2022/input_day8_test.txt diff --git a/tests/y_2022/test_2022_day8.py b/tests/y_2022/test_2022_day8.py new file mode 100644 index 0000000..4262718 --- /dev/null +++ b/tests/y_2022/test_2022_day8.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2022.common import load_input +from y_2022.day8 import Day + +with patch("builtins.open", mock_open(read_data="0")): + day = Day() + + +def test__preprocess_input(): + day._input_data = [[]] + day._preprocess_input() + assert day._Day__input_data == [] + + +def test_complete(): + day._input_data = load_input(8, 1) + day._preprocess_input() + assert day._calculate_1() == 21 + assert day._calculate_2() == 8 diff --git a/y_2022/day8.py b/y_2022/day8.py new file mode 100644 index 0000000..9da5532 --- /dev/null +++ b/y_2022/day8.py @@ -0,0 +1,86 @@ +from .common import AoCDay + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__.split(".")[1].replace("day", ""), test) + + def _preprocess_input(self): + # self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data] + # print(f"{self._input_data=}") + self.__input_data = self._input_data[0] + self.grid = {} + # print(f"{x=}") + x=self.__input_data + for c, i in enumerate(x): + # print(i) + for d, j in enumerate(i): + self.grid[(c,d)]=j + self.max_w=len(x) + self.max_h=len(x[0]) + # print(f"{self.max_w=}") + # print(f"{self.max_h=}") + # print(f"{self.grid=}") + + def is_visible(self, i): + # return True + if i[0] in [0, self.max_w-1]: + return True + if i[1] in [0, self.max_h-1]: + return True + #visible from Top + # print(f"{i},{self.grid[(i)]}") + visible_Top = True + for j in range(0, i[0]): + # print(f"vado in range {(0, i[0])}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}") + if self.grid[(j, i[1])] >= self.grid[(i)]: + visible_Top = False + + #visible from Left + visible_Left = True + for j in range(0, i[1]): + # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + if self.grid[(i[0], j)] >= self.grid[(i)]: + visible_Left = False + + #visible from Bottom + visible_Bottom = True + for j in range(i[0]+1, self.max_h): + # print(f"vado in range {(i[0]+1, self.max_h)}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}") + if self.grid[(j, i[1])] >= self.grid[(i)]: + visible_Bottom = False + + #visible from Right + visible_Right = True + for j in range(i[1]+1, self.max_w): + # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + if self.grid[(i[0], j)] >= self.grid[(i)]: + visible_Right = False + # for j in [] + # return visible_Bottom + is_visible = visible_Top or visible_Left or visible_Bottom or visible_Right + # print(is_visible) + return is_visible + # return False + + def viewing_score(self, i): + return 0 + def _calculate_1(self): + x = self.__input_data + + t = 0 + for i in self.grid: + # print(f"{i}, {self.grid[(i)]}, {self.is_visible(i)=}\n") + if self.is_visible(i): + t+=1 + return t + + def _calculate_2(self): + x = self.__input_data + # print(f"{x=}") + t = 0 + for i in self.grid: + # print(f"{i}, {self.grid[(i)]}, {self.is_visible(i)=}\n") + if self.viewing_score(i) > t: + t=self.viewing_score(i) + return t diff --git a/y_2022/input_day8_test.txt b/y_2022/input_day8_test.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/y_2022/input_day8_test.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 From 09c96c93a385febf5a251887f4725ae8aac189f7 Mon Sep 17 00:00:00 2001 From: Stefano Gallotti Date: Thu, 8 Dec 2022 08:00:23 -0800 Subject: [PATCH 2/5] partial2 --- tests/y_2022/test_2022_day8.py | 4 +- y_2022/day8.py | 112 +++++++++++++++++++++++++++------ 2 files changed, 96 insertions(+), 20 deletions(-) diff --git a/tests/y_2022/test_2022_day8.py b/tests/y_2022/test_2022_day8.py index 4262718..e696e46 100644 --- a/tests/y_2022/test_2022_day8.py +++ b/tests/y_2022/test_2022_day8.py @@ -11,8 +11,8 @@ def test__preprocess_input(): day._input_data = [[]] - day._preprocess_input() - assert day._Day__input_data == [] + # day._preprocess_input() + # assert day._Day__input_data == [] def test_complete(): diff --git a/y_2022/day8.py b/y_2022/day8.py index 9da5532..9fd1389 100644 --- a/y_2022/day8.py +++ b/y_2022/day8.py @@ -11,24 +11,24 @@ def _preprocess_input(self): self.__input_data = self._input_data[0] self.grid = {} # print(f"{x=}") - x=self.__input_data + x = self.__input_data for c, i in enumerate(x): # print(i) for d, j in enumerate(i): - self.grid[(c,d)]=j - self.max_w=len(x) - self.max_h=len(x[0]) + self.grid[(c, d)] = j + self.max_w = len(x) + self.max_h = len(x[0]) # print(f"{self.max_w=}") # print(f"{self.max_h=}") # print(f"{self.grid=}") def is_visible(self, i): # return True - if i[0] in [0, self.max_w-1]: + if i[0] in [0, self.max_w - 1]: return True - if i[1] in [0, self.max_h-1]: + if i[1] in [0, self.max_h - 1]: return True - #visible from Top + # visible from Top # print(f"{i},{self.grid[(i)]}") visible_Top = True for j in range(0, i[0]): @@ -36,23 +36,23 @@ def is_visible(self, i): if self.grid[(j, i[1])] >= self.grid[(i)]: visible_Top = False - #visible from Left + # visible from Left visible_Left = True for j in range(0, i[1]): - # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") if self.grid[(i[0], j)] >= self.grid[(i)]: visible_Left = False - #visible from Bottom + # visible from Bottom visible_Bottom = True - for j in range(i[0]+1, self.max_h): + for j in range(i[0] + 1, self.max_h): # print(f"vado in range {(i[0]+1, self.max_h)}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}") if self.grid[(j, i[1])] >= self.grid[(i)]: visible_Bottom = False - #visible from Right + # visible from Right visible_Right = True - for j in range(i[1]+1, self.max_w): + for j in range(i[1] + 1, self.max_w): # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") if self.grid[(i[0], j)] >= self.grid[(i)]: visible_Right = False @@ -64,7 +64,82 @@ def is_visible(self, i): # return False def viewing_score(self, i): - return 0 + if i[0] in [0, self.max_w - 1]: + return 0 + if i[1] in [0, self.max_h - 1]: + return 0 + print(f"{i=}, {self.grid[(i)]}") + + score_Top = 0 + for j in range(0, i[0]): + print(f"{j=}") + if i in [(1, 2), (3, 2)]: + # print( + # f"{i}vado in range {(0, i[0])}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}" + # ) + ... + # breakpoint() + if self.grid[(j, i[1])] >= self.grid[(i)]: + score_Top += 1 + break + else: + score_Top += 1 + # break + # if score_Top > 1: + # score_Top -= 1 + print(f"{score_Top=}") + + score_Left = 0 + for j in range(0, i[1]): + print(f"{j=}") + # # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + if self.grid[(i[0], i[1] - 1 - j)] >= self.grid[(i)]: + score_Left += 1 + break + else: + score_Left += 1 + # break + # if score_Left > 1: + # score_Left -= 1 + print(f"{score_Left=}") + + # visible from Bottom + score_Bottom = 1 + for j in range(i[0] + 1, self.max_h): + print(f"{j=}") + # # print( + # # f"vado in range {(i[0]+1, self.max_h)}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}" + # # ) + if self.grid[(j, i[1])] >= self.grid[(i)]: + score_Bottom += 1 + break + else: + score_Bottom += 1 + + # break + # if score_Bottom > 1: + # score_Bottom -= 1 + print(f"{score_Bottom=}") + + # visible from Right + score_Right = 1 + for j in range(i[1] + 1, self.max_w): + print(f"{j=}") + # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + if self.grid[(i[0], j)] >= self.grid[(i)]: + score_Right += 1 + break + else: + score_Right += 1 + # else: + # break + # if score_Right > 1: + # score_Right -= 1 + print(f"{score_Right=}") + + print(score_Top, score_Left, score_Bottom, score_Right) + return score_Top * score_Left * score_Bottom * score_Right + def _calculate_1(self): x = self.__input_data @@ -72,7 +147,7 @@ def _calculate_1(self): for i in self.grid: # print(f"{i}, {self.grid[(i)]}, {self.is_visible(i)=}\n") if self.is_visible(i): - t+=1 + t += 1 return t def _calculate_2(self): @@ -80,7 +155,8 @@ def _calculate_2(self): # print(f"{x=}") t = 0 for i in self.grid: - # print(f"{i}, {self.grid[(i)]}, {self.is_visible(i)=}\n") - if self.viewing_score(i) > t: - t=self.viewing_score(i) + x = self.viewing_score(i) + # print(f"{i}, {self.grid[(i)]}, {x=}\n") + if x > t: + t = x return t From caa128cb8e29ebe082621d8634bf073c93cbe20c Mon Sep 17 00:00:00 2001 From: Stefano Gallotti Date: Thu, 8 Dec 2022 19:43:54 -0800 Subject: [PATCH 3/5] day8 part2 --- y_2022/day8.py | 91 +++++++++----------------------------------------- 1 file changed, 16 insertions(+), 75 deletions(-) diff --git a/y_2022/day8.py b/y_2022/day8.py index 9fd1389..238f983 100644 --- a/y_2022/day8.py +++ b/y_2022/day8.py @@ -6,138 +6,82 @@ def __init__(self, test=0): super().__init__(__name__.split(".")[1].replace("day", ""), test) def _preprocess_input(self): - # self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data] - # print(f"{self._input_data=}") self.__input_data = self._input_data[0] self.grid = {} - # print(f"{x=}") x = self.__input_data for c, i in enumerate(x): - # print(i) for d, j in enumerate(i): self.grid[(c, d)] = j self.max_w = len(x) self.max_h = len(x[0]) - # print(f"{self.max_w=}") - # print(f"{self.max_h=}") - # print(f"{self.grid=}") def is_visible(self, i): - # return True if i[0] in [0, self.max_w - 1]: return True if i[1] in [0, self.max_h - 1]: return True + # visible from Top - # print(f"{i},{self.grid[(i)]}") visible_Top = True for j in range(0, i[0]): - # print(f"vado in range {(0, i[0])}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}") if self.grid[(j, i[1])] >= self.grid[(i)]: visible_Top = False # visible from Left visible_Left = True for j in range(0, i[1]): - # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") if self.grid[(i[0], j)] >= self.grid[(i)]: visible_Left = False # visible from Bottom visible_Bottom = True for j in range(i[0] + 1, self.max_h): - # print(f"vado in range {(i[0]+1, self.max_h)}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}") if self.grid[(j, i[1])] >= self.grid[(i)]: visible_Bottom = False # visible from Right visible_Right = True for j in range(i[1] + 1, self.max_w): - # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") if self.grid[(i[0], j)] >= self.grid[(i)]: visible_Right = False - # for j in [] - # return visible_Bottom + is_visible = visible_Top or visible_Left or visible_Bottom or visible_Right - # print(is_visible) return is_visible - # return False def viewing_score(self, i): if i[0] in [0, self.max_w - 1]: return 0 if i[1] in [0, self.max_h - 1]: return 0 - print(f"{i=}, {self.grid[(i)]}") + # visible top score_Top = 0 - for j in range(0, i[0]): - print(f"{j=}") - if i in [(1, 2), (3, 2)]: - # print( - # f"{i}vado in range {(0, i[0])}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}" - # ) - ... - # breakpoint() - if self.grid[(j, i[1])] >= self.grid[(i)]: - score_Top += 1 + for j in range(i[0] - 1, -1, -1): + score_Top += 1 + if self.grid[(j, i[1])] >= self.grid[i]: break - else: - score_Top += 1 - # break - # if score_Top > 1: - # score_Top -= 1 - print(f"{score_Top=}") + # visible left score_Left = 0 - for j in range(0, i[1]): - print(f"{j=}") - # # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") - if self.grid[(i[0], i[1] - 1 - j)] >= self.grid[(i)]: - score_Left += 1 + for j in range(i[1] - 1, -1, -1): + score_Left += 1 + if self.grid[(i[0], j)] >= self.grid[(i)]: break - else: - score_Left += 1 - # break - # if score_Left > 1: - # score_Left -= 1 - print(f"{score_Left=}") # visible from Bottom - score_Bottom = 1 + score_Bottom = 0 for j in range(i[0] + 1, self.max_h): - print(f"{j=}") - # # print( - # # f"vado in range {(i[0]+1, self.max_h)}; {i=}, {self.grid[(j, i[1])]=}, {self.grid[(i)]}" - # # ) - if self.grid[(j, i[1])] >= self.grid[(i)]: - score_Bottom += 1 + score_Bottom += 1 + if self.grid[(j, i[1])] >= self.grid[i]: break - else: - score_Bottom += 1 - - # break - # if score_Bottom > 1: - # score_Bottom -= 1 - print(f"{score_Bottom=}") # visible from Right - score_Right = 1 + score_Right = 0 for j in range(i[1] + 1, self.max_w): - print(f"{j=}") - # # print(f"{i}, {(i[1], j)=},{self.grid[(i[1], j)]=}, {self.grid[(i)]}") + score_Right += 1 if self.grid[(i[0], j)] >= self.grid[(i)]: - score_Right += 1 break - else: - score_Right += 1 - # else: - # break - # if score_Right > 1: - # score_Right -= 1 - print(f"{score_Right=}") - - print(score_Top, score_Left, score_Bottom, score_Right) + return score_Top * score_Left * score_Bottom * score_Right def _calculate_1(self): @@ -145,18 +89,15 @@ def _calculate_1(self): t = 0 for i in self.grid: - # print(f"{i}, {self.grid[(i)]}, {self.is_visible(i)=}\n") if self.is_visible(i): t += 1 return t def _calculate_2(self): x = self.__input_data - # print(f"{x=}") t = 0 for i in self.grid: x = self.viewing_score(i) - # print(f"{i}, {self.grid[(i)]}, {x=}\n") if x > t: t = x return t From 12016e66faec37829684227f1de23a58f86e7a01 Mon Sep 17 00:00:00 2001 From: Stefano Gallotti Date: Thu, 8 Dec 2022 19:50:23 -0800 Subject: [PATCH 4/5] test cases --- tests/y_2022/test_2022_day8.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/y_2022/test_2022_day8.py b/tests/y_2022/test_2022_day8.py index e696e46..988ac12 100644 --- a/tests/y_2022/test_2022_day8.py +++ b/tests/y_2022/test_2022_day8.py @@ -5,14 +5,14 @@ from y_2022.common import load_input from y_2022.day8 import Day -with patch("builtins.open", mock_open(read_data="0")): +with patch("builtins.open", mock_open(read_data="01\n23")): day = Day() def test__preprocess_input(): - day._input_data = [[]] - # day._preprocess_input() - # assert day._Day__input_data == [] + day._input_data = [["01", "23"]] + day._preprocess_input() + assert day._Day__grid == {(0, 0): "0", (0, 1): "1", (1, 0): "2", (1, 1): "3"} def test_complete(): From be793c1c6f10b61d347323658354800d4dbda92e Mon Sep 17 00:00:00 2001 From: Stefano Gallotti Date: Fri, 9 Dec 2022 13:28:39 +0100 Subject: [PATCH 5/5] test cases --- y_2022/day8.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/y_2022/day8.py b/y_2022/day8.py index 238f983..e649b08 100644 --- a/y_2022/day8.py +++ b/y_2022/day8.py @@ -7,11 +7,11 @@ def __init__(self, test=0): def _preprocess_input(self): self.__input_data = self._input_data[0] - self.grid = {} + self.__grid = {} x = self.__input_data for c, i in enumerate(x): for d, j in enumerate(i): - self.grid[(c, d)] = j + self.__grid[(c, d)] = j self.max_w = len(x) self.max_h = len(x[0]) @@ -24,25 +24,25 @@ def is_visible(self, i): # visible from Top visible_Top = True for j in range(0, i[0]): - if self.grid[(j, i[1])] >= self.grid[(i)]: + if self.__grid[(j, i[1])] >= self.__grid[(i)]: visible_Top = False # visible from Left visible_Left = True for j in range(0, i[1]): - if self.grid[(i[0], j)] >= self.grid[(i)]: + if self.__grid[(i[0], j)] >= self.__grid[(i)]: visible_Left = False # visible from Bottom visible_Bottom = True for j in range(i[0] + 1, self.max_h): - if self.grid[(j, i[1])] >= self.grid[(i)]: + if self.__grid[(j, i[1])] >= self.__grid[(i)]: visible_Bottom = False # visible from Right visible_Right = True for j in range(i[1] + 1, self.max_w): - if self.grid[(i[0], j)] >= self.grid[(i)]: + if self.__grid[(i[0], j)] >= self.__grid[(i)]: visible_Right = False is_visible = visible_Top or visible_Left or visible_Bottom or visible_Right @@ -58,45 +58,42 @@ def viewing_score(self, i): score_Top = 0 for j in range(i[0] - 1, -1, -1): score_Top += 1 - if self.grid[(j, i[1])] >= self.grid[i]: + if self.__grid[(j, i[1])] >= self.__grid[i]: break # visible left score_Left = 0 for j in range(i[1] - 1, -1, -1): score_Left += 1 - if self.grid[(i[0], j)] >= self.grid[(i)]: + if self.__grid[(i[0], j)] >= self.__grid[(i)]: break # visible from Bottom score_Bottom = 0 for j in range(i[0] + 1, self.max_h): score_Bottom += 1 - if self.grid[(j, i[1])] >= self.grid[i]: + if self.__grid[(j, i[1])] >= self.__grid[i]: break # visible from Right score_Right = 0 for j in range(i[1] + 1, self.max_w): score_Right += 1 - if self.grid[(i[0], j)] >= self.grid[(i)]: + if self.__grid[(i[0], j)] >= self.__grid[(i)]: break return score_Top * score_Left * score_Bottom * score_Right def _calculate_1(self): - x = self.__input_data - t = 0 - for i in self.grid: + for i in self.__grid: if self.is_visible(i): t += 1 return t def _calculate_2(self): - x = self.__input_data t = 0 - for i in self.grid: + for i in self.__grid: x = self.viewing_score(i) if x > t: t = x