diff --git a/2020-python/solutions/day_24.py b/2020-python/solutions/day_24.py index ec724ea..2334128 100644 --- a/2020-python/solutions/day_24.py +++ b/2020-python/solutions/day_24.py @@ -2,6 +2,7 @@ import pathlib import time +import cProfile import re import operator import itertools @@ -70,16 +71,30 @@ def find_dst_tile(steps: list) -> tuple: def count_black_neighbors(tiles: set, x_tile: int, y_tile: int) -> int: """return the number of black adjacent tile of a given one""" - return sum((x_tile+d_x, y_tile+d_y) in tiles for d_x, d_y in STEPMAP.values()) + black_neighbors = 0 + for d_x, d_y in STEPMAP.values(): + if (x_tile+d_x, y_tile+d_y) in tiles: + black_neighbors += 1 + return black_neighbors def calculate_floor_bounds(tiles: set) -> tuple: """return the floor boundaries""" - min_x = min(map(operator.itemgetter(0), tiles)) - 1 - min_y = min(map(operator.itemgetter(1), tiles)) - 1 - max_x = max(map(operator.itemgetter(0), tiles)) + 2 - max_y = max(map(operator.itemgetter(1), tiles)) + 2 + min_x = float('Inf') + max_x = float('-Inf') + min_y = float('Inf') + max_y = float('-Inf') - return range(min_x, max_x), range(min_y, max_y) + for x_tile, y_tile in tiles: + if x_tile < min_x: + min_x = x_tile + elif x_tile > max_x: + max_x = x_tile + if y_tile < min_y: + min_y = y_tile + elif y_tile > max_y: + max_y = y_tile + + return range(min_x-1, max_x+2), range(min_y-1, max_y+2) def flip_tiles(tiles: set) -> set: """calculate the new daily floor""" diff --git a/README.md b/README.md index 469d50d..7a5bdf4 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,4 @@ | [Day 21](https://adventofcode.com/2020/day/21) | [2.177ms](./2020-python/solutions/day_21.py) | [1.578ms](./2020-python/solutions/day_21.py) | | [Day 22](https://adventofcode.com/2020/day/22) | [1.516s](./2020-python/solutions/day_22.py) | [1.353s](./2020-python/solutions/day_22.py) | | [Day 23](https://adventofcode.com/2020/day/23) | [14.754s](./2020-python/solutions/day_23.py) | [3.572s](./2020-python/solutions/day_23.py) | -| [Day 24](https://adventofcode.com/2020/day/24) | [1.097s](./2020-python/solutions/day_24.py) | [457.262ms](./2020-python/solutions/day_24.py) | +| [Day 24](https://adventofcode.com/2020/day/24) | [753.985ms](./2020-python/solutions/day_24.py) | [357.455ms](./2020-python/solutions/day_24.py) |