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.

I thought I was making too many copies but the input's size wasn't so bad and ultimately it takes ~500ms to run when compiled with O3. Another victory for bruteforce-chads

![](/images/1671825042173174.webp)

![](/images/16718250396541977.webp)

Jump in the discussion.

No email address required.

  .-""-.
 /,..___\
() {_____}
  (/-@-@-\)
  {`-=^=-'}
  {  `-'  } Merry Fistmas!
   {     }
    `---'
Jump in the discussion.

No email address required.

thanks king

Jump in the discussion.

No email address required.

Fun, takes about 10 sec to run

f = open('AOC2022Day23.txt')
lines = f.read().strip().split('\n')
elves = set()
#grid = np.zeros((len(lines)+20,len(lines[0])+20))
for y,line in enumerate(lines):
    for x,c in enumerate(line):
        if c == '#':
            elves.add((x,y))
movement = True
for d in range(1000):
    if movement == False:
        print(d)
        break
    movement = False
    proposals = {}
    for (x,y) in elves:
        n = x,y-1
        ne = x+1,y-1
        nw = x-1,y-1
        e = x+1,y
        w = x-1,y
        se = x+1,y+1
        sw = x-1,y+1
        s = x,y+1
        if all([i not in elves for i in [n,ne,nw,e,w,se,sw,s]]):
            continue
        dirs = [[n,ne,nw],[s,se,sw],[w,nw,sw],[e,ne,se]]
        if all([i not in elves for i in dirs[d%4]]):
            pdir = dirs[d%4][0]
            if pdir not in proposals:
                proposals[pdir] = [(x,y)]
            else:
                proposals[pdir].append((x,y))
        elif all([i not in elves for i in dirs[(d+1)%4]]):
            pdir = dirs[(d+1)%4][0]
            if pdir not in proposals:
                proposals[pdir] = [(x,y)]
            else:
                proposals[pdir].append((x,y))
        elif all([i not in elves for i in dirs[(d+2)%4]]):
            pdir = dirs[(d+2)%4][0]
            if pdir not in proposals:
                proposals[pdir] = [(x,y)]
            else:
                proposals[pdir].append((x,y))
        elif all([i not in elves for i in dirs[(d+3)%4]]):
            pdir = dirs[(d+3)%4][0]
            if pdir not in proposals:
                proposals[pdir] = [(x,y)]
            else:
                proposals[pdir].append((x,y))
    for prop in proposals:
        if len(proposals[prop]) == 1:
            movement = True
            elves.add(prop)
            elves.discard(proposals[prop][0])
xs = []
ys = []
for elf in elves:
    xs.append(elf[0])
    ys.append(elf[1])
xsize = max(xs)-min(xs)+1
ysize = max(ys)-min(ys)+1
print(xsize*ysize-len(elves))
Jump in the discussion.

No email address required.

😴😴😴

Jump in the discussion.

No email address required.

13 days behind currently, it's not looking good for me this year.

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.

One day behind currently :platynooo:

Jump in the discussion.

No email address required.

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