This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.IO | |
let input = File.ReadAllLines(__SOURCE_DIRECTORY__ + "/Day22Input.txt") | |
type Node = | |
{ | |
Position : int * int | |
Size : int | |
Used : int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |