Code Puzzle Thread

Discuss Autohotkey related topics here. Not a place to share code.
Forum rules
Discuss Autohotkey related topics here. Not a place to share code.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Code Puzzle Thread

15 Dec 2016, 11:32

New rule: for the first month after a puzzle is made public, please only submit your solution to the puzzle author via PM so that more people get the chance to try it out before the solution is made public. The first one to solve it gets two points, everyone else within the first month gets one point.

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
  • boiler - 2 pts
  • CloakerSmoker - 2 pts
  • Emile HasKey - 2 pts
  • FanaticGuru - 2 pts
  • Helgef - 2 pts
  • just me - 2 pts
  • lexikos - 2 pts
  • wolf_II - 2 pts
  • guest3456 - 1 pt
  • jeeswg - 1 pt
  • teadrinker - 1 pt :tea:
  • Xeo786 - 1 pt

Current puzzles:
Puzzle 17.
Puzzle 15.
Dormant puzzle:
This was not completely solved:
N/A
Bonus puzzle:
These puzzles are slightly outside the scope of this thread but might still be fun for some.
Bonus puzzle 1 - By Helgef
Bonus puzzle 2 - By Helgef
Solved puzzle:
Puzzle 16. [SOLVED] by Emile HasKey
Puzzle 14. [SOLVED] by Helgef
Pzulze 13 [SOLVED] by boiler
Puzzle 12 [SOLVED] by CloakerSmoker
Puzzle 11 [SOLVED] by lexikos
Puzzle 11 [Partailly SOLVED] by jeeswg
Puzzle 10 [SOLVED] by Helgef
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 20 May 2023, 10:39, edited 40 times in total.
Helgef
Posts: 4709
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

i:=2
j:=1
MsgBox, % f(i,j)

f(a,b)
{
	static ctr:=0
	if (a+b=-4)
		return "You got it!"
	else if (++ctr>100)
		return "You Failed"
	return f(g(2*b+a,2*a),g(a,2*b+a))
}

g(x,y)
{
	return ; a non-conditional expression, eg, no (?:) or (=,<,>,...)
}
guest3456
Posts: 3453
Joined: 09 Oct 2013, 10:31

Re: Code Puzzle Thread

15 Dec 2016, 13:16

Code: Select all

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

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

Re: Code Puzzle Thread

15 Dec 2016, 16:45

guest3456 wrote:

Code: Select all

g(x,y)
{
	return mod(x, y) * -2
}
:bravo: Cheers! The score is then, guest3456 - AHK -Community: 1-0
User avatar
nnnik
Posts: 4500
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

fn( scoreTeams )
{
	return orderedTeams
}
Here's an example for a tournament Array withe three teams:

Code: Select all

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: 4709
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: 3453
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: 4709
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: 4709
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

inputArray := [ [ [1,0], [2,3] ], [ [0,1] , [0,7] ], [ [3,2],[7,0] ] ] 
scores := [ 3, 0, 6]
outputArray := [3,1,2]

out:=fn(inputArray)
str:= "Placement: `tTeam number:`n"
for k, v in out
	str.= k "`t`t" v "`n"
MsgBox, % str
ExitApp

fn( scoreTeams )
{
	scores:=[], scores[1]:=0		; Holds each team's total score, this value is appended to totalScore when all games for one team have been evaluated.
	tourEnum:=scoreTeams._NewEnum()	; enumerate all teams
	tourEnum.next(k, team)			; Retrieve first team
				
			; 	( 		enumerate each team's matches  			  ) and (   iterate over each match  and              tally score 							 || 1 to handle draw - not tested)			
	while 	(	(IsObject(teamEnum) || (teamEnum:=team._NewEnum())) && (teamEnum.next(i, match) && ((scores[k]+=(3*(match[1]>match[2])+(match[1]=match[2]))) || 1))	)	; Iterates over team k's matches																																	
			||	(((totalScore.=scores[k] "." format("{:09u}",k) A_Space )) && tourEnum.next(k, team) && ((teamEnum:="") || (scores[k]:=0) || 1)) {						; No more matches for team k, go to next team and append score
	}		;	( append total score  pad with zeros in case many teams  ) and advance to next team and (clear old team  and ensure non blank when score+=  and || 1 to make it true.
	; Sort and make array
	totalScore:=RTrim(totalScore)
	Sort, totalScore, R D%A_Space%
	totalScore:= StrSplit(RegExReplace(totalScore, "\d+\.[0]+"),A_Space)
	return totalScore
}
User avatar
nnnik
Posts: 4500
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: 3453
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

inputArray := [ [ [1,0], [2,3] ] , [ [0,1], [0,7] ] , [ [3,2], [7,0] ] ]
outputArray := fn(inputArray)
MsgBox, % "[" outputArray[1] "," outputArray[2] "," outputArray[3] "]"

fn(scoreTeams)
{
   global scores := []

   for i,team in scoreTeams
   {
      scores[i] := 0
      for j,game in team
         scores[i] += 3*(game[1] > game[2]) + (game[1] = game[2])
   }

   for i in scores
      outstr .= i . "`n"

   Sort, outstr, F MySort
   return StrSplit(outstr, "`n")
}

MySort(a, b) {
   global scores
   return scores[a] > scores[b] ? -1 : scores[a] < scores[b]
}
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

inputArray := [ [ [1,0], [2,3] ] , [ [0,1], [0,7] ] , [ [3,2], [7,0] ] ]
outputArray := fn(inputArray)
MsgBox, % "[" outputArray[1] "," outputArray[2] "," outputArray[3] "]"

fn(scoreTeams)
{
   global scores := []

   for i,team in scoreTeams
      scores[i] := Sum(Map(team, Func("CalcScore")))

   for i in scores
      outstr .= i . "`n"

   Sort, outstr, F MySort
   return StrSplit(outstr, "`n")
}

MySort(a, b) {
   global scores
   return scores[a] > scores[b] ? -1 : scores[a] < scores[b]
}

CalcScore(game) {
   return 3*(game[1] > game[2]) + (game[1] = game[2])
}

Map(array, func) {
   outarr := []
   for i,v in array
      outarr[i] := %func%(v)
   return outarr
}

Sum(array) {
   out := 0
   for i,v in array
      out += v
   return out
}

just me
Posts: 9406
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

MySort(a, b) {
   global scores
   return ((-1 * (scores[a] > scores[b])) | (scores[a] < scores[b]))
}
Helgef
Posts: 4709
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: 4709
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

Code: Select all

I[i,j,k]:=n
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

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 15071 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

#SingleInstance, force

I:=imread()	; Reads in the distorted image

; Code to restore the image

imshow(I)	; Shows the image


return

; You may add any functions or labels here:

return
GuiClose:
esc::exitapp

; Below is not to be modified - You do not need to look at this code to solve the puzzle. The image is not distorted in this code.
imshow(im)
{
	static scale:=12, w:=32, h:=32
	p:=w/2!=w//2
	VarSetCapacity(lpvBits,3*(w+p)*h,0)
	o:=0,i:=0
	Loop, % w*h
		j:=mod(A_Index-1,w)+1, j==1?++i:"" , NumPut(im[i,j,3], lpvBits, o, "Uchar"), NumPut(im[i,j,2], lpvBits, o+1, "Uchar"), NumPut(im[i,j,1], lpvBits, o+2, "Uchar"), o+=3+(j=w?p:0)
	hBitmap := DllCall("gdi32.dll\CreateBitmap", "Int", w, "Int", h, "UInt", 1, "Uint", 24, "Ptr", 0)
    hBmp := DllCall("user32.dll\CopyImage", "Uint", hBitmap, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x2008)
    DllCall("gdi32.dll\SetBitmapBits", "Uint", hBmp, "UInt", 3*(w+p)*h, "Uint", &lpvBits)		
	
	Gui, new
	Gui, margin,0,0
	Gui, add,picture, % "w" w*scale " h" h*scale  " 0x4E hwndpicId"
	Gui, show,, % "Imshow: " w*scale "-by-" h*scale " pixels."
	SendMessage, 0x172, 0x0, hBmp,, % "ahk_id " picId
	return
}

imread()
{
A=
(
0	1	1	1	1	1	1	1	1	1	1	1	1	1	1	0	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	38	91	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	45	0	0	0	0	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	66	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	177	0	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	177	0	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	80	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	46	0	0	0	0	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	170	0	0	0	0	170	170	170	170	170	38	92	171	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	0	0	206	204	0	0	170	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	0	200	200	200	200	0	172	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	170	170	170	170	0	200	200	200	200	0	172	170	170	170	38	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	7	7	7	0	0	200	200	200	200	0	0	7	8	8	2	6	9	9	9	9	18	170	170	170	170	12	7	7	7	7	0	
0	70	70	70	70	85	200	200	200	200	98	70	70	70	70	15	43	59	59	59	39	0	170	170	170	170	0	51	59	59	59	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	177	0	170	170	170	170	0	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	179	0	170	170	170	172	0	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	170	0	0	177	149	0	0	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	205	207	12	125	170	170	170	170	170	0	0	0	81	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	0	0	0	0	141	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	0	0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	0	174	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	36	0	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	0	0	0	0	131	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	30	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	200	200	200	200	200	200	200	200	200	200	200	200	200	200	44	125	170	170	170	170	170	170	170	170	170	170	170	170	170	170	0	
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
)
B=
(
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	160	39	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	167	0	0	0	0	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	0	47	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	221	0	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	0	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	216	0	0	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	216	216	216	216	216	216	216	216	216	180	0	0	0	0	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	216	105	0	0	0	216	216	216	216	216	160	15	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	0	0	118	141	0	0	216	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	0	136	135	135	135	0	227	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	216	216	216	216	0	135	135	135	135	0	224	216	216	216	160	57	255	255	255	255	255	255	255	255	255	255	255	255	255	255	0	
0	74	74	74	64	0	135	135	135	135	0	50	74	74	74	55	19	90	90	90	90	118	255	255	255	255	106	90	90	90	90	0	
0	5	5	5	5	10	135	135	135	135	15	7	7	7	7	5	1	6	6	5	0	0	255	255	255	255	0	0	5	5	5	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	137	0	255	255	255	255	0	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	137	0	255	255	255	255	0	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	135	0	0	255	255	0	0	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	73	29	135	135	135	135	135	0	0	0	0	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	0	0	0	0	36	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	63	0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	0	141	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	0	141	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	52	0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	0	0	0	0	36	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	71	29	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	135	135	135	135	135	135	135	135	135	135	135	135	135	135	99	29	135	135	135	135	135	135	135	135	135	135	135	135	135	135	0	
0	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	0	1	1	1	1	1	1	1	1	1	1	1	1	1	1	0	
)
C=
(
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	15	165	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	0	0	0	0	172	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	18	0	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	0	230	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	0	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	0	0	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	0	0	0	0	187	224	224	224	224	224	224	224	224	224	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	100	101	5	165	224	224	224	224	224	0	0	0	108	224	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	224	0	0	0	0	0	0	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	235	0	0	0	0	0	0	224	224	224	224	0	
0	98	98	98	98	98	98	98	98	98	98	98	98	98	98	22	165	224	224	224	233	0	0	0	0	0	0	224	224	224	224	0	
0	35	35	35	35	41	98	98	98	98	47	35	35	35	35	8	57	77	77	77	51	0	0	0	0	0	0	67	77	77	77	0	
0	10	10	10	0	0	98	98	98	98	0	0	10	10	10	2	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	0	98	98	98	98	0	213	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	0	98	98	98	98	0	213	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	0	0	101	100	0	0	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	0	0	0	0	210	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	57	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	218	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	219	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	210	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	56	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	210	210	210	210	210	210	210	210	210	210	210	210	210	210	46	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	2	2	2	2	2	2	2	2	2	2	2	2	2	2	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
)
	A:=txt2arr(A), B:=txt2arr(B), C:=txt2arr(C)
	I:=[]
	for l, plane in [A,B,C]
		for m, row in plane
			for n, num in row
				I[m,n,l]:=num
	return I
}

txt2arr(txt)
{
	rows:=StrSplit(txt,"`n")
	arr:=[]
	for m, row in rows
		for n, num in StrSplit(row,"`t")
			arr[m,n]:=num
	return arr
}
Good luck, and happy holidays :xmas:
Last edited by Helgef on 29 Jan 2017, 16:58, edited 1 time in total.
Helgef
Posts: 4709
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
tomoe_uehara
Posts: 213
Joined: 05 Oct 2013, 12:37
Contact:

Re: Code Puzzle Thread

29 Jan 2017, 07:47

I'd say it's because of unclear objective
User avatar
nnnik
Posts: 4500
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: 4709
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: 4709
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

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.
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: Code Puzzle Thread

31 Jan 2017, 18:53

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 10 guests