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.
This commit is contained in:
2024-07-04 11:10:11 -04:00
parent dcfae2cb13
commit 3a915cb9e3
7 changed files with 204 additions and 1 deletions

44
2018/d3.py Normal file
View File

@@ -0,0 +1,44 @@
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()