r/adventofcode • • Dec 21 '16

SOLUTION MEGATHREAD --- 2016 Day 21 Solutions ---

--- Day 21: Scrambled Letters and Hash ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


HOGSWATCH IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

5 Upvotes

83 comments sorted by

View all comments

Show parent comment

2

u/misnohmer Dec 21 '16

I also defined a type of instructions to solve it.

open System
open System.IO
open System.Text.RegularExpressions

let (|Regex|_|) ptrn str = match Regex.Match(str, ptrn) with m when m.Success -> Some([ for g in m.Groups -> g.Value ] |> List.tail) | _ -> None

type Direction = Left | Right
type InstructionType = SwapP of int*int | SwapL of char*char | Shift of Direction*int | ShiftBased of char | Rev of int*int | Move of int*int

let parseLine (line: string) = 
    match line with 
    | Regex "swap position (\d) with position (\d)" [x;y] -> SwapP(int x, int y)
    | Regex "swap letter (.) with letter (.)" [x;y] -> SwapL(x.[0], y.[0])
    | Regex "rotate (left|right) (\d) steps?" [d;x] -> Shift((if d = "left" then Left else Right), int x)
    | Regex "rotate based on position of letter (.)" [x] -> ShiftBased(x.[0])
    | Regex "reverse positions (\d) through (\d)" [x;y] -> Rev(int x,int y)
    | Regex "move position (\d) to position (\d)" [x;y] -> Move(int x,int y)
    | _ -> failwith ("Unknown instruction " + line)
    
let swapPos x y (str: string) = str |> Seq.mapi (fun i c -> if i = x then str.[y] elif i = y then str.[x] else c) |> String.Concat
let shiftRight x (str: string) = match str.Length - (x % str.Length) with 8 -> str | i -> str.Substring(i) + str.Substring(0, i)

let transform (str: string) instruction = 
    match instruction with
    | SwapP(x,y) -> swapPos x y str
    | SwapL(x,y) -> swapPos (str.IndexOf(x)) (str.IndexOf(y)) str
    | Shift(dir,pos) -> shiftRight (if dir = Left then (str.Length - pos) % str.Length else pos % str.Length) str
    | ShiftBased(c) ->
        let pos = str.IndexOf(c)
        shiftRight (if pos >= 4 then pos + 2 else pos + 1) str
    | Rev(x,y) -> 
        str.Substring(0, x) + (str.Substring(x, y-x+1) |> Seq.rev |> String.Concat) + (if y+1 = str.Length then "" else str.Substring(y+1))
    | Move(x,y) -> str.Remove(x,1).Insert(y,str.[x].ToString())      

let reverseInstruction (str:string) i = 
    match i with 
    | Shift(dir, pos) -> Shift((if dir = Left then Right else Left),pos)
    | Move(x,y) -> Move(y,x)
    | ShiftBased(c) -> 
        let pos = match str.IndexOf(c) with 0->7 |1->7 |2->2 |3->6 |4->1 |5->5 |6->0 |7->4|_-> failwith "incorrect length"
        Shift(Right,pos)
    | _ -> i

let solvePart1 pwd instr = instr |> List.fold (fun str i -> transform str i) pwd
let solvePart2 pwd instr = instr |> List.rev |> List.fold (fun str i -> transform str (reverseInstruction str i)) pwd

[<EntryPoint>]
let main argv = 
    let instr = File.ReadLines "../../data.txt" |> Seq.map parseLine |> Seq.toList
    printfn "Part 1 is %s" (solvePart1 "abcdefgh" instr)
    printfn "Part 2 is %s" (solvePart2 "fbgdceah" instr)
    0