aboutsummaryrefslogtreecommitdiffstats
path: root/src/20/solve.py
blob: 63695b7ddbf7cbd3f550199c177a1c565402874e (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
149
150
151
import sys
sys.path.append("../common")
import aoc
from copy import deepcopy

data = list(aoc.data)

def get_map(p):
    return vmap[p[1] + spos[1]][p[0] + spos[0]]

def set_map(p, c):
    global vmap, spos
    vmap[p[1] + spos[1]][p[0] + spos[0]] = c

def new_pos(p, c):
    p = p[:]
    if c == "N":
        p[1] -= 2
    elif c == "S":
        p[1] += 2
    elif c == "W":
        p[0] -= 2
    elif c == "E":
        p[0] += 2
    return p

def calc_pos(stack):
    p = [0,0]
    for c in stack:
        p = new_pos(p, c)
    return p

xmin = 0
xmax = 0
ymin = 0
ymax = 0

def check_size(stack):
    global xmin, xmax, ymin, ymax

    p = calc_pos(stack)
    if p[0] < xmin:
        xmin = p[0]
    if p[0] > xmax:
        xmax = p[0]
    if p[1] < ymin:
        ymin = p[1]
    if p[1] > ymax:
        ymax = p[1]

def draw_route(stack):
    p = calc_pos(stack)
    set_map(p, ".")
    np = new_pos(p, stack[-1])
    cp = [0,0]
    cp[0] = p[0] + int((p[0] - np[0])/2)
    cp[1] = p[1] + int((p[1] - np[1])/2)
    set_map(cp, "+")

def iter_regex(func):
    stacklens = [0]
    stack = []
    for i in range(1, len(data)-1):
        c = data[i]
        if c == "(":
            stacklens.append(0)
        elif c == "|":
            for i in range(stacklens[-1]):
                stack.pop()
            stacklens[-1] = 0
        elif c == ")":
            for i in range(stacklens[-1]):
                stack.pop()
            stacklens.pop()
        else:
            stack.append(c)
            stacklens[-1] += 1
            func(stack)

def fwriteMap():
    f = open("output", "w+")
    for y in range(len(vmap)):
        f.write("".join([str(v) for v in vmap[y]]) + "\n")
    f.close()

mshape = None
spos = None
vmap = None

def genMap():
    global vmap, mshape, spos

    iter_regex(check_size)
    xdif = xmax - xmin + 2
    ydif = ymax - ymin + 2

    spos = (-xmin+1, -ymin+1)
    mshape = (xdif, ydif)

    vmap = [["#" for x in range(xdif+1)] for y in range(ydif+1)]
    vmap[spos[1]][spos[0]] = "X"

    iter_regex(draw_route)


adjacent = ((-1, 0), (0, -1), (1, 0), (0, 1))

def gen_countmap(sp):
    countmap = dict()
    next = dict()
    next[(sp[0], sp[1])] = 0
    counter = 0
    steps = list()
    while len(next) > 0 and len(steps) == 0: # first steps available will be shortest
        countmap = {**countmap, **next} # merge dictionaries
        counter += 1
        temp = dict()
        for n in next:
            for dir in adjacent:
                nx = n[0]+dir[0]
                ny = n[1]+dir[1]
                if get_map((nx, ny)) != "#" and (nx, ny) not in countmap and (nx, ny) not in temp:
                    temp[(nx,ny)] = counter
        next = temp
    return countmap

def next_step(cmap, p):
    # adjacent squares
    npos = [[p[0] + dir[0], p[1] + dir[1]] for dir in adjacent]

    # steps and dist
    steps = [[np[0], np[1], cmap[np[0], np[1]]] for np in npos if (np[0], np[1]) in cmap]

    if len(steps) == 0:
        return None
    else:
        return sorted(steps, key = lambda x: x[2])[0] #closest

genMap()

def solve1(args):
    cmap = gen_countmap((0,0))
    ipos = sorted(cmap, key = lambda x : cmap[x])[-1]
    return int(cmap[ipos]/2)

def solve2(args):
    cmap = gen_countmap((0,0))
    count = len([v for v in cmap if int(cmap[v]/2) >= 1000 and get_map(v) == "."])
    return count

aoc.run(solve1, solve2, sols=[4121, 8636])