diff --git a/2024/12/daily.py b/2024/12/daily.py index e502ef8..4b6cf43 100644 --- a/2024/12/daily.py +++ b/2024/12/daily.py @@ -52,6 +52,58 @@ def perimeter(grid, block): return sum +def sides(grid, block): + sum = 0 + + min_x = min([x for x, _ in block]) + max_x = max([x for x, _ in block]) + min_y = min([y for _, y in block]) + max_y = max([y for _, y in block]) + + # read from left to right + # if there's a block and not a block above then there's a side + # side_check on, if side_check off, add one to side + # if not a block then side_check off + # new line reset side_check + for y in range(min_y, max_y + 1): + up_side_check = False + down_side_check = False + for x in range(min_x, max_x + 1): + if (x, y) in block and (x, y - 1) not in block: + if not up_side_check: + up_side_check = True + sum += 1 + else: + up_side_check = False + + if (x, y) in block and (x, y + 1) not in block: + if not down_side_check: + down_side_check = True + sum += 1 + else: + down_side_check = False + + # Do the same but left and right + for x in range(min_x, max_x + 1): + right_side_check = False + left_side_check = False + for y in range(min_y, max_y + 1): + if (x, y) in block and (x + 1, y) not in block: + if not right_side_check: + right_side_check = True + sum += 1 + else: + right_side_check = False + + if (x, y) in block and (x - 1, y) not in block: + if not left_side_check: + left_side_check = True + sum += 1 + else: + left_side_check = False + + return sum + def part1(input_text): grid = parse(input_text) block_sets = [] @@ -73,7 +125,24 @@ def part1(input_text): return sum def part2(input_text): - return 0 + grid = parse(input_text) + block_sets = [] + visited = set() + + for y in range(len(grid)): + for x in range(len(grid[y])): + if (x, y) in visited: + continue + block = blocks(grid, x, y) + block_sets.append(block) + visited.update(block) + + sum = 0 + for block in block_sets: + s = sides(grid, block) + sum += s * len(block) + + return sum if __name__ == "__main__": with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file: diff --git a/2024/12/daily_test.py b/2024/12/daily_test.py index 6026e5d..2e15a28 100644 --- a/2024/12/daily_test.py +++ b/2024/12/daily_test.py @@ -16,6 +16,29 @@ def sample_data(): MMMISSJEEE """ +@pytest.fixture +def e_grid(): + s = """ +EEEEE +EXXXX +EEEEE +EXXXX +EEEEE +""" + return(parse(s)) + +@pytest.fixture +def ab_grid(): + s = """ +AAAAAA +AAABBA +AAABBA +ABBAAA +ABBAAA +AAAAAA +""" + return(parse(s)) + @pytest.fixture def parsed_data(sample_data): return parse(sample_data) @@ -37,9 +60,16 @@ def test_perimeter(parsed_data): b = blocks(parsed_data, 0, 0) assert perimeter(parsed_data, b) == 18 +def test_sides_e_grid(e_grid): + b = blocks(e_grid, 0, 0) + assert sides(e_grid, b) == 12 + +def test_sides_ab_grid(ab_grid): + b = blocks(ab_grid, 0, 0) + assert sides(ab_grid, b) == 12 def test_part1(sample_data): assert part1(sample_data) == 1930 -# def test_part2(sample_data): -# assert part2(sample_data) == 81 +def test_part2(sample_data): + assert part2(sample_data) == 1206