summaryrefslogtreecommitdiff
path: root/22_2023.py
blob: 389f62f8ef585d693dc91a692f2fbb3c6252bd9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
import re
from copy import deepcopy
from math import gcd
from collections import defaultdict, Counter, deque
import heapq
import math

D = open(sys.argv[1]).read().strip()
L = D.split("\n")
G = [[c for c in row] for row in L]
R = len(G)
C = len(G[0])

BS = []
for line in L:
    st, ed = line.split("~")
    sx, sy, sz = [int(x) for x in st.split(",")]
    ex, ey, ez = [int(x) for x in ed.split(",")]
    B = []
    if sx == ex and sy == ey:
        assert sz <= ez
        for z in range(sz, ez + 1):
            B.append((sx, sy, z))
    elif sx == ex and sz == ez:
        assert sy <= ey
        for y in range(sy, ey + 1):
            B.append((sx, y, sz))
    elif sy == ey and sz == ez:
        assert sx <= ex
        for x in range(sx, ex + 1):
            B.append((x, sy, sz))
    else:
        assert False
    assert len(B) >= 1
    BS.append(B)

SEEN = set()
for B in BS:
    for x, y, z in B:
        SEEN.add((x, y, z))

while True:
    any_ = False
    for i, B in enumerate(BS):
        ok = True
        for x, y, z in B:
            if z == 1:
                ok = False
            if (x, y, z - 1) in SEEN and (x, y, z - 1) not in B:
                ok = False
        if ok:
            any_ = True
            for x, y, z in B:
                assert (x, y, z) in SEEN
                SEEN.discard((x, y, z))
                SEEN.add((x, y, z - 1))
            BS[i] = [(x, y, z - 1) for (x, y, z) in B]
    if not any_:
        break
OLD_SEEN = deepcopy(SEEN)
OLD_B = deepcopy(BS)

p1 = 0
p2 = 0
for i, B in enumerate(BS):
    SEEN = deepcopy(OLD_SEEN)
    BS = deepcopy(OLD_B)
    for C in BS:
        for x, y, z in C:
            assert (x, y, z) in SEEN

    for x, y, z in B:
        SEEN.discard((x, y, z))

    FALL = set()
    while True:
        any_ = False
        for j, C in enumerate(BS):
            if j == i:
                continue
            ok = True
            for x, y, z in C:
                if z == 1:
                    ok = False
                if (x, y, z - 1) in SEEN and (x, y, z - 1) not in C:
                    ok = False
            if ok:
                FALL.add(j)
                for x, y, z in C:
                    assert (x, y, z) in SEEN
                    SEEN.discard((x, y, z))
                    SEEN.add((x, y, z - 1))
                BS[j] = [(x, y, z - 1) for (x, y, z) in C]
                any_ = True
        if not any_:
            break
    if len(FALL) == 0:
        p1 += 1
    p2 += len(FALL)
print(p1)
print(p2)