From 7f2e09616c0fa3c3ddb941d7099786ff129f7d69 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Thu, 12 Dec 2024 07:31:50 -0500 Subject: [PATCH] getting started on part 2 not done --- 2024/day12/main.py | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/2024/day12/main.py b/2024/day12/main.py index 07926b0..a3e71a3 100644 --- a/2024/day12/main.py +++ b/2024/day12/main.py @@ -10,7 +10,7 @@ VISITED = set() LIMITS = [0,0] CURRENT_PLOT = () -def add_p_and_a(input, cp, a, p): +def add_p_and_ap1(input, cp, a, p): ft = input[cp[0]][cp[1]] G[CURRENT_PLOT].append(cp) VISITED.add(tuple(cp)) @@ -18,7 +18,7 @@ def add_p_and_a(input, cp, a, p): np = [cp[0]+i[0], cp[1]+i[1]] if 0 <= np[0] < LIMITS[0] and 0 <= np[1] < LIMITS[1] and tuple(np) not in VISITED: if input[np[0]][np[1]] == ft: - a, p = add_p_and_a(input, np, a, p) + a, p = add_p_and_ap1(input, np, a, p) a += 1 p += 4 for j in [[1,0],[0,1],[-1,0],[0,-1]]: @@ -27,15 +27,27 @@ def add_p_and_a(input, cp, a, p): if 0 <= nnp[0] < LIMITS[0] and 0 <= nnp[1] < LIMITS[1]: if input[nnp[0]][nnp[1]] == ft: p -= 1 - - - - - return a, p - - +def add_p_and_ap2(input, cp, a, p): + ft = input[cp[0]][cp[1]] + G[CURRENT_PLOT].append(cp) + VISITED.add(tuple(cp)) + for i in [[1,0],[0,1],[-1,0],[0,-1]]: + np = [cp[0]+i[0], cp[1]+i[1]] + if 0 <= np[0] < LIMITS[0] and 0 <= np[1] < LIMITS[1] and tuple(np) not in VISITED: + if input[np[0]][np[1]] == ft: + a, p = add_p_and_ap2(input, np, a, p) + a += 1 + p += 4 + for j in [[1,0],[0,1]]: + for i in [-1, 1]: + nnp = [cp[0]+j[0], cp[1]+j[1]] + #check the sides, to see if the perimiter needs to be decrimented + if 0 <= nnp[0] < LIMITS[0] and 0 <= nnp[1] < LIMITS[1]: + if input[nnp[0]][nnp[1]] == ft: + p -= 1 + return a, p def part_one(input): VISITED.clear() @@ -44,13 +56,21 @@ def part_one(input): for col, j in enumerate(i): if (row, col) not in VISITED: CURRENT_PLOT = (row, col) - a, p = add_p_and_a(input, [row, col], 0, 0) + a, p = add_p_and_ap1(input, [row, col], 0, 0) p1 += a*p print("Part 1: ", p1) def part_two(input): - pass + VISITED.clear() + p2 = 0 + for row, i in enumerate(input): + for col, j in enumerate(i): + if (row, col) not in VISITED: + CURRENT_PLOT = (row, col) + a, p = add_p_and_ap2(input, [row, col], 0, 0) + p2 += a*p + print("Part 2: ", p2) def main(): f = open("input.txt", 'r')