From f4bc6772f7ec544375a809fa27e42eb06952ea1a Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Thu, 12 Dec 2024 20:02:54 +0100 Subject: [PATCH] Solved 2024/12 P2 --- 2024/12/12.md | 12 ++++--- 2024/12/solution.py | 55 +++++++++++++++++++++++++------ __pycache__/fred.cpython-311.pyc | Bin 25606 -> 25675 bytes fred.py | 2 +- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/2024/12/12.md b/2024/12/12.md index fdc58d5..7e4fb13 100644 --- a/2024/12/12.md +++ b/2024/12/12.md @@ -123,8 +123,6 @@ So, it has a total price of `1930`. Your puzzle answer was `1437300`. -The first half of this puzzle is complete! It provides one gold star: \* - ## \-\-- Part Two \-\-- {#part2} Fortunately, the Elves are trying to order so much fence that they @@ -203,8 +201,14 @@ Adding these together produces its new total price of `1206`. *What is the new total price of fencing all regions on your map?* -Answer: +Your puzzle answer was `849332`. -Although it hasn\'t changed, you can still [get your puzzle +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2024) and +try another puzzle. + +If you still want to see it, you can [get your puzzle input](12/input). diff --git a/2024/12/solution.py b/2024/12/solution.py index 4acdabc..ae3db3b 100644 --- a/2024/12/solution.py +++ b/2024/12/solution.py @@ -1,11 +1,12 @@ #!/bin/python3 import sys,time,re from pprint import pprint +import numpy as np sys.path.insert(0, '../../') from fred import list2int,get_re,nprint,lprint,loadFile,toGrid,bfs,get_value_in_direction,addTuples,grid_valid start_time = time.time() -input_f = 'test' +input_f = 'input' ######################################### # # @@ -78,7 +79,7 @@ def part1(): start_time = time.time() -print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') +print('Part 1:',part1(), '\t', round((time.time() - start_time)*1000), 'ms') ######################################### @@ -104,14 +105,15 @@ def get_neighbors_and_corners(grid,node,visited): neighbors.append(n) visited.append(n) neighbors += get_neighbors_and_corners(grid,n,visited) - - return neighbors +def rotate_90_clockwise(grid:list) -> list: + return [list(row) for row in zip(*grid[::-1])] + def part2(): grid = toGrid(input_f) - + #print(grid) #nprint(grid) values = {} visited = [] @@ -136,7 +138,7 @@ def part2(): for pdx,p in enumerate(total_plots): total_plots[pdx] = sorted(p) - pprint(total_plots) + #pprint(total_plots) #directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] directions = [(0, 1), (1, 0)] @@ -164,13 +166,46 @@ def part2(): print(end) for p in total_plots: + area = len(p) + fences = 0 for r,c in p: visited = [] - print('@',(r,c)) - - find_edge(r,c,p,visited) - input() + #print('@',(r,c),get_value_in_direction(grid,(r,c))) + + if get_value_in_direction(grid,(r,c),'up') != grid[r][c] and get_value_in_direction(grid,(r,c),'left') != grid[r][c]: + #print('┌ corner - up+left') + fences+=1 + if get_value_in_direction(grid,(r,c),'up') == grid[r][c] and get_value_in_direction(grid,(r,c),'left') == grid[r][c] and get_value_in_direction(grid,(r,c),'up-left') != grid[r][c]: + #print('└ corner - up+left+upleft') + fences+=1 + + if get_value_in_direction(grid,(r,c),'left') != grid[r][c] and get_value_in_direction(grid,(r,c),'down') != grid[r][c]: + #print('└ corner - left+down') + fences+=1 + if get_value_in_direction(grid,(r,c),'left') == grid[r][c] and get_value_in_direction(grid,(r,c),'down') == grid[r][c] and get_value_in_direction(grid,(r,c),'down-left') != grid[r][c]: + #print('┐ corner - left+down+downleft') + fences+=1 + + if get_value_in_direction(grid,(r,c),'down') != grid[r][c] and get_value_in_direction(grid,(r,c),'right') != grid[r][c]: + #print('┘ corner - down+right') + fences+=1 + if get_value_in_direction(grid,(r,c),'down') == grid[r][c] and get_value_in_direction(grid,(r,c),'right') == grid[r][c] and get_value_in_direction(grid,(r,c),'down-right') != grid[r][c]: + #print('┌ corner - down+right+downright') + fences+=1 + + if get_value_in_direction(grid,(r,c),'right') != grid[r][c] and get_value_in_direction(grid,(r,c),'up') != grid[r][c]: + #print('┐ corner - right+up') + fences+=1 + if get_value_in_direction(grid,(r,c),'right') == grid[r][c] and get_value_in_direction(grid,(r,c),'up') == grid[r][c] and get_value_in_direction(grid,(r,c),'up-right') != grid[r][c]: + #print('└ corner - right+up+upright') + fences+=1 + #┌ ┐ + #└ ┘ + #input() + + result+=(area*fences) + # for v in total_plots: # total_corners = 0 # corners = 0 diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index a00555043c65c5be6727bd6626b31d10db284747..686326e109645d8e776a0b8060058dd12bcd11e0 100644 GIT binary patch delta 319 zcmZoW!Fc)vqxy1QUM>b82-c2HpXbTI@EF8_0d6Scv&2MoGe*scarul|6VGY-YNyDh za;C_qilr!|3Z^Kga-=Aw3Z`+TD7Uai$)u>XFht3w=%lE%Fhr z5Rk^0q5+gsNMQ_S(A3>*!r02pYGk5sXta4B%Lntx{_aYf*V(M*H0%Z{*JLVU0#Zdr z5H?7t2r3R{L&ZTe%^-FQh-d{7ZJUeT6*w5XH&5|j!NE9TvTcMab8xFZym-s{Q0@EF8_0X8V(Gsi@AGe*^karumD6VGYds;9`M z3Z^lo$hWXY$)qT>Fht3wXrw5$Fhr5Rk^0q5_mtNMQ_S(A3=Q z!r02pYG|r&XuSCx%Lnt#4{TO*8g>AcYBCiu0jVM*2pc3+1QiFfq2eH_sAKXTw^Nf3 y+sHB%HE*8ouE4?Av3ZC83J%8J$%zrFjD4HiBN90oEjPbRJj=**!g%t{