Hacker News new | ask | show | jobs
by lloydatkinson 936 days ago
I’ve often wondered why AOC and hackerrank and the like have you solving the required problem AND the implicit task of parsing the input.
1 comments

The task of parsing the input is the only transferable skill in the whole thing.
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 ()