Hacker News new | ask | show | jobs
by pitched 936 days ago
The task of parsing the input is the only transferable skill in the whole thing.
1 comments

But you can't do a decent job if you want to earn points. Parsing requires diligence and less assumption. Add one extra space, and the whole AOC parsing leads to unpredictable results.
One of my solutions last year, with F#, was very aware of bad input and was robust against it.

Here's a part of it:

        type Shape =
            | Rock
            | Paper
            | Scissors

        let parseCharacters (line: string) =
            let onlySingleCharacters (a: string, b: string) = a.Length = 1 && b.Length = 1

            match line.Split() with
            | [| a; b |] when onlySingleCharacters (a, b) ->
                match a[0], b[0] with
                | 'A', 'X' -> Some(Rock, Rock)
                | 'A', 'Y' -> Some(Rock, Paper)
                | 'A', 'Z' -> Some(Rock, Scissors)
                | 'B', 'X' -> Some(Paper, Rock)
                | 'B', 'Y' -> Some(Paper, Paper)
                | 'B', 'Z' -> Some(Paper, Scissors)
                | 'C', 'X' -> Some(Scissors, Rock)
                | 'C', 'Y' -> Some(Scissors, Paper)
                | 'C', 'Z' -> Some(Scissors, Scissors)
                | _ -> None
            | _ -> None

        let rec readLines () =
            let line = Console.ReadLine()

            match line with
            | null
            | "" -> []
            | _ ->
                match parseCharacters line with
                | Some parsedLine -> parsedLine :: readLines ()
                | None -> readLines ()