Skip to content

Instantly share code, notes, and snippets.

View BartMassey's full-sized avatar

Bart Massey BartMassey

View GitHub Profile
@BartMassey
BartMassey / aoc2017day23p2-optimize.md
Last active December 24, 2017 01:03
Optimization for Advent of Code 2017 Day 23 Part 2

Automating Advent of Code 2017 Day 23 Part 2

Copyright (c) 2017 Bart Massey

After thinking about it for a while, here's what I came up with as a series of semi-plausible automated compiler optimizations that would untangle the Day 23 inner loop.

Let's start with the raw loop. I wrote it in Go, because reasons. The f flag is set to false iff b is composite. This is what I call "trial multiplication": try

@BartMassey
BartMassey / aoc2017day4p2.py
Created December 4, 2017 09:41
Partial solution to Advent of Code 2017 Day 4 Part 2
# Once you know the Cartesian (x, y) coordinate of spiral position i in Part 1,
# you can compute Part 2 really easily using just a map from coordinates to int.
# map[(0,0)] = 1, and then you go around the spiral. At each new coordinate, you look
# at its nine neighbors with a pair of for loops. Most of them (including the new position)
# won't be in there.
# Here's a Python implementation to show what I mean.
# Essentially a Fibonacci-style dynamic
# programming calculation but with 2-3 neighbors.
@BartMassey
BartMassey / sides.txt
Created April 14, 2016 02:25
Description of reaching same position with opposite sides on move
29 W 29 W
K.... K....
..... .....
..... .....
..... .....
..... .....
....k ....k
29 B 29 B
..... .....
-- Copyright © 2016 Bart Massey
-- Chart Histogram demo
import Control.Lens
import Data.Default.Class
import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
pdfRender :: String -> Renderable a -> IO ()
M[16],X=16,W,k;main(){T(system("stty cbreak")
);puts(W&1?"WIN":"LOSE");}K[]={2,3,1};s(f,d,i
,j,l,P){for(i=4;i--;)for(j=k=l=0;k<4;)j<4?P=M
[w(d,i,j++)],W|=P>>11,l*P&&(f?M[w(d,i,k)]=l<<
(l==P):0,k++),l=l?P?l-P?P:0:l:P:(f?M[w(d,i,k)
]=l:0,++k,W|=2*!l,l=0);}w(d,i,j){return d?w(d
-1,j,3-i):4*i+j;}T(i){for(i=X+rand()%X;M[i%X]
*i;i--);i?M[i%X]=2<<rand()%2:0;for(W=i=0;i<4;
)s(0,i++);for(i=X,puts("\e[2J\e[H");i--;i%4||
puts(""))printf(M[i]?"%4d|":" |",M[i]);W-2