Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
@theburningmonk
theburningmonk / Day21_model.fsx
Created December 22, 2016 01:57
Advent of Code (Day 21)
type Direction = Left | Right
type Instruction =
| SwapPos of int * int
| SwapLetter of char * char
| Rotate of Direction * int
| RotateBasedOn of char
| Reverse of int * int
| Move of int * int
@theburningmonk
theburningmonk / Day20_findAllowedIPs.fsx
Created December 20, 2016 22:23
Advent of Code (Day 20)
let findAllowedIPs input =
let rec loop lo hi input = seq {
match input with
| [] -> ()
| (lo', hi')::tl ->
if lo' > hi + 1L then
yield! { hi+1L .. lo'-1L}
yield! loop lo (max hi hi') tl
}
@theburningmonk
theburningmonk / Day20_input.fsx
Created December 20, 2016 22:21
Advent of Code (Day 20)
open System.IO
open Checked
let input =
File.ReadAllLines(__SOURCE_DIRECTORY__ + "/Day20Input.txt")
|> Seq.map (fun line ->
let [| lo; hi |] = line.Split('-')
int64 lo, int64 hi)
|> Seq.sortBy fst
|> Seq.toList
@theburningmonk
theburningmonk / Day19_Part2.fsx
Last active December 20, 2016 13:31
Advent of Code (Day 19)
let part2 =
let n = log3 input
let m = pown 3.0 n
let l = input - m
if l = 0.0 then m |> int
elif l <= m then l |> int
else m + (l - m) * 2.0 |> int
@theburningmonk
theburningmonk / Day19_Part1.fsx
Created December 20, 2016 12:58
Advent of Code (Day 19)
let input = 3018458.0
let logn n m = log10 m / log10 n |> floor |> int
let log2 = logn 2.0
let log3 = logn 3.0
let part1 =
let l = input - pown 2.0 (log2 input) |> int
2 * l + 1
type Elf = { N: int; mutable Gifts: int }
let rec loop (elves: ResizeArray< Elf>)=
let this = elves.[0]
let opposite = elves.[elves.Count / 2]
this.Gifts <- this.Gifts + opposite.Gifts
opposite.Gifts <- 0
elves.RemoveAt(elves.Count/2)
@theburningmonk
theburningmonk / Day18_part1_part2.fsx
Created December 18, 2016 21:21
Advent of Code (Day 18)
let input = "^.^^^..^^...^.^..^^^^^.....^...^^^..^^^^.^^.^^^^^^^^.^^.^^^^...^^...^^^^.^.^..^^..^..^.^^.^.^......."
let part1 = solve input 40
let part2 = solve input 400000
@theburningmonk
theburningmonk / Day18_common.fsx
Created December 18, 2016 21:20
Advent of Code (Day 18)
open System
let previousTiles (row : string) =
seq {
yield [| '.'; row.[0]; row.[1] |]
yield! row |> Seq.windowed 3
yield [| row.[row.Length-2]; row.[row.Length-1]; '.' |]
}
let genRows input =
@theburningmonk
theburningmonk / Day17_findPaths.fsx
Created December 18, 2016 18:39
Advent of Code (Day 17)
let findPaths input =
[| ((0, 0), input) |]
|> Seq.unfold (fun paths ->
paths
|> Array.filter (fun (pos, _) -> pos <> (3, 3))
|> Array.collect (fun (pos, path) ->
let hashVals = hash path |> Seq.take 4
hashVals
|> Seq.zip [| up; down; left; right |]
|> Seq.choose (fun (f, hashVal) ->
@theburningmonk
theburningmonk / Day17_step.fsx
Created December 18, 2016 18:36
Advent of Code (Day 17)
let inline between low hi x = x >= 0 && x < hi
let step direction (dx, dy) (x, y) path =
if x + dx |> between 0 width && y + dy |> between 0 height
then Some((x+dx, y+dy), path + direction)
else None
let up = step "U" (0, -1)
let down = step "D" (0, 1)
let left = step "L" (-1, 0)