Day 3: Foggy Forest

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • Chais@sh.itjust.works
    link
    fedilink
    arrow-up
    2
    ·
    1 day ago

    Python

    from pathlib import Path
    from typing import Any
    
    
    def parse_input(input: str) -> list[str]:
        return input.strip().splitlines()
    
    
    def part_one(input: str) -> int:
        p = parse_input(input)
        s = 0
        for m in range(len(p) - 1):
            for n in range(len(p) - 1):
                for l in range(1, len(p) - max(n, m)):
                    if p[m][n] == p[m + l][n] == p[m][n + l] == p[m + l][n + l] == "#":
                        s += 1
        return s
    
    
    def part_two(input: str) -> int:
        p = parse_input(input)
        s = 0
        for m in range(len(p) - 1):
            for n in range(len(p) - 1):
                for l in range(1, len(p) - max(n, m)):
                    for o in range(l):
                        if p[m][n + o] == p[m + l - o][n] == p[m + o][n + l] == p[m + l][n + l - o] == "#":
                            s += 1
        return s
    
    
    if __name__ == "__main__":
        input = Path("input").read_text("utf-8")
        print(part_one(input))
        print(part_two(input))