master
Alfred Melch 6 years ago
parent de9bb292a3
commit 08a2687d94

@ -0,0 +1,73 @@
#!/usr/bin/env python3
import re
with open('input.txt', 'r') as f:
claims_raw = [x.strip() for x in f.readlines()]
# print(claims_raw)
class Claim:
def __init__(self, claim_id, x, y, width, height):
self.claim_id = claim_id
self.x1 = x
self.y1 = y
self.width = width
self.height = height
self._claimed_patches = None
@property
def x2(self):
return self.x1 + self.width - 1
@property
def y2(self):
return self.y1 + self.height - 1
def claimed_patches(self):
if self._claimed_patches is not None:
return self._claimed_patches
patches = set()
for i in range(self.x1, self.x2 + 1):
for j in range(self.y1, self.y2 + 1):
patches.add((i, j))
self._claimed_patches = patches
return patches
def intersections(self, claim):
return self.claimed_patches().intersection(claim.claimed_patches())
claim_regex = re.compile(r'#(\d*) @ (\d*),(\d*): (\d*)x(\d*)')
claims = list()
for claim in claims_raw:
parse = claim_regex.match(claim)
claims.append(Claim(
int(parse[1]),
int(parse[2]),
int(parse[3]),
int(parse[4]),
int(parse[5])
))
# print(claims)
# print(claims[0].width, claims[0].height, len(claims[0].claimed_patches()))
# print(claims[0].intersections(claims[1]))
c1 = Claim(1, 1, 3, 4, 4)
c2 = Claim(1, 3, 1, 4, 4)
print(c1.intersections(c2))
# sqares that are claimed by two or more claims
all_interference = list()
for i, claim1 in enumerate(claims):
for claim2 in claims[i + 1:]:
tmp = claim1.intersections(claim2)
if len(tmp) != 0:
print('intersection found:', claim1.claim_id, claim2.claim_id, len(tmp))
all_interference.extend(tmp)
print(len(set(all_interference)))

@ -0,0 +1,67 @@
#!/usr/bin/env python3
import re
with open('input.txt', 'r') as f:
claims_raw = [x.strip() for x in f.readlines()]
# print(claims_raw)
class Claim:
def __init__(self, claim_id, x, y, width, height):
self.claim_id = claim_id
self.x1 = x
self.y1 = y
self.width = width
self.height = height
self._claimed_patches = None
@property
def x2(self):
return self.x1 + self.width - 1
@property
def y2(self):
return self.y1 + self.height - 1
def claimed_patches(self):
if self._claimed_patches is not None:
return self._claimed_patches
patches = set()
for i in range(self.x1, self.x2 + 1):
for j in range(self.y1, self.y2 + 1):
patches.add((i, j))
self._claimed_patches = patches
return patches
def intersections(self, claim):
return self.claimed_patches().intersection(claim.claimed_patches())
claim_regex = re.compile(r'#(\d*) @ (\d*),(\d*): (\d*)x(\d*)')
claims = list()
for claim in claims_raw:
parse = claim_regex.match(claim)
claims.append(Claim(
int(parse[1]),
int(parse[2]),
int(parse[3]),
int(parse[4]),
int(parse[5])
))
for claim1 in claims:
intersects = False
for claim2 in claims:
if claim1 is claim2:
continue
tmp = claim1.intersections(claim2)
if len(tmp) != 0:
intersects = True
break
if not intersects:
break
print(claim1, claim1.claim_id)

@ -0,0 +1,59 @@
--- Day 3: No Matter How You Slice It ---
The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit (thanks to someone who helpfully wrote its box IDs on the wall of the warehouse in the middle of the night). Unfortunately, anomalies are still affecting them - nobody can even agree on how to cut the fabric.
The whole piece of fabric they're working on is a very large square - at least 1000 inches on each side.
Each Elf has made a claim about which area of fabric would be ideal for Santa's suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim's rectangle is defined as follows:
The number of inches between the left edge of the fabric and the left edge of the rectangle.
The number of inches between the top edge of the fabric and the top edge of the rectangle.
The width of the rectangle in inches.
The height of the rectangle in inches.
A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:
...........
...........
...#####...
...#####...
...#####...
...#####...
...........
...........
...........
The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2
Visually, these claim the following areas:
........
...2222.
...2222.
.11XX22.
.11XX22.
.111133.
.111133.
........
The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.)
If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?
Your puzzle answer was 100595.
--- Part Two ---
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
For example, in the claims above, only claim 3 is intact after all claims are made.
What is the ID of the only claim that doesn't overlap?
Your puzzle answer was 415.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,2 @@
01: 100595
02: 415
Loading…
Cancel
Save