![Smile :-)](./images/smilies/icon_smile.gif)
Mein Code speichert das Ergebnis ja auch als Bild. Das habe ich eben noch schnell durch das markieren des nicht-überlappenden Teils ergänzt. Sieht bei jetzt so aus:
![Bild](https://i.imgur.com/5xInxst.png)
Code: Alles auswählen
# ----- Part 1 -----
claims = list()
with open('day_03_input.txt', 'r') as inputfile:
for line in inputfile:
origin, size = line.split()[2:]
start_x, start_y = origin[:-1].split(',')
origin = tuple([int(start_x), int(start_y)])
width, height = size.split('x')
size = tuple([int(width), int(height)])
claims.append(tuple([origin, size]))
squares = list()
for origin, size in claims:
start_x, start_y = origin
width, height = size
square_inches = set([(start_x + x, start_y + y) for x in range(width) for y in range(height)])
squares.append(square_inches)
union = set()
for i, mat1 in enumerate(squares):
for j, mat2 in enumerate(squares):
if j > i:
intersection = mat1 & mat2
if len(intersection) > 0:
union = union | intersection
print(len(union))
# ----- Part 2 -----
for idnum, square in enumerate(squares, 1):
intersection = square & union
if len(intersection) == 0:
print(idnum)
Ging mir so ähnlich, deine Variante ist aber noch etwas konsequenter
Code: Alles auswählen
#!/usr/bin/env python3
import re
import fileinput
from collections import namedtuple
Claim = namedtuple('Claim', ['id', 'left', 'top', 'width', 'height'])
PATTERN = re.compile(r'\d+')
def iter_claims():
for line in fileinput.input():
yield Claim(*[int(item) for item in re.findall(PATTERN, line)])
def main():
claims = list(iter_claims())
claim_map = dict()
isolated = set(claim.id for claim in claims)
for claim in claims:
for row in range(claim.top, claim.top + claim.height):
for cell in range(claim.left, claim.left + claim.width):
claim_map.setdefault((row, cell), set())
claim_map[(row, cell)].add(claim.id)
if len(claim_map[(row, cell)]) > 1:
isolated -= set(claim_map[(row, cell)])
print('Squares:', sum(len(cell) > 1 for cell in claim_map.values()))
assert len(isolated) == 1
print('Isolated:', isolated.pop())
if __name__ == '__main__':
main()
Code: Alles auswählen
#!/usr/bin/env python3
from itertools import combinations
# ----- Part 1 -----
claims = list()
with open('day_03_input.txt', 'r') as inputfile:
for line in inputfile:
origin, size = line.split()[2:]
origin = tuple(map(int, origin[:-1].split(',')))
size = tuple(map(int, size.split('x')))
claims.append((origin, size))
squares = [
set((start_x + x, start_y + y) for x in range(width) for y in range(height))
for (start_x, start_y), (width, height) in claims
]
union = set()
for mat1, mat2 in combinations(squares, 2):
union |= mat1 & mat2
print(len(union))
# ----- Part 2 -----
for idnum, square in enumerate(squares, 1):
if union.isdisjoint(square):
print(idnum)
Hi __blackjack__, es ist mir immer eine Freude, deine Code-Beispiele durch zu arbeiten, sie zu verstehen und daraus zu lernen.__blackjack__ hat geschrieben: ↑Donnerstag 27. Dezember 2018, 11:42 @ThomasL: Ich habe Deinen Code mal überarbeitet, aber der Ansatz ist IMHO nicht gut, denn Dein Original braucht bei mir 11 Sekunden! Das habe ich auf knapp unter 4 drücken können, ohne das ich speziell irgendwas gemacht habe ausser Vereinfachungen die IMHO alle nicht auf Kosten der Lesbarkeit gehen, aber gegen den Sekundenbruchteil den der Ansatz von kbr und mir braucht, ist das IMHO immer noch zu langsam.
Code: Alles auswählen
from itertools import combinations
import re
# ----- Part 1 -----
pattern = re.compile(r'#\d+ @ (\d+),(\d+): (\d+)x(\d+)')
squares = list()
with open('day_03_input.txt', 'r') as inputfile:
for line in inputfile:
start_x, start_y, width, height = list(map(int, pattern.match(line).groups()))
squares.append(set((start_x + x, start_y + y) for x in range(width) for y in range(height)))
union = set()
for mat1, mat2 in combinations(squares, 2):
union |= mat1 & mat2
print(len(union))
# ----- Part 2 -----
for idnum, square in enumerate(squares, 1):
if union.isdisjoint(square):
print(idnum)
Langsam wird das ja eher zum Advent of Intcode
Kommt mir bekannt vor....__blackjack__ hat geschrieben: ↑Montag 9. Dezember 2019, 22:42 ....Da habe ich mit Umwandlung in Zeichenketten und dann die einzelnen Ziffern wieder in `int` umwandeln gearbeitet, und dann die Reihenfolge umgedreht und per „slicing“ nur die nötige Anzahl davon dann weitergereicht......
Code: Alles auswählen
def _parse_instruction(instruction):
opcode = instruction % 100
modes = [
(instruction // 100) % 10,
(instruction // 1000) % 10,
(instruction // 10000) % 10,
]
return opcode, modes