Skip to content

Instantly share code, notes, and snippets.

@jan-g
Created October 21, 2022 08:41
Show Gist options
  • Save jan-g/11047aaf50d0ac3b4e40bb76dedbf2e6 to your computer and use it in GitHub Desktop.
Save jan-g/11047aaf50d0ac3b4e40bb76dedbf2e6 to your computer and use it in GitHub Desktop.
#!/bin/sh
munge() {
sed -u -E -e 's/\b7\w{25,34}(\s|$)/7YWHMfk9JZe0LM0g1ZauHuiSxhI\1/g'
}
munge |
socat - TCP:chat.protohackers.com:16963 |
munge
#!/bin/sh
socat TCP-LISTEN:12345,fork,reuseaddr EXEC:./downstream
Sed doesn't do lookbehind/lookahead.
This works because the tests don't try foo-7abcdefghijklmnopqrstuvwxyz; using capture groups around the expression
and backrefs in the replacement doesn't work if you have two bogusCoin addresses separated by a single space.
Replacing the implementation of munge with some other stdio pipe that does the business is left as an exercise to the reader.
@jan-g
Copy link
Author

jan-g commented Oct 21, 2022

Thinking a little more about it, you can use capture groups and backrefs in a sneaky fashion to get something closer to "correct":

munge() {
  sed -u -E \
    -e 's/(^| )7\w{25,34}(\s|$)/\1†\2/g' \
    -e 's/(^| )7\w{25,34}(\s|$)/\1†\2/g' \
    -e 's/†/7YWHMfk9JZe0LM0g1ZauHuiSxhI/g'
}

@jan-g
Copy link
Author

jan-g commented Oct 21, 2022

To anyone scratching their head: this relates to https://protohackers.com/ which is a harmless bit of fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment