Skip to content

Instantly share code, notes, and snippets.

View paulmr's full-sized avatar

Paul Roberts paulmr

  • Guardian
  • London
View GitHub Profile
@paulmr
paulmr / advent16.erl
Last active April 4, 2017 11:36
Advent of Code 2015, Day 16
-module(advent16).
-compile([export_all]).
-define(PATTERN1, [{ children, 3 }, { cats, 7 }, { samoyeds, 2 }, { pomeranians, 3 },
{ akitas, 0 }, { vizslas, 0 }, { goldfish, 5 }, { trees, 3 }, { cars, 2 },
{ perfumes, 1 }]).
parseLine(Line) ->
{ ok, [ SueNum ], Rest } = io_lib:fread("Sue ~d: ", Line),
@paulmr
paulmr / advent13.erl
Created March 29, 2017 15:32
Advent of Code 2015, Day 13
-module(advent13).
-compile([export_all]).
parse_person(Str) ->
list_to_atom(string:strip(Str -- ".\n")).
parse_happiness("gain", Amount) -> list_to_integer(Amount);
parse_happiness("lose", Amount) -> -list_to_integer(Amount).
@paulmr
paulmr / advent12.erl
Last active March 29, 2017 11:49
Advent of Code 2015, Day 12
-module(advent12).
%% depends on JSX <https://github.com/talentdeficit/jsx> for JSON processing
-export([main/0, main/1]).
red_is_bad([]) -> false;
red_is_bad([ { _, <<"red">> } | _ ]) -> true;
red_is_bad([_ | Rest ]) -> red_is_bad(Rest).
@paulmr
paulmr / advent10.erl
Created March 24, 2017 20:02
Advent of Code 2015, Day 10
%#!/bin/env escript
%%
-mode(compile).
digit([]) -> [];
digit([C | Rest]) ->
digit(Rest, C, 1).
digit([], C, Count) ->
integer_to_list(Count) ++ [C];
@paulmr
paulmr / advent9.erl
Last active March 27, 2017 10:54
Advent of Code 2015, Day 9
-module(advent9).
-export([main/0, main/1]).
add_distance(From, To, Distance, Map) ->
[{{To, From}, Distance},
{{From, To}, Distance} | Map ].
find_distance(From, To, Map) ->
element(2, lists:keyfind({ From, To }, 1, Map)).
@paulmr
paulmr / advent8.erl
Created March 22, 2017 20:11
Advent of Code 2015, Day 8
-module(advent8).
-export([start/0]).
cleanUp(Input) ->
string:strip(
string:strip(
Input, both, $\n
)
).
@paulmr
paulmr / advent6.erl
Created March 21, 2017 10:01
Advent of Code 2015, Day 6
-module(advent6).
-export([start/0]).
init(Width, Height) ->
lists:duplicate(Height, lists:duplicate(Width, 0)).
count(Lights) ->
lists:sum(lists:flatten(Lights)).
% http://adventofcode.com/2016/day/11
-module(advent11).
%-compile([export_all]).
-export([start/0, part1/0, part2/0, test/0]).
-record(state, { elevator = 1, floors }).
-define(TOP_FLOOR, 4).
@paulmr
paulmr / macroDef.scala
Created March 14, 2017 23:20
A simple scala macro example.
// required to enable the feature
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object MacroDefn {
def log(s: String): Unit = macro MacroImpl.log
}
object MacroImpl {
val verbose = util.Properties.propIsSet("test.logging")
function gitbase() {
local start=$PWD
cd $(while [[ ! -d ./.git ]]; do if [[ $PWD == "/" ]]; then echo $start; exit; fi; cd .. ; done; pwd);
}