remix logo

Hacker Remix

Bad Apple but it's 6,500 regexes that I search for in Vim

598 points by vortex_ape 7 days ago | 61 comments

adityaathalye 7 days ago

Hah, trust nolen to 1,000x something :))) I have used similar tactics in the past, but separately and definitely not in one day! For the interested:

- Bad Matrix (tput blocks to the terminal): https://www.evalapply.org/posts/bad-matrix/

- Animating Text Art in Javascript (print text into fixed grid, flipbook-style): https://www.evalapply.org/posts/animate-text-art-javascript/...

- oxo (format and print tic-tac-toe board to terminal, so I can regex-match for win/loss/draw results): https://github.com/adityaathalye/oxo/blob/7681e75edaeec5aa1f...

But, I mean, that Bad Apple takes the cake!

(edit: add missing link)

DiggyJohnson 6 days ago

Ahaha `oxo` is an unhinged project to actually execute. Thanks for sharing all of these projects.

Brb forking and integrating ascii-text third party ads

adityaathalye 5 days ago

Thank you for the kind appreciation. ascii-text third party ads adds a whole commercial dimension... Now, to make it really 21st century, someone can replace the "randumb" computer player with an LLM adversary :)

jordigh 7 days ago

The tech demo that really made me fall in love with Bad Apple was getting it to run on the NES.

https://somethingnerdy.com/downloads/

Here it is running from my Everdrive.

https://inversethought.com/jordi/video/badapple.mp4

Yes, with full audio. It's about one gigabyte of data. On a system where the typical game size is no more than a couple hundred kilobytes, and your CPU only has three 8-bit registers for you to do any calculation with.

junon 6 days ago

Very cool. Having done a bit of NES dev I can imagine this wasn't super straightforward to make performant for the graphics, given you can typically only have a few sprites on a row before the NES starts to 'dissolve' them (not sure the term).

I wonder if it's using the background tile map for this instead of sprites, though that's also an impressive amount of graphics bandwidth.

> with full audio playback rate (44.2kHz)

The audio being so clear is also impressive, is that something that the card extends? IIRC the PCM channel on the NES isn't anywhere near that bitrate, and is also 8-bit sample size.

Dylan16807 6 days ago

The channel can either play back delta-modulation samples from memory, or you can directly set the output as a 7 bit value.

So by burning a lot of CPU cycles, you can keep up a perfectly good sample rate using the latter method.

godd2 5 days ago

The bitrate of the PCM is determined based on how quickly you can write a byte to the register. The fastest you could write general data is once every 6 cycles, which gives ~298 MHz of sample rate, so 44.2 kHz is easily doable if that's all you want to do with the CPU.

junon 4 days ago

Do you mean 298 KHz? I thought the 6502 on the NES was slightly over 1MHz

godd2 5 days ago

> I wonder if it's using the background tile map for this instead of sprites

Yes, it's all background tiles being loaded continuously from the SD card. We created the tiles with a custom tile de-maker.

junon 4 days ago

Very cool, thanks for the info :)

Wii2 6 days ago

Depending on what aspects of that you enjoyed, you might also enjoy this similar Bad Apple implementation on the NES. With the extra challenge of it being done through ACE in Super Mario Bros. and all data being streamed in through the controller. https://www.youtube.com/watch?v=lfG8DbxFibY

They also made an accompanying breakdown video. https://www.youtube.com/watch?v=Wa0u1CjGtEQ

eieio 6 days ago

This is really neat - did you do any kind of writeup on it? I’d love to read it if you did.

jordigh 6 days ago

It's not my work. I'm just a script kitty.

There are some details in the description and comments here.

https://www.youtube.com/watch?v=WPUYjDNks9Y

panzi 6 days ago

That is glorious!

rav 7 days ago

Regarding the Vim macro that ends by going to the next line to be "replayable": You can also use the following command to run the macro once per line:

        :%norm @q

eieio 7 days ago

oh wow, TIL, I'm pretty surprised I didn't know this trick!

back when I was vim golfing the normal solution was to make the macro recursive. So you'd record your macro, and you'd end it with '+@q' (move to next line and run the macro again). Then you run the macro once and it runs over every line.

This ends up being really efficient in terms of keystrokes but in practice I think it's hard to think about and not very ergonomic, so I don't end up using it much. But it's a fun trick for golfing.

rav 7 days ago

There's also the no-macro solution where you just use ":%norm [series of keystrokes]" to run the given keystrokes on each line, but that comes with the added difficulty of not giving any visual feedback of what the keystrokes will do before you submit the entire line.

One thing to keep in mind is that ":%norm" will place the cursor at the start of each line, before any indentation, whereas the trick of ending the macro with "+" will place the cursor at the start of each line after the indentation. But this can be worked around with ":%norm ^@q", using ^ to skip indentation before running macro q on each line.

Izkata 6 days ago

Related to that, macros are just recorded into normal registers. You can get it out with:

  "qp
Edit it, and put it back into the register with

  "qdd

rav 5 days ago

Heads up - you should use "qD instead of "qdd to avoid an extra newline at the end of the register contents. (In fact the current Vim 9.1.954 behavior seems a bit odd in that it moves the cursor down, but not to the start of the line, as if j is pressed... Seems like a bug to me.)

Izkata 5 days ago

And D goes from cursor to end of line instead of the whole line. Wasn't sure how complicated I wanted the description to be.

cryptonector 6 days ago

Most often when I run a macro it's to change lines matching searches, so I just start the macro with the search (or `n` if the macro doesn't do additional searches) then I end the macro with `@q` (or whatever register), then execute the macro. I don't think I've ever had occasion to run a macro on every line, though I've had occasion to run macros over line ranges (but still, all matching a specific pattern).

rav 5 days ago

To run a macro on the start of each line matching your search, you can use:

    :g//norm @q
Here, g// repeats the most recent search, and norm @q runs the q macro on each matched line. This is not quite the same as starting the macro with a search, since the cursor is at the start of the line and not at the start of the match, but it's often good enough.

You can also restrict it to just the matches inside a range of lines: First select the lines in visual mode, then type :g//norm @q, which will cause Vim to insert a range before g, as in: :'<,'>g//norm @q, which means "between line '< and '>, find lines containing a match of the most recent search, and run @q on each such line".

cryptonector 5 days ago

Thanks!

PaulHoule 7 days ago

These were on sale last month

https://us.govee.com/products/govee-curtain-lights

and my understanding is that you can upload an animated GIF to it... I just added making a "bad apple" GIF for it to my Kanban board though I don't know how much memory the device has and how well I can get it to work.

(Sometimes that part where Remmy Scarlet spreads her wings still makes chills go down my spine)

nokeya 6 days ago

Someone definitely need to shoot Bad Apple using a kanban board!

PaulHoule 6 days ago

Another project on my Kanban board is a plan to big spread of Touhou characters linked with QR codes. Here's a prototype card

https://bsky.app/profile/up-8.bsky.social/post/3lbqfh7pesc2x

and the spreads I make are like

https://bsky.app/profile/up-8.bsky.social/post/3latxcwmkpk2w

I have a list of 20 that I need to fill out a little (somehow I missed Koakuma) and then I have to have my image sorter find a good set of images that fit together stylistically. (A friend of mine was talking about how RA's in dorms would make picture sets for all the rooms, seemed to me Touhou would be the ultimate basis for this)

a_t48 6 days ago

I've done this with Twinkly lights, but the lights sadly don't have enough memory to run more than a few seconds.

panki27 6 days ago

I have a GIF of Bad Apple at 64x32, just under 1MB.

Big shout out to https://ezgif.com/ !

nl 6 days ago

I have one of these curtain lights and they are great!