r/adventofcode • • Dec 13 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 13 Solutions -🎄-

Advent of Code 2021: Adventure Time!


--- Day 13: Transparent Origami ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:09:38, megathread unlocked!

39 Upvotes

798 comments sorted by

View all comments

2

u/Durdys Dec 13 '21

F#:

module AdventOfCode2021.Day13

open System
open System.Text.RegularExpressions

type Fold =
    private
    | X of int
    | Y of int

let private regex = Regex("fold along (?<xy>\w+)=(?<location>\d+)")

let processInputData (data : string array) =
    let stringToXy s i =
        match s with
        | "x" -> X i
        | "y" -> Y i
        | _ -> failwith $"Invalid char '{s}'"
    
    let locations = data
                    |> Seq.takeWhile (fun s -> s <> "")
                    |> Seq.map (fun s ->
                        let split = s.Split(',')
                        split[0] |> Int32.Parse, split[1] |> Int32.Parse)
                    |> Seq.toList
                    
    let instructions = data
                       |> Seq.skipWhile (fun s -> s <> "")
                       |> Seq.skip 1
                       |> Seq.map regex.Match
                       |> Seq.map (fun m -> stringToXy
                                               (m.Groups["xy"].Value)
                                               (m.Groups["location"].Value |> Int32.Parse))
                       |> Seq.toList
    
    locations, instructions

let rec private calculateVisibleDots (folds : Fold list) (locations : (int * int) list)  =
    match folds with
    | [ ] -> locations
             |> Seq.distinct
             |> Seq.toList
             
    | fold :: tail ->
        let remaining, reflected = 
            match fold with
            | X i ->
                let locationsToReflect, remaining = locations
                                                    |> List.partition (fun (x, _) -> x > i)
                remaining, [ for x, y in locationsToReflect do
                               let reflectedX = (2 * i) - x
                               (reflectedX, y) ]
            | Y i ->
                let locationsToReflect, remaining = locations
                                                    |> List.partition (fun (_, y) -> y > i)
                remaining, [ for x, y in locationsToReflect do
                               let reflectedY = (2 * i) - y
                               (x, reflectedY) ]
                
        calculateVisibleDots tail (remaining @ reflected)

[<RequireQualifiedAccess>]
module Part1 =
    
    let calculateVisibleDots (locations : (int * int) list) (folds : Fold list) =
        let points = calculateVisibleDots [ folds |> List.head ] locations  
        
        points |> Seq.length
            
[<RequireQualifiedAccess>]
module Part2 =
    
    let calculateCharactersAndPrint (locations : (int * int) list) (folds : Fold list) =
        let points = calculateVisibleDots folds locations
        let xBound = points |> Seq.map fst |> Seq.sortDescending |> Seq.head
        let yBound = points |> Seq.map snd |> Seq.sortDescending |> Seq.head
        
        for y = 0 to yBound do
            for x = 0 to xBound do
                printf $"%c{if points |> Seq.contains (x, y) then 'â–ˆ' else ' '}"
            printfn ""

1

u/daggerdragon Dec 14 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.