Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mcpower opened this issue Jun 26, 2019 · 1 comment
Open

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

mcpower opened this issue Jun 26, 2019 · 1 comment

Comments

@mcpower
Copy link

mcpower commented Jun 26, 2019

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.

@ldruschk
Copy link
Owner

ldruschk commented Jun 26, 2019

Indeed, I apparently missed yet another special case. I think with your approach of using the ordering of chars, which gives us another 13 bytes for the solution, it could be possible to rewrite the solution to cover this case as well, for example by replacing the until and instead of taking the first solution finding all solutions (although you need to figure out when to stop), but I currently don't have the time to look into this further.

Regarding what solutions get accepted there definitely seems to be something wrong with the 2nd rule, or we are all just misunderstanding it. I guess instead of the service not supplying strings with leading/trailing spaces, we just got lucky and the service did not trigger the special case that I dropped at the end. My second idea would have been the same as in your writeup, trimming spaces when looking at the length but not when returning them, but as you pointed out, this fails as well.

I guess it's best to wait until they (hopefully) release the source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants