aboutsummaryrefslogtreecommitdiffstats
path: root/src/13/solve.py
blob: e89466cce2005e7ba2455e01c0e8f88567dfbc0a (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import sys
sys.path.append("../common")
import aoc

tmap = [list(l) for l in aoc.data.split("\n") if len(l) != 0]
xlen, ylen = len(tmap[0]), len(tmap)

arrows = dict()
arrows["<"] = (-1, 0)
arrows[">"] = (1, 0)
arrows["^"] = (0, -1)
arrows["v"] = (0, 1)

directions = [(-1,0), (0,-1), (1, 0), (0, 1)] # l - u - r - d

def check_pos(x, y):
    if x < xlen and x >= 0 and y < ylen and y >= 0:
        return tmap[y][x]
    return " "

def correct(x, y):
    cr = check_pos(x+1, y)
    cl = check_pos(x-1, y)
    cu = check_pos(x, y-1)
    cd = check_pos(x, y+1)

    if cr == "|": cr = " "
    if cl == "|": cl = " "
    if cu == "-": cu = " "
    if cd == "-": cd = " "

    r = cr != " "
    l = cl != " "
    u = cu != " "
    d = cd != " "

    vsum = r + l + u + d
    if vsum > 2:
        return "+"

    # 2 around
    if r:
        if l:
            return "-"
        elif u:
            return "/"
        else:
            return "\\"
    else:
        if not l:
            return "|"
        elif u:
            return "/"
        else:
            return "\\"

carts = list()
for y in range(ylen):
    for x in range(xlen):
        if tmap[y][x] in arrows:
            carts.append([x, y, arrows[tmap[y][x]], 0, 0])
            tmap[y][x] = correct(x,y)

def cart_at(x, y):
    global carts
    for i in range(len(carts)):
        c = carts[i]
        if c[0] == x and c[1] == y and not c[4]:
            return i
    return None

def draw_map():
    for y in range(ylen):
        f = lambda x : "".join(["#" if cart_at(x,y) != None else tmap[y][x]])
        aoc.debug([f(x) for x in range(len(tmap[y]))])

def advance(cart):
    ncart = [0 for x in range(5)]
    ncart[0] = cart[0]+cart[2][0]
    ncart[1] = cart[1]+cart[2][1]
    col = cart_at(ncart[0], ncart[1])
    if col != None:
        return ncart[0], ncart[1], col

    c = tmap[ncart[1]][ncart[0]]
    d = directions.index(cart[2])

    if c == "+":
        d = (d + len(directions)-1 + cart[3]) % len(directions)
        ncart[2] = directions[d]
        ncart[3] = (cart[3] + 1) % 3
    else: # dont need to change direction for '-' and '|'
        if c == "/":
            ncart[2] = directions[3-d]
        elif c == "\\":
            if d == 0: #l
                ncart[2] = directions[1]
            elif d == 1: #u
                ncart[2] = directions[0]
            elif d == 2: #r
                ncart[2] = directions[3]
            elif d == 3: #d
                ncart[2] = directions[2]
        else:
            ncart[2] = cart[2]
        ncart[3] = cart[3]
    return ncart


def solve1(args):
    global carts

    crash = None
    while not crash:
        carts = sorted(carts, key=lambda x:(x[0], x[1]))
        for c in range(len(carts)):
            nc = advance(carts[c])
            if len(nc) == 3:
                crash = nc
                break
            else:
                carts[c] = nc

    return f"{crash[0]},{crash[1]}"

def solve2(args):
    global carts

    while len(carts) > 1:
        carts = sorted(carts, key=lambda x:(x[0], x[1]))
        rcarts = list()
        for c in range(len(carts)):
            if carts[c][4]: continue;
            nc = advance(carts[c])
            if len(nc) == 3:
                carts[c][4] = 1
                carts[nc[2]][4] = 1
                rcarts.append(carts[c])
                rcarts.append(carts[nc[2]])
            else:
                carts[c] = nc
        for c in rcarts:
            if c in carts: # prevent doubles
                carts.remove(c)

    return f"{carts[0][0]},{carts[0][1]}"

aoc.run(solve1, solve2)