Code Puzzle Thread

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

Re: Code Puzzle Thread

31 Jan 2017, 19:06

FanaticGuru wrote:

Code: Select all

; 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
Excellent! :bravo:

I shall return with some comments, in the meantime, it is now your privilege to submit a new puzzle if you like, it will reward you an additional point too ;)
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

01 Feb 2017, 19:04

FanaticGuru wrote: 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
An easy way to find how the image was distorted would have been to look at the color planes as grayscale images, as hinted in the puzzle description ;)
These are colorplanes of the distorted images, displayed as grayscale images. From left to right, red, green and blue.
rgb_dist.png
rgb_dist.png (43.23 KiB) Viewed 5177 times

Now, compare to the the undistorted grayscale images (from the puzzle description),
ImageImageImage
Here it is easy to see that red and green was swapped, and how they were flipped. Undistorted red was flipped upside down and taken as green, undistorted green was flipped from left to right and takes as red, finally, undistorted red was flipped both upside down and from left to right, it was kept as red.
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

11 Feb 2017, 13:00

I leave it open to any one who likes to submit a puzzle to do so, please read the rules in the first post first. First submition gets one point.
If it takes to long (subjective) before someone submits any puzzle, I'll try to come up with something.
Cheers.
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

04 Mar 2017, 06:51

Puzzle 4: Defeat the MsgBox.
Puzzle objectives: Make the MsgBox at the bottom of the code in the code box display the text You won instead of I Won.
Puzzle rules: You are only allowed to add one line of code on top of the code in the code box below, and nothing else.
Code:

Code: Select all

I			:= "You "
Won 		:= "Failed"
You 		:= "I "
Failed		:= "Won"
DidYouWin	:= I Failed ? !(You Failed) : I Won
MsgBox, % DidYouWin ? You Won : You Failed
Good luck.
User avatar
nnnik
Posts: 3340
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

04 Mar 2017, 08:38

How about using a massive One-Liner that opens the Script with FileOpen then read it replace some lines write it to another file then runs it. The other process then closed the opening process and Displays I won.
Recommends AHK Studio
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

04 Mar 2017, 09:05

It is not what I had in mind :)
In a way it is just a cumbersome way of changing the code which I meant you shouldn't change, in another way it is a creative idea, which cannot really be said to be forbidden from the way I formulated the rules. :think:
I guess I really didn't answer your question, so, you decide :D
FanaticGuru
Posts: 1193
Joined: 30 Sep 2013, 22:25

Re: Code Puzzle Thread

05 Mar 2017, 04:03

Helgef wrote:Puzzle 4: Defeat the MsgBox.
Puzzle objectives: Make the MsgBox at the bottom of the code in the code box display the text You won instead of I Won.
Puzzle rules: You are only allowed to add one line of code on top of the code in the code box below, and nothing else.
Code:

Code: Select all

I			:= "You "
Won 		:= "Failed"
You 		:= "I "
Failed		:= "Won"
DidYouWin	:= I Failed ? !(You Failed) : I Won
MsgBox, % DidYouWin ? You Won : You Failed
Good luck.
Solution:

Code: Select all

#CommentFlag ? 
I			:= "You "
Won 		:= "Failed"
You 		:= "I "
Failed		:= "Won"
DidYouWin	:= I Failed ? !(You Failed) : I Won
MsgBox, % DidYouWin ? You Won : You Failed
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

05 Mar 2017, 04:40

:bravo: That is the exact solution I wanted. I was thinking, either this gets solved pretty fast, or never. It is quite rare to see that directive being used.
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

30 Mar 2017, 15:08

Puzzle 5: Random exit.
Puzzle objectives: Make the script below display the random number generated by calling the function z().
Puzzle rules: You are only allowed to fill in the incomplete ternary operation on the first line. The proposed solution must always work. You are not allowed to manually assign a value to the variable rnd, the random number must be that assigned to rnd by the [docs]Random[/docs] command in the function z(). You are not allowed to call the built-in function [docs]OnExit[/docs]. No file manipulations are allowed.
Code:

Code: Select all

w:= ? : ; You can only complete this operation, the code below is not to be edited.
OnExit("z")
ExitApp
z(){
	global rnd
	Random, rnd
	ExitApp
}
mb(x,y){
	MsgBox, % %y%%x%
}
Good luck.
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

09 Jun 2017, 08:39

I give this topic a discreet bump by modifying the rules for Puzzle 5, I will allow the function [docs]OnExit[/docs] to be used. You get one point for solving it with [docs]OnExit[/docs] and two points without it (original rules).
Feel free to ask questions if you have any.
Cheers.
wolf_II
Posts: 2143
Joined: 08 Feb 2015, 20:55

Re: Code Puzzle Thread

09 Jun 2017, 15:43

Code: Select all

w:=OnExit(Func("mb").Bind("nd","r"))?:z()
OnExit("z")
ExitApp
z(){
	global rnd
	Random, rnd
	ExitApp
}
mb(x,y){
	MsgBox, % %y%%x%
}
:?:
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

09 Jun 2017, 16:00

:!: :D Perfect.

Edit:
@wolf_II, if you want, you can submit a puzzle for an additional point. The original puzzle 5 remains if someone wants, if not the solution will follow me to my grave :beard:. In the mean time it is time for a new puzzle, if wolf_II doesn't submit one within some random time ( ;) ) I'll try to come up with something.

Cheers.
User avatar
jeeswg
Posts: 5142
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Code Puzzle Thread

24 Sep 2017, 17:35

Puzzle 5.
Spoiler
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

25 Sep 2017, 03:05

That is creative jeeswg, nice one :thumbup:
However, only one line is allowed. I'll give a little hint, the solution doesn't require any knowledge about windows API (dllcall) or messages. However, if you can do it with dllcall/onmessage on one line, I'll accept it.

Note to all: If anyone has a puzzle they like to submit, please do so, make sure you read the rules in the first post. Puzzles need not be hard/easy/fun or boring, anything you can come up with is welcome. :wave:

Cheers.
Edit: I added a note in the rules:
  • 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: )
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Code Puzzle Thread

25 Sep 2017, 04:12

Code: Select all

w:= True ? {base: {__Delete: Func("mb").Bind("nd","r")}} : "93"
OnExit("z")
ExitApp
z(){
	global rnd
	Random, rnd
	ExitApp
}
mb(x,y){
	MsgBox, % %y%%x%
}
Credit to wolf_II for the BoundFunc line, I can't remember who I stole the _base trick from

EDIT: Removed the double-assignment
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

25 Sep 2017, 07:23

Perfect qwerty12 :bravo:
I can't remember who I stole the _base trick from
Maybe from the introduction to custom objects in the documentation, where we see that we can also do w:= (a:={__Delete:Func("mb").bind("d","rn")}) ? new a : "". :)

You will get 2 points for this one, and another if you submit the next puzzle. Cheers :wave:
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Code Puzzle Thread

25 Sep 2017, 08:22

Helgef wrote:Maybe from the introduction to custom objects in the documentation, where we see that we can also do w:= (a:={__Delete:Func("mb").bind("d","rn")}) ? new a : "". :)
Ah, thank you! (Properly thinking about it, I think I might have picked it up from a post by Nextron, as a way to deregister, say, a shell hook without an explicit OnExit handler - I've never actually looked at the documentation backing that :oops:)

I am sorry, I will have to decline the offer to submit my own puzzle; my knowledge of the AutoHotkey language itself is pretty basic and, well, I just don't have the ability to actually come up with a non-trivial puzzle. But thanks again :) Regards!
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

25 Sep 2017, 10:16

my knowledge of the AutoHotkey language itself is pretty basic
:lolno: :lol:
It is free for anyone who likes to submit a puzzle then.

Cheers.
wolf_II
Posts: 2143
Joined: 08 Feb 2015, 20:55

Re: Code Puzzle Thread

27 Sep 2017, 11:42

After reading the posts by qwerty12, I remembered coming across an interesting unexpected behaviour in a function I wrote some time back (being inspired by forum folks, one of which was HotkeyIt). After checking back with our quizzmaster Helgef I now present my puzzle in the form of two functions (the first of which is buggy IMHO), and a use-case for the two functions that demonstrates the unexpected behaviour:

Code: Select all

;-------------------------------------------------------------------------------
Watchdog(wParam, lParam) { ; a self-contained ShellHook function
;-------------------------------------------------------------------------------
    static init   := DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
        , MsgNum  := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
        , neglect := OnMessage(MsgNum, "Watchdog")
        , CleanUp := {base: {__Delete: "Watchdog"}}

    If Not IsObject(CleanUp) {
        OnMessage(MsgNum, "")
        DllCall("DeregisterShellHookWindow", "Ptr", A_ScriptHwnd)
    }

    If (wParam = 1)     ; HSHELL_WINDOWCREATED
    Or (wParam = 4)     ; HSHELL_WINDOWACTIVATED
    Or (wParam = 32772) ; HSHELL_RUDEAPPACTIVATED (fullscreen app activated)
        Guard(lParam)   ; lParam is the active window's handle
}



;-------------------------------------------------------------------------------
Guard(hWnd) { ; do something with the new active window
;-------------------------------------------------------------------------------
    ; code
}

Code: Select all

SetBatchLines, -1
Return ; end of auto-execute section



;-------------------------------------------------------------------------------
Watchdog(wParam, lParam) { ; a self-contained ShellHook function
;-------------------------------------------------------------------------------
    static init   := DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
        , MsgNum  := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
        , neglect := OnMessage(MsgNum, "Watchdog")
        , CleanUp := {base: {__Delete: "Watchdog"}}

    If Not IsObject(CleanUp) {
        MsgBox, CleanUp is being called! ; temporary line added for debugging
        OnMessage(MsgNum, "")
        DllCall("DeregisterShellHookWindow", "Ptr", A_ScriptHwnd)
    }

    If (wParam = 1)     ; HSHELL_WINDOWCREATED
    Or (wParam = 4)     ; HSHELL_WINDOWACTIVATED
    Or (wParam = 32772) ; HSHELL_RUDEAPPACTIVATED (fullscreen app activated)
        Guard(lParam)   ; lParam is the active window's handle
}



;-------------------------------------------------------------------------------
Guard(hWnd) { ; do something with the new active window
;-------------------------------------------------------------------------------

    ; this example displays a ToolTip with some info
    WinGetTitle, Title, ahk_id %hWnd%
    WinGetClass, Class, ahk_id %hWnd%
    ToolTip, %Title%`n%Class%
    SetTimer, ToolTipOff, -2000
    Return

    ToolTipOff:
        ToolTip ; off
    Return
}
I would expect to see a MsgBox when I exit the script, but I see none. The whole CleanUp will not get called, unless the bug is fixed.

The puzzle is: Can someone fix the bug?
Nebulous hint

Good luck and have fun! :D
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

27 Sep 2017, 17:08

Cool wolf_II. I'll make a note in the first post that this is the current puzzle :thumbup:

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 7 guests