[py] Port 2018_03
This commit is contained in:
parent
167e0125ac
commit
71b7bcc548
3 changed files with 59 additions and 57 deletions
|
|
@ -2,7 +2,7 @@ import sys
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from .y2018 import d01, d02
|
||||
from .y2018 import d01, d02, d03
|
||||
from .y2020 import d10
|
||||
from .y2021 import d14
|
||||
from .y2022 import d01, d02, d03, d04, d05, d06
|
||||
|
|
@ -10,6 +10,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
|
|||
DAYS = {
|
||||
"2018_01": y2018.d01.solve,
|
||||
"2018_02": y2018.d02.solve,
|
||||
"2018_03": y2018.d03.solve,
|
||||
"2020_10": y2020.d10.solve,
|
||||
"2021_14": y2021.d14.solve,
|
||||
"2022_01": y2022.d01.solve,
|
||||
|
|
|
|||
57
py/aoc/y2018/d03.py
Normal file
57
py/aoc/y2018/d03.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import re
|
||||
|
||||
CLAIM_RE = r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)"
|
||||
|
||||
|
||||
def load_claims(inputstr):
|
||||
claims = {}
|
||||
for line in inputstr.splitlines():
|
||||
match = re.fullmatch(CLAIM_RE, line)
|
||||
elf, x, y, w, h = match.groups()
|
||||
elf, x, y, w, h = int(elf), int(x), int(y), int(w), int(h)
|
||||
claims[elf] = (x, y, w, h)
|
||||
return claims
|
||||
|
||||
|
||||
# PART 1
|
||||
|
||||
|
||||
def count_claims(claims):
|
||||
squares = {}
|
||||
for x, y, w, h in claims.values():
|
||||
for dw in range(w):
|
||||
for dh in range(h):
|
||||
coords = (x + dw, y + dh)
|
||||
squares[coords] = squares.get(coords, 0) + 1
|
||||
return squares
|
||||
|
||||
|
||||
def find_doubles(squares):
|
||||
return len(list(filter(lambda x: x > 1, squares.values())))
|
||||
|
||||
|
||||
# PART 2
|
||||
|
||||
|
||||
def is_intact(squares, x, y, w, h):
|
||||
for dw in range(w):
|
||||
for dh in range(h):
|
||||
coords = (x + dw, y + dh)
|
||||
if squares.get(coords, 0) > 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def find_intact_claim(claims, squares):
|
||||
for elf, (x, y, w, h) in claims.items():
|
||||
if is_intact(squares, x, y, w, h):
|
||||
return elf
|
||||
|
||||
|
||||
def solve(inputstr):
|
||||
claims = load_claims(inputstr)
|
||||
squares = count_claims(claims)
|
||||
doubles = find_doubles(squares)
|
||||
print(f"Part 1: {doubles}")
|
||||
intact = find_intact_claim(claims, squares)
|
||||
print(f"Part 2: {intact}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue