Skip to content

Google CTF 2019 Code Golf: ["a a","b"] produces wrong output #1

@mcpower

Description

@mcpower

g ["a a","b"] returns "ab a", which is not correct - "a ba" also minimises length and is lexicographically less, as ' ' is less than all printable ASCII chars. I don't think there's any way of fixing this case without rewriting the entire thing 😢

However, with that fact in mind, you can shorten down the final solution to 132 chars which still yields the flag:

(x:a)#(y:b)=max x y:a#b
x#y=x++y
g=head.sortOn(0<$).sort.map(foldl1(\r->(#r).until(and.zipWith(((<'!').).min)r)(' ':))).permutations

Surprisingly, using these optimisations in the solution which covers edge cases allows the solution to be submitted:

(x:a)#(y:b)=max x y:a#b
x#y=x++y
u=reverse.dropWhile(==' ')
g=head.sortOn(0<$).sort.map(foldl(\r->(#r).until(and.zipWith(((<'!').).min)r)(' ':))"").permutations.map(u.u)

which fails to yield the flag. In fact, removing .map(u.u) in the solution allows the flag to be retrieved! I don't understand what's going on here, but it seems like the task was ill-defined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions