Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
@theburningmonk
theburningmonk / Day23_execute.fsx
Created December 24, 2016 00:36
Advent of Code (Day 23)
let toggle = function
| Inc reg -> Dec reg
| Dec reg | Tgl reg -> Inc reg
| Cpy (n, dest) -> Jnz (n, dest)
| Jnz (n, offset) -> Cpy (n, offset)
let execute initValues (inputs : string[]) =
let registers = new Dictionary<string, int>()
initValues |> Seq.iter (fun (key, value) -> registers.[key] <- value)
@theburningmonk
theburningmonk / Day23_parse.fsx
Created December 24, 2016 00:36
Advent of Code (Day 23)
open System.IO
let inputs = File.ReadAllLines (__SOURCE_DIRECTORY__ + "/Day23Input.txt")
type Operant =
| Value of int
| Reg of string
type Instruction =
| Cpy of n: Operant * dest: Operant
@theburningmonk
theburningmonk / Day22_Part2.fsx
Created December 23, 2016 20:54
Advent of Code (Day 22)
open System.Collections.Generic
let findPaths (grid : Node[,]) start target =
let cache = new HashSet<int * int>([start])
[| ([start], grid) |]
|> Seq.unfold (fun states ->
states
|> Seq.collect (fun ((x, y)::tl, grid) ->
[ x+1, y; x-1, y; x, y-1; x, y+1 ]
@theburningmonk
theburningmonk / Day22_grid.fsx
Created December 23, 2016 20:22
Advent of Code (Day 22)
let maxX = nodes |> Array.map (fun n -> fst n.Position) |> Array.max
let maxY = nodes |> Array.map (fun n -> snd n.Position) |> Array.max
let between min max x = x >= min && x <= max
let grid = Array2D.init (maxX + 1) (maxY + 1) (fun x y ->
nodes |> Array.find (fun n -> n.Position = (x, y)))
let move (srcX, srcY) (destX, destY) (grid : Node[,]) =
let src = grid.[srcX, srcY]
let dest = grid.[destX, destY]
@theburningmonk
theburningmonk / Day22_Part1.fsx
Last active December 23, 2016 21:34
Advent of Code (Day 22)
let byAvail = nodes |> Array.sortByDescending (fun x -> x.Size - x.Used)
let part1 =
nodes
|> Seq.filter (fun x -> x.Used > 0)
|> Seq.sumBy (fun x ->
byAvail
|> Seq.takeWhile (fun y -> y.Size - y.Used >= x.Used)
|> Seq.filter (fun y -> y.Position <> x.Position)
|> Seq.length)
@theburningmonk
theburningmonk / Day22_parse.fsx
Created December 23, 2016 20:14
Advent of Code (Day 22)
open System
open System.IO
let input = File.ReadAllLines(__SOURCE_DIRECTORY__ + "/Day22Input.txt")
type Node =
{
Position : int * int
Size : int
Used : int
@theburningmonk
theburningmonk / Day21_Part2_v2.fsx
Created December 22, 2016 03:02
Advent of Code (Day 21)
let inverseRotateBasedOn char (input : char[]) =
{ 1..input.Length }
|> Seq.map (fun n -> rotate Left n input)
|> Seq.filter (fun original -> rotateBasedOn char original = input)
|> Seq.head
let applyInverse instructions input =
instructions
|> Array.rev
|> Array.fold (fun input inst ->
@theburningmonk
theburningmonk / Day21_Part2.fsx
Created December 22, 2016 02:10
Advent of Code (Day 21)
let rec insertions x = function
| [] -> [[x]]
| (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys))
let rec permutations = function
| [] -> seq [ [] ]
| x :: xs -> Seq.concat (Seq.map (insertions x) (permutations xs))
let part2 =
permutations [ 'a'..'h' ]
@theburningmonk
theburningmonk / Day21_scramble.fsx
Last active December 22, 2016 02:04
Advent of Code (Day 21)
let swapPos src dest (input : char[]) =
Array.init input.Length (fun idx ->
if idx = src then input.[dest]
elif idx = dest then input.[src]
else input.[idx])
let swapLetter lf rt (input : char[]) =
input |> Array.map (fun x ->
if x = lf then rt
elif x = rt then lf
@theburningmonk
theburningmonk / Day21_parse.fsx
Created December 22, 2016 01:59
Advent of Code (Day 21)
open System.IO
open System.Text.RegularExpressions
let input = File.ReadAllLines(__SOURCE_DIRECTORY__ + "/Day21Input.txt")
let (|Regex|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
else None