Avent of Code - Day 14 - Regolith ReservoirsteemCreated with Sketch.

in #adventofcode2 years ago

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 14 - Regolith Reservoir

https://adventofcode.com/2022/day/14

image.png

Q1

import sys
from collections import defaultdict, deque
from math import inf

file1 = open(sys.argv[1], "r")

grid = defaultdict(lambda: ".")
sand = (500, 0)
minx = sand[0]
maxx = sand[0]
miny = sand[1]
maxy = sand[1]

while True:
    line = file1.readline()
    if not line:
        break
    line = line.strip()
    arr = line.split("->")
    prev = None
    for s in arr:
        a, b = map(int, s.split(","))
        minx = min(minx, a)
        maxx = max(maxx, a)
        miny = min(miny, b)
        maxy = max(maxy, b)
        grid[(a, b)] = "#"
        if prev:
            if a == prev[0]:
                for x in range(min(b, prev[1]), max(b, prev[1]) + 1):
                    grid[(a, x)] = "#"
            elif b == prev[1]:
                for x in range(min(a, prev[0]), max(a, prev[0]) + 1):
                    grid[(x, b)] = "#"
        prev = (a, b)

ans = 0

print(minx, maxx, miny, maxy)
while True:
    x, y = sand
    settled = False
    while minx <= x <= maxx and miny <= y <= maxy:
        if grid[(x, y + 1)] == ".":
            y += 1
        elif grid[(x + 1, y + 1)] == "#" and grid[(x - 1, y + 1)] == "#":
            grid[(x, y)]= "#"
            ans += 1
            settled = True
            break
        elif grid[(x - 1, y + 1)] == ".":
            y += 1
            x -= 1
        else:
            y += 1
            x += 1
    if not settled:
        break

print(ans)

Q2

import sys
from collections import defaultdict, deque
from math import inf

file1 = open(sys.argv[1], "r")

grid = defaultdict(lambda: ".")
sand = (500, 0)
minx = sand[0]
maxx = sand[0]
miny = sand[1]
maxy = sand[1]

while True:
    line = file1.readline()
    if not line:
        break
    line = line.strip()
    arr = line.split("->")
    prev = None
    for s in arr:
        a, b = map(int, s.split(","))
        minx = min(minx, a)
        maxx = max(maxx, a)
        miny = min(miny, b)
        maxy = max(maxy, b)
        grid[(a, b)] = "#"
        if prev:
            if a == prev[0]:
                for x in range(min(b, prev[1]), max(b, prev[1]) + 1):
                    grid[(a, x)] = "#"
            elif b == prev[1]:
                for x in range(min(a, prev[0]), max(a, prev[0]) + 1):
                    grid[(x, b)] = "#"
        prev = (a, b)

ans = 0
ground_level = maxy + 2

while grid[sand] != "#":
    x, y = sand
    while True:
        if y + 1 == ground_level:
            grid[(x, y)] = "#"
            ans += 1
            break
        if grid[(x, y + 1)] == ".":
            y += 1
        elif grid[(x + 1, y + 1)] == "#" and grid[(x - 1, y + 1)] == "#":
            grid[(x, y)]= "#"
            ans += 1
            break
        elif grid[(x - 1, y + 1)] == ".":
            y += 1
            x -= 1
        else:
            y += 1
            x += 1

print(ans)

This is just a simulation algorithm - follow the instructions. Since the ground is unlimited - we can use the hash map and the keys are the tuples of coordinates (x and y)


Steem to the Moon!

Coin Marketplace

STEEM 0.25
TRX 0.21
JST 0.036
BTC 98122.26
ETH 3384.17
USDT 1.00
SBD 3.38