Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
@theburningmonk
theburningmonk / Day13_Part2.fsx
Created December 15, 2016 00:46
Advent of Code (Day 13)
let part2 =
travel (1, 1)
|> Seq.take 50
|> Seq.map snd
|> Seq.sumBy Seq.length
|> (+) 1 // the starting position
@theburningmonk
theburningmonk / Day13_Part1.fsx
Created December 15, 2016 00:46
Advent of Code (Day 13)
let part1 =
travel (1, 1)
|> Seq.filter (fun (_, ps) -> ps |> List.exists ((=) (31, 39)))
|> Seq.head
|> fst
@theburningmonk
theburningmonk / Day13_shared.fsx
Created December 15, 2016 00:46
Advent of Code (Day 13)
#time
open System.Collections.Generic
open Checked
let input = 1362
// taken from http://www.necessaryandsufficient.net/2009/04/optimising-bit-counting-using-iterative-data-driven-development/
let countSetBits n =
let n = n - ((n >>> 1) &&& 0x55555555)
@theburningmonk
theburningmonk / Day13_shared.fsx
Created December 15, 2016 00:46
Advent of Code (Day 13)
#time
open System.Collections.Generic
open Checked
let input = 1362
// taken from http://www.necessaryandsufficient.net/2009/04/optimising-bit-counting-using-iterative-data-driven-development/
let countSetBits n =
let n = n - ((n >>> 1) &&& 0x55555555)
let countSetBitsV1 n =
let rec loop n count =
if n = 0 then count
else loop (n >>> 1) <| count + (n &&& 1)
loop n 0
let countSetBitsV2 n =
let rec loop n count =
if n = 0 then count
@theburningmonk
theburningmonk / Day12_Part1.fsx
Last active December 13, 2016 03:40
Advent of Code (Day 12)
open System
open System.IO
let inputs = File.ReadAllLines (__SOURCE_DIRECTORY__ + "/Day12Input.txt")
let (|Int|_|) x =
match Int32.TryParse x with
| true, n -> Some n
| _ -> None
@theburningmonk
theburningmonk / Day11_Part2.fsx
Created December 12, 2016 23:41
Advent of Code (Day 11)
let initFloorsPart2 =
[|
set [ Generator "promethium"; Microchip "promethium";
Generator "elerium"; Microchip "elerium";
Generator "dilithium"; Microchip "dilithium" ]
set [ Generator "cobalt"; Generator "curium";
Generator "ruthenium"; Generator "plutonium" ]
set [ Microchip "cobalt"; Microchip "curium";
Microchip "ruthenium"; Microchip "plutonium" ]
set []
@theburningmonk
theburningmonk / Day11_Part1.fsx
Created December 12, 2016 23:41
Advent of Code (Day 11)
let initFloorsPart1 =
[|
set [ Generator "promethium"; Microchip "promethium" ]
set [ Generator "cobalt"; Generator "curium";
Generator "ruthenium"; Generator "plutonium" ]
set [ Microchip "cobalt"; Microchip "curium";
Microchip "ruthenium"; Microchip "plutonium" ]
set []
|]
@theburningmonk
theburningmonk / Day11_solve.fsx
Created December 12, 2016 23:40
Advent of Code (Day 11)
let solve initState =
let visitedStates = new System.Collections.Generic.HashSet<string>()
let next { Elevator = floorNum; Floors = floors } =
// all the different ways items can be loaded into the elevator
let itemCombos (floor : Floor) =
seq {
for item in floor do
yield set [ item ]
for otherItem in floor.Remove item do
@theburningmonk
theburningmonk / Day11_model_part2.fsx
Created December 12, 2016 17:54
Advent of Code (Day 11)
type State =
{
Elevator : int
Floors : Floor[]
}
override x.ToString() =
let byElement =
x.Floors
|> Seq.mapi (fun n items -> items |> Seq.map (fun i -> n, i))