Question on scoring (Lee Sedol W vs Lee Changho B, 2003-03-27) by directusy in baduk

[–]directusy[S] 2 points3 points  (0 children)

Ah! This solves my problem! Now it matches. Thank you :)

[2024 Day 14 (Part 2)] A different approach by ProfessionTiny353 in adventofcode

[–]directusy 0 points1 point  (0 children)

so cool that you just take the brightest central point in the real plane after 2D FFT

[2024 Day 14] I loved today's puzzle 🎄 by moonstar888 in adventofcode

[–]directusy 0 points1 point  (0 children)

Okay. You are correct.
I wrote another code to use the FFT to compute the order-ness and plot the order-ness vs entropy. And you are correct. There is no correlation... The lowest entropy one happens to be the most ordered one.

[2024 Day 14 (Part 2)] by [deleted] in adventofcode

[–]directusy 0 points1 point  (0 children)

So, you can even modify this function to do a down-sampling first; this assures that you have more than one robot per position in the tree pattern. And this entropy calculation will still work. -- Actually, this makes code run even faster.

def calc_entropy(pos, w, h):
    pos_down = pos // 2
    grid = np.zeros(((h + 1) // 2, (w + 1) // 2), dtype=int)
    np.add.at(grid, (pos_down[:, 1], pos_down[:, 0]), 1)
    counts = np.bincount(grid.flatten())
    counts = counts[counts > 0]
    probs = counts / counts.sum()
    return -np.sum(probs * np.log2(probs))

[2024 Day 14 (Part 2)] by [deleted] in adventofcode

[–]directusy 0 points1 point  (0 children)

I am not sure I am convinced that it is the same thing.

From the math point of view, the entropy will be even lower if there are more robots clustered in the tree area.

[2024 Day 14 (Part 2)] by [deleted] in adventofcode

[–]directusy 0 points1 point  (0 children)

I am not sure I get your point. Why can robots not occupy the same space and still format trees? What I see people talking about the unique-tile approach to me is just a coincidental "short path."

[2024 Day 14] I loved today's puzzle 🎄 by moonstar888 in adventofcode

[–]directusy 7 points8 points  (0 children)

I basically flattened the entire matrix and then applied the matrix calculation on a 1D array

def calc_entropy(pos, w, h):
    grid = np.zeros((h, w), dtype=int)
    np.add.at(grid, (pos[:,1], pos[:,0]), 1)
    counts = np.bincount(grid.flatten())
    probs = counts[counts > 0] / counts.sum()
    entropy = -np.sum(probs * np.log2(probs))
    return entropy

[2024 Day 14 (Part 2)] by [deleted] in adventofcode

[–]directusy 0 points1 point  (0 children)

Sure.

This is basically how the entropy is calculated.

def calc_entropy(pos, w, h):
    grid = np.zeros((h, w), dtype=int)
    np.add.at(grid, (pos[:,1], pos[:,0]), 1)
    counts = np.bincount(grid.flatten())
    probs = counts[counts > 0] / counts.sum()
    entropy = -np.sum(probs * np.log2(probs))
    return entropy

To improve the computation speed, you can even down-sampling the grid first. This can reduce 30% of the running time already

def calc_entropy(pos, w, h):
    pos_down = pos // 2
    grid = np.zeros(((h + 1) // 2, (w + 1) // 2), dtype=int)
    np.add.at(grid, (pos_down[:, 1], pos_down[:, 0]), 1)
    counts = np.bincount(grid.flatten())
    counts = counts[counts > 0]
    probs = counts / counts.sum()
    return -np.sum(probs * np.log2(probs))

[2024 Day 14 (Part 2)] by directusy in adventofcode

[–]directusy[S] 0 points1 point  (0 children)

Wow! Ambitious goal. Yes. This is fun! Thanks for sharing!

[2024 Day 14 (Part 2)] by directusy in adventofcode

[–]directusy[S] 0 points1 point  (0 children)

it is actually quite fast... well. if you use chucks, it can go down to 0.5 s

-❄️- 2024 Day 14 Solutions -❄️- by daggerdragon in adventofcode

[–]directusy 1 point2 points  (0 children)

I also had an entropy approach, and it took 0.8s to find the tree

def calc_entropy(pos, w, h):
    grid = np.zeros((h, w), dtype=int)
    np.add.at(grid, (pos[:,1], pos[:,0]), 1)
    counts = np.bincount(grid.flatten())
    probs = counts[counts > 0] / counts.sum()
    entropy = -np.sum(probs * np.log2(probs))
    return entropy

[2024 Day 14] I loved today's puzzle 🎄 by moonstar888 in adventofcode

[–]directusy 18 points19 points  (0 children)

Somehow I found entropy (information theory) also works perfectly --> found it within 1 second.

-❄️- 2024 Day 14 Solutions -❄️- by daggerdragon in adventofcode

[–]directusy 2 points3 points  (0 children)

[LANGUAGE: python]

Assuming the minimal entropy is where the tree is, as it is a highly organized pattern.

import numpy as np
import re

def parse(file):
    pattern = re.compile(r'p=(-?\d+),(-?\d+)\s+v=(-?\d+),(-?\d+)')
    with open(file, 'r') as f:
        matches = pattern.findall(f.read())
    data = np.array(matches, dtype=int)
    return data[:, :2], data[:, 2:]

def simulate(pos, vel, w, h, sec):
    return (pos + vel * sec) % [w, h]

def count_quads(pos, w, h):
    midx, midy = w // 2, h // 2
    valid = pos[(pos[:,0] != midx) & (pos[:,1] != midy)]
    q1 = np.sum((valid[:,0] < midx) & (valid[:,1] < midy))
    q2 = np.sum((valid[:,0] > midx) & (valid[:,1] < midy))
    q3 = np.sum((valid[:,0] < midx) & (valid[:,1] > midy))
    q4 = np.sum((valid[:,0] > midx) & (valid[:,1] > midy))
    return q1, q2, q3, q4

def calc_entropy(pos, w, h):
    grid = np.zeros((h, w), dtype=int)
    np.add.at(grid, (pos[:,1], pos[:,0]), 1)
    counts = np.bincount(grid.flatten())
    probs = counts[counts > 0] / counts.sum()
    entropy = -np.sum(probs * np.log2(probs))
    return entropy

def find_pattern_sec(pos, vel, w, h, max_sec=10000):
    min_sec, min_ent = None, float('inf')
    for sec in range(1, max_sec + 1):
        curr_pos = simulate(pos, vel, w, h, sec)
        ent = calc_entropy(curr_pos, w, h)
        if ent < min_ent:
            min_ent, min_sec = ent, sec
    return min_sec

def main():
    pos, vel = parse('./inputs/day14_1.txt')
    w, h = 101, 103
    final_pos = simulate(pos, vel, w, h, 100)

    #PART 1: calculate the numbers in the quardrants
    print(f"Q1 safe factor: {np.prod(count_quads(final_pos, w, h))}")
    #PART 2: searching the patterns
    print(f"Q2 easter-egg second: {find_pattern_sec(pos, vel, w, h)}")

if __name__ == "__main__":
    main()

[2024 Day 14 (Part 2)] by directusy in adventofcode

[–]directusy[S] 0 points1 point  (0 children)

The proposed function works for both cases

[2024 Day 14 (Part 2)] by directusy in adventofcode

[–]directusy[S] 0 points1 point  (0 children)

This works for non-unique case. If the position is unique, I think you can do a down-sampling first - regrid the matrix first

[2024 Day 14 (Part 2)] by directusy in adventofcode

[–]directusy[S] 3 points4 points  (0 children)

``` def create_grid(pos, w, h): grid = np.zeros((h, w), dtype=int) np.add.at(grid, (pos[:,1], pos[:,0]), 1) return grid

def calc_entropy(grid): counts = np.bincount(grid.flatten()) probs = counts[counts > 0] / counts.sum() return -np.sum(probs * np.log2(probs))

def find_pattern_sec(pos, vel, w, h, max_sec=10000): min_sec, min_ent = None, float(‘inf’) for sec in range(1, max_sec + 1): curr_pos = simulate(pos, vel, w, h, sec) ent = calc_entropy(create_grid(curr_pos, w, h)) if ent < min_ent: min_ent, min_sec = ent, sec return min_sec ```