Skip to content

Instantly share code, notes, and snippets.

View BartMassey's full-sized avatar

Bart Massey BartMassey

View GitHub Profile
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
-- 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 ()
@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
..... .....
@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 / 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 / memsafe.rs
Created February 4, 2018 03:28
Memory safety demo in Rust by Will Chrichton. Compile with `rustc -o memsafe memsafe.rs`
// http://willcrichton.net/notes/rust-memory-safety/
// Modified by Bart Massey 2018-02-03
#![feature(alloc, allocator_api)]
extern crate alloc;
use std::slice;
use std::heap::{Heap, Alloc};
use alloc::allocator::Layout;
-- Copyright © 2017 Bart Massey
-- | Illustrate the FTP problem in a realistic way.
import Data.List
import Data.Maybe
-- | Given a list of values `xs`, return a list of tuples
-- consisting of a list of `n` copies of a given value and a
-- count of its occurrences, ordered by value. Setting `n`
@BartMassey
BartMassey / deck.rs
Created May 10, 2018 00:47
Deck of cards in Rust
// Copyright © 2018 Bart Massey
// Simple deck of cards using enum_derive.
#[macro_use] extern crate custom_derive;
#[macro_use] extern crate enum_derive;
custom_derive! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, IterVariants(Suits))]
@BartMassey
BartMassey / swagger-params.json
Created May 10, 2018 20:28
Swagger 2.0 complex parameter demo
{
"consumes": [
"application/json"
],
"swagger": "2.0",
"basePath": "/",
"schemes": [
"https"
],
"produces": [
@BartMassey
BartMassey / gc.py
Created May 17, 2018 19:27
Demonstration of need for garbage collection in Python
# Copyright (c) 2018 Bart Massey
# GC demo.
# Create a circular structure. The reference
# count of x and y would be 2: one for the
# variable and 1 for the internal reference.
x = [1]
y = [2]
y[0] = x