Hacker News new | ask | show | jobs
by nikolay 936 days ago
I wonder why these are not available from the project directly and why input data is not in some standard format like JSON. Parsing the input for speed is the dirtiest part. If it was always in JSON, then at least that part would have been prettier!

Last nights task I did with stinky code string-splitting for speed.

2 comments

I think the first (or first couple) AOC relied on JSON input more.

I assumed Eric (the creator) moved away from JSON to have a level playing field and simplify parsing (and potentially build dependencies) across various programming languages.

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.
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 ()