Unable to load image

Day 23 AoC: :marseymerchantelf: Unstable Diffusion :!marseymerchantelf:

postmaxx your solutions in here. @Platybells

19
Jump in the discussion.

No email address required.

Horrifically inefficient, but it works. I thought programming the elf positions as just co-ordinates rather than maintaining a matrix structure would've scaled better for the inevitable "Part 2: now run it for a billion turns" surprise, but I guess not since the grid space never really reaches ludicrous lengths. Takes roughly 10 minutes.

foo = []
with open('day23_input.txt', 'r') as inp:
    foo = inp.read().split('\n')

elves = []
for f in range(len(foo)):
    for g in range(len(foo[f])):
        if foo[f][g] == '#':
            elves.append([f, g])

surround_check = [[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1]]
pos_check = [[0,1,7],[4,3,5],[6,5,7],[2,3,1]]

prev_state, turns = None, 0
while(elves != prev_state):
    prev_state = elves.copy()
    candidates = [None for i in range(len(elves))]
    for e in range(len(elves)):
        surround = [[elves[e][0] + surround_check[i][0], elves[e][1] + surround_check[i][1]] in elves for i in range(8)]
        if sum(surround) == 0:
            continue
        check = turns%4
        for x in range(4):
            if not (surround[pos_check[check][0]] or surround[pos_check[check][1]] or surround[pos_check[check][2]]):
                candidates[e] = [elves[e][0] + surround_check[pos_check[check][0]][0], elves[e][1] + surround_check[pos_check[check][0]][1]]
                break
            check = (check+1)%4
    for c in range(len(candidates)):
        if candidates[c] != None:
            flag = False
            for d in range(c):
                if candidates[d] == candidates[c]:
                    flag = True
                    candidates[d] = None
            for d in range(c+1, len(candidates)):
                if candidates[d] == candidates[c]:
                    flag = True
                    candidates[d] = None
            if not flag:
                elves[c] = candidates[c]   
    turns += 1  

max_y, min_y, max_x, min_x = max([i[0] for i in elves]), min([i[0] for i in elves]), max([i[1] for i in elves]), min([i[1] for i in elves])
empty = (max_y-min_y+1)*(max_x-min_x+1)
for y in range(min_y, max_y+1):
    line = '.' * (max_x - min_x + 1)
    for e in elves:
        if e[0] == y:
            empty -= 1
            line = line[:e[1]-min_x] + '#' + line[e[1]-min_x+1:]
    print(line)
print(empty, turns)
Jump in the discussion.

No email address required.

You sat down and wrote all this shit. You could have done so many other things with your life. What happened to your life that made you decide writing novels of bullshit here was the best option?

Jump in the discussion.

No email address required.

I have been studying programming for 3 days straight now and idk what the frick anything you wrote down means.

Okay wait I understand like half of it why do you have functions named elves and candidates and dumb shit like that.

Jump in the discussion.

No email address required.



Now playing: Steel Drum Rhumba (DKC2).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.