Code Puzzle Thread

Talk about anything
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Code Puzzle Thread

15 Dec 2016, 11:32

Edit: I adjusted the rules, you get 1 point for solving, and one point when you submit a new puzzle, it is nicer to reward someone for adding than to take away points if someone doesn't want / have time to submit a new one.

Main game rule: The first one to solve the puzzle, gets 1 point, and is allowed to submit the next puzzle, which is rewarded by an additional point . There is only one puzzle at any given time. If you want to submit a puzzle, you have to solve one first.

General puzzle rules:
  • It should be coding related.
  • It should not assume any cultural or special knowledge from other fields than programming / ahk.
  • Puzzles should be fairly short, both in formulation and solution. Eg, do not post your 5000 line, non-working, script and ask us to fix it.
  • Clearly state the objective and rules of the puzzle.
  • The submitter most be able to solve it, before posting it.
  • Multiple solution problems are allowed, if there is any solution that is not acceptable, it should be stated beforehand.
  • Code should be compatible with the latest version of ahk.
  • Edit: In general, the sought solution should not assume any knowledge about windows API or Messages. DllCalls and onmessage are allowed though, if reasonable. (Vague and subjective :lol: )
Genreal rules for a submitting solution:
  • Read the rules for the puzzle.
  • You can suggest as many solutions as you like, but only one at the time, only post new suggestions when your previous ones has been rejected.
  • Solutions should consist of runnable code and or a clearly stated answer.
  • If your solution is accepted, you are allowed to submit a new puzzle within 2 days of confirmation that it is solved, and you recieve an additional point.
  • When puzzle submition is open for anyone, submition is rewarded by 1 point.


Puzzle example



Scoreboard:
  • nnnik - 3 pts
  • qwerty12 - 3 pts
  • FanaticGuru - 2 pts
  • just me - 2 pts
  • wolf_II - 2 pts
  • guest3456 - 1 pt
  • Helgef - 1 pt
  • Xeo786 - 1 pt


Current puzzle:
Puzzle 10 - By nnnik


Solved puzzle:

Puzzle 9 [SOLVED] by just me
Puzzle 8 - by Helgef - [SOLVED] by just me
Puzzle 7 - by Xeo786
- [SOLVED] by nnnik
Puzzle 6 - By wolf_II - [SOLVED] by qwerty12
Puzzle 5 - version 1 By Helgef - [SOLVED] by qwerty12
Puzzle 5 - version 2 By Helgef - [SOLVED] by wolf_II
Puzzle 4 By Helgef - [SOLVED] by FanaticGuru
Puzzle 3 By Helgef - [SOLVED] by FanaticGuru
Puzzle 2 By nnnik - [SOLVED] by Helgef
Puzzle 1 By Helgef - [SOLVED] by guest3456


I'll start by submitting a fairly simple one.
Good luck!
Last edited by Helgef on 02 Feb 2018, 04:51, edited 24 times in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

15 Dec 2016, 11:34

Puzzle objective: Make the function f(a,b) return the string "You got it!".
Puzzle rules: You are only allowed to edit the line with the return of the function g(x,y). You are not allowed to use any conditional expressions, eg no ternary or comparisons =,>,< ....
Code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

guest3456
Posts: 2402
Joined: 09 Oct 2013, 10:31

Re: Code Puzzle Thread

15 Dec 2016, 13:16

Code: [Select all] [Download] GeSHi © Codebox Plus

g(x,y)
{
return mod(x, y) * -2
}

Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

15 Dec 2016, 16:45

guest3456 wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

g(x,y)
{
return mod(x, y) * -2
}

:bravo: Cheers! The score is then, guest3456 - AHK -Community: 1-0
User avatar
nnnik
Posts: 3223
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

16 Dec 2016, 17:39

One thing I like about AHK is the fact that you do not need to use ifs at all due to the way it handles expressions:
The following function receives the results of a tournament in form of an array like: ( [team1:[game1:[scoreteam1,scoreenemyteam], game2:...], team2... ] )
It is now your Job to order them by their overall performance: A winning team gets three points. A draw gets each team 1 point.
The team with the highest points should be returned first the one with second highest second ( since they have no names simply return their numbers ) ....
However you may not use ifs or Ternary operators and only 1 while loop.

Code: [Select all] [Download] GeSHi © Codebox Plus

fn( scoreTeams )
{
return orderedTeams
}

Here's an example for a tournament Array withe three teams:

Code: [Select all] [Download] GeSHi © Codebox Plus

inputArray := [ [ [1,0], [2,3] ], [ [0,1] , [0,7] ], [ [3,2],[7,0] ] ] 
scores := [ 3, 0, 6]
outputArray := [3,1,2]
Recommends AHK Studio
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

16 Dec 2016, 18:01

nnnik wrote:One thing I like about AHK is the fact that you do not need to use ifs at all due to the way it handles expressions:

Seems like a nice puzzle nnnik, although, the point was that the solver of the previous puzzle would submit the next, i.e., I was hoping guest3456 would submit the next one.
guest3456
Posts: 2402
Joined: 09 Oct 2013, 10:31

Re: Code Puzzle Thread

16 Dec 2016, 20:03

i'm not good at coming up with puzzles. i think nnnik's is a good one. i have no idea what he means by "the way AHK handles expressions"

Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

17 Dec 2016, 01:18

Ok, we continue with nnnik's puzzle then.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

17 Dec 2016, 19:21

Only tested on the example, I hope I got what you asked for, because this ain't pretty :lol:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
nnnik
Posts: 3223
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

20 Dec 2016, 17:32

That's good but you could have used any amount of for loops
Recommends AHK Studio
guest3456
Posts: 2402
Joined: 09 Oct 2013, 10:31

Re: Code Puzzle Thread

20 Dec 2016, 23:02

nnnik wrote:That's good but you could have used any amount of for loops


hrmmm in that case:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



i used ternary with my custom sort function but thats gotta be allowed if any Sort command is allowed

and here's the best i could do with map/reduce:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


just me
Posts: 5459
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Code Puzzle Thread

21 Dec 2016, 06:37

You don't need the ternary:

Code: [Select all] [Download] GeSHi © Codebox Plus

MySort(a, b) {
global scores
return ((-1 * (scores[a] > scores[b])) | (scores[a] < scores[b]))
}
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

21 Dec 2016, 08:54

nnnik wrote:That's good but you could have used any amount of for loops

I see, in that case my solution was unnecessarily ugly. I'll take the score since I posted first, but guest3456's solution with just me's addition is obviously nicer.
I'll try to come up with a new puzzle.

Edit: I adjusted the rules, see top post.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

26 Dec 2016, 20:06

Puzzle 3: a 4-D puzzle
There is a shorter description available here.
Note: The objectives of this puzzle has been clarified (2017-01-29)


Consider the following (undistorted) image:
Image
In ahk it can be represented as a 3-Dimensional array, eg

where i=1,2,...,h, h is the height of the image, j=1,2,...,w, w is the width of the image, k=1,2,3 are the color components (red, green and blue, respectively) , finally, n=0,1,...,255 is the color intensity, 0 is no intensity (black) and 255 is full intensity (white).
As a reference, these are the three color components of the image, displayed as grayscale images:
ImageImageImage
From left to right: Red, green and blue components of the undistorted image, as grayscale images.

To make a grayscale image of color component x, simply let,

Code: [Select all] [Download] GeSHi © Codebox Plus

I_grey[i,j,k]:=I[i,j,x] ; for all i,j and k

In this puzzle, we have w=h=32 and I[1,1] is the top-left corner of the image, and I[32,32] is bottom-right. Eg, the intensity of the red component of the top-left pixel, is at I(1,1,1), green is at I(1,1,2) and blue is at I(1,1,3).
Puzzle objective: Restore a, hard-coded, distorted version of this image, where each color plane has been (independently from the others) transformed in a fully reversible matter.


Puzzle objective clarification:
Running the code below, should show you this image (enlarged for easy viewing):
distorted.png
distorted.png (24.26 KiB) Viewed 4471 times

which is a distorted version of the first image in this post. The pixel data of this image is stored in the 3D-array I in the code, the objective is to manipulate I such that imshow(I) shows the undistorted image (i.e., the first image of this post).


Puzzle rules: You are only to manipulate the array I containing the (EDIT: distorted) pixel data as it is returned by the function imread() given in the code snippet below, you do not read in the images from this post. To complete this puzzle, post your code for fully restoring the image.
Code: Contains the distorted image, which is given in the array I, and a function, imshow(I), to display an image on the form of a 3-D array, as described in this post.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Good luck, and happy holidays :xmas:
Last edited by Helgef on 29 Jan 2017, 16:58, edited 1 time in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

21 Jan 2017, 08:51

A gentle bump :angel:
Did anyone give Puzzle 3 a try? Or, was it
  • To hard?
  • To boring?
  • Bad/unclear instructions/objectives?
Cheers!
User avatar
nnnik
Posts: 3223
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

29 Jan 2017, 07:52

Nah it has too much text
Recommends AHK Studio
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

29 Jan 2017, 10:07

Thanks for your comments, I'll try to improve it later.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

29 Jan 2017, 16:57

Puzzle 3, short description
Let
Image
be represented as a 3D-array,

Code: [Select all] [Download] GeSHi © Codebox Plus

I_org[i,j,k]:=n

where i=1,2,...,h, h=32 is the height of the image, j=1,2,...,w, w=32 is the width of the image, k=1,2,3 are the color components (red, green and blue, respectively) , finally, n=0,1,...,255 is the color intensity, 0 is no intensity (black) and 255 is full intensity (white).
Now, given a distorted version of this image, I_dist=f(I_org), find the inverse, g, of f, such that x=g(f(x)). As a reference, above are the three color components of the undistorted image, displayed as grayscale images.
Puzzle rules: Given the code: (above or download), you are only to manipulate the array I containing the distorted (called I_dist in this post) pixel data as it is returned by the function imread(), you do not read in the images from this post. To complete this puzzle, post your code for fully restoring the image.

Note: In the original (longer) description, the objectives has been clarified.
FanaticGuru
Posts: 1166
Joined: 30 Sep 2013, 22:25

Re: Code Puzzle Thread

31 Jan 2017, 18:53

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

; Code to restore the image
IT := imread()
for H, element in IT
for W, Color in element
{
I[33-H, W, 1] := Color.2
I[H, 33-W, 2] := Color.1
I[33-H, 33-W, 3] := Color.3
}
The code part was easy but it was a bit of a puzzle to figure out which colors got flipped where. Right off I knew the colors were being flipped and possibly switched between RGB but just looking at the colors I could not get it right but once I used Window Spy to get the actually RGB of the colors on the webpage of the restored image, it was trivial to get the flipping and switching right.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 8 guests