r/adventofcode • • Dec 08 '16

SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---

#AoC_Ops:

[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION

We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg


--- Day 8: Two-Factor Authentication ---

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


:(){ :|:& };: 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!

10 Upvotes

197 comments sorted by

View all comments

Show parent comment

1

u/misnohmer Dec 08 '16

I ended up with something similar.

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

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

let width,height = 50,6
let screen = Array2D.init width height (fun _ _ -> false)

let create_rect (w,h) = for x in [0..(w-1)] do for y in [0..h-1] do screen.[x, y] <- true

let rotate_x y dist =
    let row = [0..width-1] |> List.map (fun x -> screen.[x, y])
    for x in [0..width-1] do screen.[(x + dist) % width, y] <- row.[x]

let rotate_y x dist =
    let column = [0..height-1] |> List.map (fun y -> screen.[x, y])
    for y in [0..height-1] do screen.[x, (y + dist) % height] <- column.[y]

let print (screen: bool[,]) =
    for y in [0..height-1] do
        for x in [0..width-1] do 
            printf (if screen.[x, y] then "x" else " ")
        printfn ""

let process_instruction line =
    match line with
     | Regex @"rect (\d+)x(\d+)" [w; h] -> create_rect (Int32.Parse w,Int32.Parse h)
     | Regex @"rotate row y=(\d+) by (\d+)" [y; dist] -> rotate_x (Int32.Parse y) (Int32.Parse dist)
     | Regex @"rotate column x=(\d+) by (\d+)" [x; dist] -> rotate_y (Int32.Parse x) (Int32.Parse dist)
     | _ -> failwith ("Unknown instruction for " + line)
    
[<EntryPoint>]
let main argv = 
    File.ReadLines("..\..\data.txt") |> List.ofSeq |> List.iter process_instruction
    screen |> Seq.cast<bool> |> Seq.filter id |> Seq.length |> (printfn "Part 1 is %d")
    print screen
    0