Compare commits
2 Commits
04cae681c3
...
7ce86c3d44
Author | SHA1 | Date | |
---|---|---|---|
7ce86c3d44 | |||
dfa3493046 |
77
2023/day18/part1.1.py
Normal file
77
2023/day18/part1.1.py
Normal file
@ -0,0 +1,77 @@
|
||||
import sys
|
||||
import os
|
||||
from pprint import pprint
|
||||
import time
|
||||
|
||||
|
||||
from shapely.geometry import Point
|
||||
from shapely.geometry.polygon import Polygon
|
||||
import shapely
|
||||
|
||||
import numpy as np
|
||||
|
||||
from matplotlib.path import Path
|
||||
import sympy
|
||||
#colors
|
||||
from termcolor import colored
|
||||
|
||||
def p(x,inside):
|
||||
for idx,i in enumerate(x):
|
||||
for jdx,j in enumerate(i):
|
||||
|
||||
#if j == '#':
|
||||
# print(colored(j,'red'),end='')
|
||||
#if (idx,jdx) in steps:
|
||||
# print(colored(j,'blue'),end='')
|
||||
if (idx,jdx) in inside:
|
||||
print(colored(j,'green'),end='')
|
||||
else:
|
||||
print(j,end='')
|
||||
print()
|
||||
|
||||
input_f = ''
|
||||
if len(sys.argv) == 1:
|
||||
input_f = 'test'
|
||||
else:
|
||||
input_f = sys.argv[1]
|
||||
|
||||
grid = []
|
||||
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
grid.append(line.rstrip().split())
|
||||
|
||||
steps = []
|
||||
|
||||
size = 20
|
||||
|
||||
cur = (0,0)
|
||||
|
||||
#cur = (int(size/2),int(size/2))
|
||||
|
||||
steps.append(cur)
|
||||
|
||||
for i in grid:
|
||||
direction,length,color = i
|
||||
length = int(length)
|
||||
|
||||
if direction == 'R':
|
||||
cur = (cur[0],cur[1]+length)
|
||||
|
||||
if direction == 'D':
|
||||
cur = (cur[0]+length,cur[1])
|
||||
|
||||
if direction == 'L':
|
||||
cur = (cur[0],cur[1]-length)
|
||||
|
||||
if direction == 'U':
|
||||
cur = (cur[0]-length,cur[1])
|
||||
|
||||
steps.append(cur)
|
||||
|
||||
polygon = Polygon(steps)
|
||||
|
||||
area = polygon.buffer(0.5,join_style="mitre").area
|
||||
|
||||
print(int(area))
|
||||
|
@ -6,9 +6,12 @@ import time
|
||||
|
||||
from shapely.geometry import Point
|
||||
from shapely.geometry.polygon import Polygon
|
||||
import shapely
|
||||
|
||||
import numpy as np
|
||||
|
||||
from matplotlib.path import Path
|
||||
|
||||
import sympy
|
||||
#colors
|
||||
from termcolor import colored
|
||||
|
||||
@ -18,8 +21,8 @@ def p(x,inside):
|
||||
|
||||
#if j == '#':
|
||||
# print(colored(j,'red'),end='')
|
||||
#elif (idx,jdx) in steps:
|
||||
# print(colored(j,'green'),end='')
|
||||
#if (idx,jdx) in steps:
|
||||
# print(colored(j,'blue'),end='')
|
||||
if (idx,jdx) in inside:
|
||||
print(colored(j,'green'),end='')
|
||||
else:
|
||||
@ -41,7 +44,7 @@ with open(input_f) as file:
|
||||
pprint(grid)
|
||||
|
||||
|
||||
size = 1000
|
||||
size = 20
|
||||
pit = [ ['.']*size for i in range(size)]
|
||||
|
||||
cur = (int(size/2),int(size/2))
|
||||
@ -112,23 +115,32 @@ for ydx,y in enumerate(pit):
|
||||
|
||||
#p(pit,s_ground)
|
||||
|
||||
pprint(len(s_ground))
|
||||
pprint(len(m_ground))
|
||||
# pprint(len(s_ground))
|
||||
# pprint(len(m_ground))
|
||||
|
||||
print(len(m_ground)+len(set(steps)))
|
||||
# print(len(m_ground)+len(set(steps)))
|
||||
|
||||
print('Part 1: ' + str(len(set(steps))))
|
||||
print('Part 1.2: ' + str(len(s_ground)+len(set(steps))))
|
||||
# print('Part 1: ' + str(len(set(steps))))
|
||||
# print('Part 1.2: ' + str(len(s_ground)+len(set(steps))))
|
||||
|
||||
print(len(ground) + len(set(steps)))
|
||||
# print(len(ground) + len(set(steps)))
|
||||
# p(pit,s_ground)
|
||||
# print(len(s_ground))
|
||||
# p(pit,m_ground)
|
||||
# print(len(m_ground))
|
||||
|
||||
#print('Part 1: ',end='')
|
||||
#print(area(polygon))
|
||||
#print(len(m_ground))
|
||||
|
||||
print('Part 1.2: ' + str(shapely.area(polygon)))
|
||||
|
||||
|
||||
|
||||
# 12835
|
||||
# 13134
|
||||
# 17440
|
||||
# 57559
|
||||
# 58612
|
||||
# 62918
|
||||
# Part 1: 4306
|
||||
# Part 1.2: 17141
|
||||
# Part 1.2: 61865 //corrent
|
||||
# 4306
|
||||
|
||||
|
||||
|
89
2023/day18/part2.py
Normal file
89
2023/day18/part2.py
Normal file
@ -0,0 +1,89 @@
|
||||
import sys
|
||||
import os
|
||||
from pprint import pprint
|
||||
import time
|
||||
|
||||
|
||||
from shapely.geometry import Point
|
||||
from shapely.geometry.polygon import Polygon
|
||||
import shapely
|
||||
|
||||
import numpy as np
|
||||
|
||||
from matplotlib.path import Path
|
||||
import sympy
|
||||
#colors
|
||||
from termcolor import colored
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
def p(x,inside):
|
||||
for idx,i in enumerate(x):
|
||||
for jdx,j in enumerate(i):
|
||||
|
||||
#if j == '#':
|
||||
# print(colored(j,'red'),end='')
|
||||
#if (idx,jdx) in steps:
|
||||
# print(colored(j,'blue'),end='')
|
||||
if (idx,jdx) in inside:
|
||||
print(colored(j,'green'),end='')
|
||||
else:
|
||||
print(j,end='')
|
||||
print()
|
||||
|
||||
input_f = ''
|
||||
if len(sys.argv) == 1:
|
||||
input_f = 'test'
|
||||
else:
|
||||
input_f = sys.argv[1]
|
||||
|
||||
grid = []
|
||||
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
grid.append(line.rstrip().split())
|
||||
|
||||
steps = []
|
||||
|
||||
#size = 20
|
||||
|
||||
cur = (0,0)
|
||||
|
||||
steps.append(cur)
|
||||
|
||||
for i in grid:
|
||||
color = i[2][2:-1]
|
||||
|
||||
match int(color[-1:]):
|
||||
case 0:
|
||||
direction = 'R'
|
||||
case 1:
|
||||
direction = 'D'
|
||||
case 2:
|
||||
direction = 'L'
|
||||
case 3:
|
||||
direction = 'U'
|
||||
|
||||
length = int(color[:-1], 16)
|
||||
|
||||
if direction == 'R':
|
||||
cur = (cur[0],cur[1]+length)
|
||||
|
||||
if direction == 'D':
|
||||
cur = (cur[0]+length,cur[1])
|
||||
|
||||
if direction == 'L':
|
||||
cur = (cur[0],cur[1]-length)
|
||||
|
||||
if direction == 'U':
|
||||
cur = (cur[0]-length,cur[1])
|
||||
|
||||
steps.append(cur)
|
||||
|
||||
polygon = Polygon(steps)
|
||||
|
||||
area = polygon.buffer(0.5,join_style="mitre").area
|
||||
|
||||
print(int(area))
|
||||
|
||||
print("--- %s seconds ---" % (time.time() - start_time))
|
Loading…
Reference in New Issue
Block a user