Files
aocpy/2018/d3.py
felixm 3a915cb9e3 Start solving 2018 problems
I have also updated get.py to download the problems as
`d<day>.txt` instead of `i<day>.txt`. That allows me
to get the day input via `__input__.replace('.py', '.txt')`
which is a little more concise. I don't know why
I didn't do this earlier.
2024-07-04 11:10:27 -04:00

45 lines
996 B
Python

from lib import str_to_ints
from collections import defaultdict
def part_1(data):
claims = defaultdict(int)
for line in data.splitlines():
id, x, y, w, h = str_to_ints(line)
for dx in range(w):
for dy in range(h):
claims[(x + dx, y + dy)] += 1
r = sum([1 for v in claims.values() if v > 1])
print(r)
def part_2(data):
claims = defaultdict(list)
all_ids = set()
for line in data.splitlines():
id, x, y, w, h = str_to_ints(line)
for dx in range(w):
for dy in range(h):
claims[(x + dx, y + dy)].append(id)
all_ids.add(id)
for xs in claims.values():
if len(xs) > 1:
for x in xs:
if x in all_ids:
all_ids.remove(x)
assert len(all_ids) == 1
print(all_ids.pop())
def main():
with open("i3.txt") as f:
data = f.read()
part_1(data)
part_2(data)
if __name__ == "__main__":
main()