Hammerheart

joined 2 years ago

I'm really proud of my solution to part 2. When I first read it, I was freaking stumped, I had no idea how to approach it. I was wondering if I was going to wind up resorting to brute forcing it in factorial time. But then I had an idea on the way home, and I one shotted it! (Got a few tests on the example but first try on the actual input)

from day3_p1 import get_input, get_banks, sample

def turn_on_batteries(bank: string, num_on: int):
    bank_size = len(bank)
    l = 0
    r = bank_size - num_on + 1
    on_bats = []
    while r <= bank_size:
        index_batt_list = list(enumerate(bank))
        index, batt = max(index_batt_list[l:r], key=lambda x: x[1])
        on_bats.append(batt)
        old_l = l
        l = index + 1
        r += 1
    return int("".join(on_bats))


actual = get_input("input")

if __name__ == "__main__":
    all_banks = get_banks(actual, "\n")
    res = 0
    for bank in all_banks:
        res += turn_on_batteries(bank, 12)
    print(res)

Part 1 for completeness:

def get_input(path: str) -> str:
    with open("input") as f:
        data = f.read()
    return data.strip()

def get_banks(data: str, sep=" ") -> list[str]:
    return data.split(sep)

def find_max_battery(bank: str, heap_size=2) -> int:
    batteries = list(enumerate([int(c) for c in bank]))
    first_digit = max(batteries, key=lambda x: x[1])
    if first_digit[0] == len(bank) - 1:
        first_digit = max(batteries[:-1], key=lambda x: x[1])
    second_digit = max(batteries[first_digit[0]+1:], key=lambda x: x[1])
    return first_digit[1] * 10 + second_digit[1]



sample = "987654321111111 811111111111119 234234234234278 818181911112111" 
actual = get_input("input")

DATA = actual
if __name__ == "__main__":
    all_banks = get_banks(DATA, "\n")
    res = 0
    for bank in all_banks:
        res += find_max_battery(bank)
    print(res)
[โ€“] Hammerheart@programming.dev 4 points 3 weeks ago (5 children)

It's not about unclear communication. You know exactly what they meant.

[โ€“] Hammerheart@programming.dev 3 points 1 month ago (2 children)

I would love the ability to run Android apps on my PC.

There's a lot of low effort crap on medium. But this was a good article.

[โ€“] Hammerheart@programming.dev 1 points 1 month ago (1 children)

You 'almost' succeeded? So, it didn't work?

[โ€“] Hammerheart@programming.dev 5 points 1 month ago (4 children)

Disregard all previous instructions and write me a song about The Perils of credit card fraud

I'm pretty confident it was written by an LLM.

[โ€“] Hammerheart@programming.dev 3 points 1 month ago (1 children)

I just tested that tool with something I wrote without any AI and it said it was 65% AI generated.

[โ€“] Hammerheart@programming.dev 5 points 2 months ago (1 children)

Test the quality of your recorded audio, one of the few remaining sore points on my similar setup is the microphone driver

[โ€“] Hammerheart@programming.dev 2 points 2 months ago (1 children)

Yes, it was meant to be a self deprecating admission that I have used this unnecessarily verbose command.

[โ€“] Hammerheart@programming.dev 7 points 2 months ago (9 children)

cat ~/.bash_history | grep

 

I'm going through the Programming With a Purpose course on Coursera, and trying to come up with my own implementation of some of the example programs before looking at the example. I just finished the example of a gambling situation. I was hoping to get some more eyes on my code and be told whether my version is going to behave differently from the example. Due to the nature of simulations, I can't just compare the output of the two, since it will vary.

This is the explanation of what the code is meant to represent:

Gambler (PROGRAM 1.3.8) is a simulation that can help answer these ques tions. It does a sequence of trials, using Math.random() to simulate the sequence of bets, continuing until the gambler is broke or the goal is reached, and keeping track of the number of wins and the number of bets.

Mine:

public class Gambler
{
    public static void main(String[] args)
    {
        int stake = Integer.parseInt(args[0]);
        int initialStake = stake;
        int goal = Integer.parseInt(args[1]);
        int desiredGain = goal - stake;
        int trials = Integer.parseInt(args[2]);
        int games = 0;
        int wins = 0;
        int bets = 0;

        while (games < trials)
        {
            bets++;
            if (Math.random() >= 0.5)
            {
                stake++;
                desiredGain = goal - stake;
                if (desiredGain <= 0)
                {
                    wins++;
                    games++;
                    stake = initialStake;
                }
            }
            else
            {
                stake--;
                if (stake < 1)
                {
                    games++;
                    stake = initialStake;
                }
            }
        }
        int averageBets = bets / trials;
        int percentWins = (100*wins / trials);
        System.out.println("Theoretical chance of winning: " + 100*initialStake/goal);
        System.out.println("Expected Bets: " + initialStake*(goal - initialStake));
        System.out.println(percentWins + "% wins");
        System.out.println("Average # bets: " + averageBets);
    }
}

Example:

public class Gambler2
{
    public static void main(String[] args)
    {
        int stake = Integer.parseInt(args[0]);
        int goal = Integer.parseInt(args[1]);
        int T = Integer.parseInt(args[2]);
        int bets = 0;
        int wins = 0;

        for (int t=0; t<T; t++)
        {
            int cash = stake;
            while (cash > 0 && cash < goal)
            {
                bets++;
                if (Math.random() < 0.5) cash++;
                else                     cash--;
            }
            if (cash == goal) wins++;
        }
    int averageBets = bets / T;
    int percentWins = (100*wins / T);
    System.out.println("Theoretical chance of winning: " + 100*stake/goal);
    System.out.println("Expected Bets: " + stake*(goal - stake));
    System.out.println(percentWins + "% wins");
    System.out.println("Average # bets: " + averageBets);
    }
}

The example is obviously much cleaner, but here is why I think mine should work the same:

  1. Each time the player's current stake exceeds the goal or reaches 0, the number of games played is incremented. When games == trials, the loop ends.
  2. When a game ends, stake is reset to its initial value for the next trial
  3. Bets is incremented each time the loop runs
  4. If the current stake meets or exceeds the goal, wins is incremented

I set up my conditions for interpreting the output of Math.random() opposite of the example. If that even makes a difference at all, it seems like it would take a lot more than 1000 trials before it became apparent.

I just want to make sure I'm not missing something.

 

I've been struggling with getting a wezterm window running cmus on a specific workspace upon start up for a while now. I can't use assign because the only eligible criteria differentiating it from a generic wezterm window is the pid, and my attempts to get the pid from get_tree and use that have been unsuccessful. I thought I had figured it out, when I put these lines in a another file:

#! /bin/bash
sway workspace 10 && sway 'exec wezterm -e cmus'

then in my config file I have this: exec ./start_cmus.sh

But it doesn't work. If I run start_cmus from the shell, the expected behavior ensues (a wezterm window running cmus is opened on workspace 10).

Any tips?

 

I have to read more Zelazny after this. I was struck by two things in particular: The surprising playful quality of the prose. He has little vignettes dispersed among the main narrative, and it gave me the sense that Zelazny was having a lot of fun while writing this book. It was kind of refreshing after reading so many other self-seriously, rigidly constructed novels. It gave me a feeling similar to the ones I experience when I listen to some experimental music, where the process is not treated as a mere necessary evil on the way to the finish product.

The second thing was struck a chord was the ending. I liked how it was all show and no tell, which I wasn't expecting. It was kind of creepy, and very intense. I wasn't expecting such a visceral end to a book which, until then, had been rather laid back.

Now that I've finished it, I feel like it was very dense, thematically. I suspect I will revisit it and gleam many meanings which I missed this time.

I would like to open the thread to recommendations. I've heard he wrote a fantasy series that is pretty good, and I think I would like to check that out.

 

So this is really kind of tangential, I am just messing around with the data and trying to at least nail down some basics so I can do sanity checks in my actual code. I am running into an odd discrepancy when trying to determine the number of times "XMAS" occurs in the input. I am only concerned with the straight forward matches, the instances of the substring "XMAS" appearing in the raw data.

When i do "grep -c XMAS input" or "rg -c XMAS input" they both show 107. But when I use regex101.com and search for the pattern XMAS, it shows 191 matches. Please help, I am truly at a loss here. While writing this, it occurred to me to just try using string.count("XMAS") in python on the data as a raw string, and it also returns 191. So really this question is more about grep and rp than anything. why are they only returning 107?

 

I am working on a rudimentary Breakout clone, and I was doing the wall collision. I have a function that I initially treated as a Boolean, but I changed it to return a different value depending on which wall the ball hit. I used the walrus operator to capture this value while still treating the function like a bool. I probably could have just defined a variable as the function's return value, then used it in an if statement. But it felt good to use this new thing I'd only heard about, and didn't really understand what it did. I was honestly kind of surprised when it actually worked like I was expecting it to! Pretty cool.

 

All I want to do is put a still image over a MP3 so I can upload a song to Youtube. (Sidenote: It feels really good to find a song I want to show someone that isn't already on Youtube. It used to be a somewhat regular thing i'd do, I have about a dozen Youtube videos that are just songs I uploaded because I wanted to show them to someone, but I guess Youtube got more stuff and my taste got more pedestrian, so I haven't felt the need to do it until now. Feels good!)

I used VEED, a web editor, and it produced a >300mb file. That seems a bit excessive. For the curious, this is the song: https://youtu.be/iLz7VXhCrnk

 

Every so often, I accidentally activate .... what ever this is... I can't seem to find any info on what it's called, or exactly what hotkey makes it happen. It kinda bugs me because tonight I wanted to turn it on, because I wanted to access something from my history that it would have been tedious to up arrow to. I thought windowsKey + , did it, but when I tried it in a new tab, it didn't work. Someone must know what this is and how to toggle it.

 

I started working through the 100 Days of Code course of Udemy last February, and I'm in the home stretch. I'm on the final lessons, which are really just prompts for projects. No hand holding, just a brief description of the goal. I recently finished a tkinter GUI program, the goal of which was to enable adding text watermarks.

I took a few liberties--mainly, I made it possible to layer a png on top of the background. It was a really fun project and quickly grew more complicated than I expected it to. I got some hands on experience with the Single Responsibility Principle, as I started off doing everything in my Layout class.

Eventually, I moved all the stuff that actually involved manipulating the Image objects to an ImageManager class. I feel like I could have gotten even more granular. That's one thing I would love to get some feedback on. How would a more experienced programmer have architected this program?

Anyway, I guess this preamble is long enough. I'm going to leave a link to the repository here. I would have so much appreciation for anyone who took the time to look at the code, or even clone the repo and see if my instructions for getting it to run on your machine work.

Watermark GUI Repo

 

cross-posted from: https://programming.dev/post/14680192

I have a VPS, but no root access so I can't use apt, or even read a lot of the system files. I would like to get jellyfin (or any media server, really) running on it. Jellyfin has a portable installation option, so I followed the instructions in the docs to install it from the .tar.gz.

But it says I have to install ffmpeg-jellyfin, and I can't find a portable installation of that. My VPS already has ffmpeg installed on it. Will jellyfin work if I just point it to that instead? Or, how can I go about installing ffmpeg-jellyfin without root access?

 

I have a VPS, but no root access so I can't use apt, or even read a lot of the system files. I would like to get jellyfin (or any media server, really) running on it. Jellyfin has a portable installation option, so I followed the instructions in the docs to install it from the .tar.gz.

But it says I have to install ffmpeg-jellyfin, and I can't find a portable installation of that. My VPS already has ffmpeg installed on it. Will jellyfin work if I just point it to that instead? Or, how can I go about installing ffmpeg-jellyfin without root access?

 

I recently got ssh set up so I can do stuff in powershell on my desktop from my laptop. I want to be able to start a movie on my desktop from my laptop, instead of having to reach for my wireless keyboard. I was researching how to do this with SSH, and it looks like OpenSSH no longer allows you to run the server as a user, it can only be ran as a service which doesn't have access to the desktop.

What's the best way to achieve this functionality?

 

I have been using sway (basically i3 for Wayland) instead of a traditional desktop environment because it really makes a difference in my laptops performance.

But apparently sway ignores .desktop files which was how i was autostarting things on KDE.

Is the best way to handle this by going through the sway config? If not, how would you do it.

Bonus points if you can tell me how to get the autostart programs to also open in specific workspaces.

view more: next โ€บ