Skip to content

Instantly share code, notes, and snippets.

Day 3

Part 1

There are 2 perfect squares each rotation of the spiral. ⌊(⌈⍵*÷2)÷2 is a Chebyshev distance of ⍵ to origin, i.e. the absolute value of larger coordinate. To get the other, subtract that square and do some modular magic dance (there are 4 zeros at each layer, and 4 max values at turning points where both coordinates are equal), and just add the two together.

{(|k-(2×k)|⍵-(1+k×2)*2) + k←⌊(⌈⍵*÷2)÷2 ;k} 277678

(That ;k is GNU APL notation for local variable, just remove it for Dyalog).

Part 2

@mwgamera
mwgamera / abx.tcl
Last active September 7, 2019 14:03
#!/bin/sh
# Bare-bones Tk GUI for ABX testing.
# klg, Nov 2017; next line executed by sh \
exec wish "$0" ${1+"$@"}
package require Tk
package require msgcat
namespace import msgcat::mc
# Require k of n trials to pass
lassign {13 16} abx_k abx_n
#!/usr/bin/env perl
# List or edit comments in RFC 7845 Ogg Opus files.
# klg, Nov 2017
use strict;
package Ogg::Page {
use strict;
use Carp;
use Digest::CRC;
#!/usr/bin/env perl
# Show combined changelog for the whole dpkg-based system.
# klg, Nov 2017
use strict;
use File::ReadBackwards;
use Dpkg::Version 'version_compare';
use Dpkg::Changelog::Debian;
open \*STDOUT, '|-', $ENV{PAGER} || 'pager' if -t;
@mwgamera
mwgamera / .pl
Last active December 3, 2017 23:23
use utf8;
use Unicode::Normalize;
# Expand horizontal Kana iteration marks
# 例: いすゞ→いすず ところゞゝゝ→ところどころ
sub expand_kanaiter {
local $_ = NFKD($_[0]);
s{(
[あ-んア-ンー][\x{3099}\x{309a}]?
(|(?1))
#!/usr/bin/env perl
# Strip non-essential markers from JPEG data and output bare JIF file
# (which might not actually be suitable for interchange because of missing
# color space information, subsampling geometry, and whatnot).
# klg, Sep 2017
use strict;
my @M;
{
my $buf = '';
@mwgamera
mwgamera / tmux.pl
Last active January 28, 2022 03:13
# Emit signals when tmux session is attached or detached.
# klg, May 2017
use strict;
use Irssi;
use POSIX 'mkfifo';
Irssi::signal_register {
'tmux attached' => ['int'],
'tmux detached' => ['int']
};
@mwgamera
mwgamera / vizon.pl
Last active October 30, 2017 23:48
# Bet in Rizon lottery
# <https://wiki.rizon.net/index.php?title=Vizon>
# klg, May 2017
use strict;
use Irssi;
use List::Util 'shuffle';
my $server_tag = 'Rizon';
my $vizon_nick = 'VIzon';
#!/bin/sh
# Execute a window manager but allow changing it easily at run time.
# Requires sufficiently dumb WM that eofs on exit (beware of its children
# sharing standard output) and leaves sane state when simply killed.
# klg, Mar 2017
# \
exec wish "$0" "$@"
package require Tk
wm withdraw .
#!/usr/bin/es --
# Password-based encryption with GPG2
# klg, Dec 2016
fn easkpass {
* = $* 'Password: '
<>/dev/tty >[1=0] let (
g = `{stty -g}
)
unwind-protect {