Solve 2021:9 "Smoke Basin"
Had to rewrite whole solution due to getting the example input right, but wrong on the actual puzzle input. Off by like 20.
This commit is contained in:
parent
811d2cb224
commit
c7753f58fc
2 changed files with 71 additions and 0 deletions
34
2021-python/solutions/day_09.py
Normal file
34
2021-python/solutions/day_09.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from solutions import BaseSolution
|
||||
|
||||
|
||||
class Solution(BaseSolution):
|
||||
input_file = "09.txt"
|
||||
|
||||
def __str__(self):
|
||||
return "Day 9: Smoke Basin"
|
||||
|
||||
def parse_input(self, data):
|
||||
return [[int(c) for c in r] for r in data.split()]
|
||||
|
||||
def solve(self, puzzle_input):
|
||||
lp = 0
|
||||
lpi = len(puzzle_input)
|
||||
for v, row in enumerate(puzzle_input):
|
||||
lr = len(row)
|
||||
for i in range(lr):
|
||||
x = row[i]
|
||||
s1 = row[i - 1] if i > 0 else 11
|
||||
s2 = row[i + 1] if i < lr - 1 else 11
|
||||
s3 = puzzle_input[v - 1][i] if v > 0 else 11
|
||||
s4 = puzzle_input[v + 1][i] if v < lpi - 1 else 11
|
||||
if all(x < s for s in [s1, s2, s3, s4]):
|
||||
lp += 1 + x
|
||||
return lp
|
||||
|
||||
def solve_again(self, puzzle_input):
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solution = Solution()
|
||||
solution.show_results()
|
||||
37
2021-python/tests/test_day_09.py
Normal file
37
2021-python/tests/test_day_09.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import unittest
|
||||
|
||||
from solutions.day_09 import Solution
|
||||
|
||||
|
||||
class Day09TestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.solution = Solution()
|
||||
self.puzzle_input = self.solution.parse_input(
|
||||
"""
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
"""
|
||||
)
|
||||
|
||||
def test_parse_puzzle_input(self):
|
||||
data = """
|
||||
2199943210
|
||||
3987894921
|
||||
"""
|
||||
assert self.solution.parse_input(data) == [
|
||||
[2, 1, 9, 9, 9, 4, 3, 2, 1, 0],
|
||||
[3, 9, 8, 7, 8, 9, 4, 9, 2, 1],
|
||||
]
|
||||
|
||||
def test_solve_first_part(self):
|
||||
assert self.solution.solve(self.puzzle_input) == 15
|
||||
|
||||
# def test_solve_second_part(self):
|
||||
# assert self.solution.solve_again(self.puzzle_input) == True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Reference in a new issue