Skip to content

Instantly share code, notes, and snippets.

fn apply_instruction(program: &mut Vec<u32>, ip: usize) {
let instruction = program[ip];
let verb = program[ip+1] as usize;
let noun = program[ip+2] as usize;
let target = program[ip+3] as usize;
program[target] = match instruction {
1 => program[verb] + program[noun],
2 => program[verb] * program[noun],
_ => unimplemented!("Unknown instruction: {}", instruction),
use std::ops::AddAssign;
use std::collections::HashMap;
use std::collections::HashSet;
type Segment = (char, u32);
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
struct Point {
x: i32,
y: i32
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::cmp::max;
fn fuel_for_mass(mass: i32) -> i32 {
let mut fuel = mass;
fuel = fuel / 3; // assume round down
fuel -= 2;
//println!("mass: {} requires: {}", mass, max(fuel, 0));
max(fuel, 0)
@mbrenig
mbrenig / sum_sodoku.py
Created September 13, 2018 20:45
sumation sodoku solver - with python-constraint
# pip install python-constraint
from constraint import Problem, AllDifferentConstraint, ExactSumConstraint
DIGITS = range(1, 10)
COLS = "ABCDEFGHI"
ROWS = "123456789"
COL_GRP = ["ABC", "DEF", "GHI"]
ROW_GRP = ["123", "456", "789"]
CELLS = []
for cg in COL_GRP:
@mbrenig
mbrenig / words.py
Created August 9, 2018 16:28
Word ladder solver (RK)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) Metaswitch Networks.
"""Picks two 5 letter words at random from a dictionary,
finds a route between the words by only changing one letter at a time"""
import random
import heapq
import time
@mbrenig
mbrenig / profit.py
Created September 18, 2017 14:02
Profit Share Comparison "Tool"
"""
profit.py
Pair-wise comparison and adjustment of profit share contribution ratios.
"""
from pprint import pprint
import random
# Seed this with numbers that are approximately sensible...
@mbrenig
mbrenig / weeks_to_quarters.py
Created May 19, 2017 17:02
Assign week to quarters.
"""
Map the next 1000 weeks to quarters in a sensible way so that
most quarters are 13 weeks long... and occasionally we introduce
a 14 week quater, so as to maximise the amount of working days that
fall in their correct quarter
"""
from datetime import date, timedelta
from collections import defaultdict
import networkx as nx
@mbrenig
mbrenig / csv_entropy.py
Created April 4, 2017 14:31
csv_entropy.py -- Find the entropy in CSVs
"""
csv_entropy.py
Author: MBJ (2017)
Purpose: Command line utility to assess the most "Informative" columns in a CSV file.
Detail:
* Reads the contents of a CSV file
- Assumes header row is at the top
@mbrenig
mbrenig / query.py
Created December 2, 2016 12:14
Elasticsearch Query Example.
import json
from pprint import pprint
from elasticsearch import Elasticsearch # pip install elasticsearch
host = 'corpus.metaswitch.com'
es = Elasticsearch(hosts=[host])
query = {
"_source": ["html", "id", "subject", "breadcrumbs.title", "replyCount" ],
"size" : 100,
@mbrenig
mbrenig / dateutil.py
Last active May 25, 2016 12:02
Python libraries demo
# https://pypi.python.org/pypi/python-dateutil/2.5.3
# https://dateutil.readthedocs.io/en/stable/
from dateutil import parser # pip install python-dateutil
if __name__ == '__main__':
try:
a = parser.parse("18 Aug 2001")
print a
b = parser.parse("2016-04-05 12:33:55")
print b