Code Puzzle Thread

Talk about anything
just me
Posts: 5577
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Code Puzzle Thread

09 Jan 2018, 07:21

Hi Helgef,

yes, I think I solved it, but I'm waiting for others who might jump in (because I won't contribute a new puzzle).
My last hint has been my personal key to solve the problem.

:wave:
Helgef
Posts: 3228
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

09 Jan 2018, 08:21

:thumbup:. submitting new puzzles aren't mandatory, you are welcome to submit your solution whenever you like.

Cheers :wave: .
just me
Posts: 5577
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Code Puzzle Thread

09 Jan 2018, 11:27

And the answer is: 42!

Athough it should always be true, it's obviously not true in this case. I tried without success. After that I noticed, that the LOOP and the Helgef sections always give the same result, regardless of the value in Count. So I added Task.tick := 0 above Task.List:= []. The Helgef section was indeed 'significant faster' after doing this: 0 ms for 50,000 Task instances, WOW! :shock:

This seemed to be a little too fast. I realized that Task.tick isn't touched by Task.List:= [] at all. From this follows that Task.__delete() is not called. The only reason for this behaviour is: The objects are not released because 'something' still holds a reference. Some thoughts later I found this 'something'. It's the enumerator object created by while (_enum:= Task.List._newEnum()) in the ENUM section. So the real answer is:

Code: Select all

; ENUM -----------------------------------------------
loop % count
	new Task("name" A_Index)
Task.time:= ""
while (_enum:= Task.List._newEnum()).Next(key, val) {
	loop {
		key.end()
	} until !_enum.Next(key, val)
}
key:=""
_enum := ""	; <<<<< added
msgBOX(result.= "ENUM`t:= " Task.tick "`n")
Due to my limited knowledge about OOP and the AHK implementation details my explanation might be technically imprecise, but the solution is working.

Cheers ;) .
Helgef
Posts: 3228
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

09 Jan 2018, 12:23

You are spot on just me, both in regards to reasoning, explaination and solution, great job :thumbup:. You will be rewarded a point.

Since just me already declined the offer to submit a new puzzle, it is free for anyone who likes to, to do so.

Cheers.
_3D_
Posts: 189
Joined: 29 Jan 2014, 14:40

Re: Code Puzzle Thread

10 Jan 2018, 03:36

_enum := "" ; <<<<< added
Cheers ;) .
Yep my mistake.

Code: Select all

class Task {
	static time:= 0
	static tick:= 0
	static List:= []
	__new(name, mode:= 0x13) {
		this.mode:= mode
		this.name:= name
		Task.List[this]:= ""
	}
	static end:= func("__Task_end")
	__delete() { ;destruct idea by Helgef
		if !Task.time
			Task.time:= A_TickCount
		if this.mode != "" { ;prevent second run
			;on destruct actions <<< ----------------------------------	;
			this.mode:= this.name:= "" ;clear to prevent second run  	;
		}																;
		Task.tick:= A_TickCount - Task.time								;
}	}																	;
__Task_end(byRef this) {												;
	if !Task.time														;
		Task.time:= A_TickCount											;
	this.__delete()			; forced call on destruct actions	<<< ---	;
	Task.List.Delete(this)	; remove reference from Task.List
	this:= "" ;if no more references <<< destruct
}

result:= ""
count:= 50000
;count:= 20
; LOOP -----------------------------------------------
loop count
	new Task("name" A_Index)
Task.time:= ""
while !isEmpty { ;just less code but the same
	isEmpty:= 0
	for key in Task.List {
		key.end()
		isEmpty:= 1 
}	}
msgBOX(result.= "LOOP`t:= " Task.tick "`n") ;<<< 1391
; Helgef ---------------------------------------------
loop count
	new Task("name" A_Index)
Task.time:= "" ;Task.time:= 0 (the same)
Task.List:= []
;next line is needed to become destruction ready
;there count times calling of Task.__delete()
;but the fastest way
msgBOX("Wait for Helgef",, "T1") ;1 seccond enough
msgBOX(result.= "HELL`t:= " Task.tick "`n") ;<<<  234 (fastest)
Ty guys!
AHKv2.0 use the future now.
Mariaspencer
Posts: 1
Joined: 24 Jan 2018, 01:57
GitHub: Mariaspencer

Re: Code Puzzle Thread

24 Jan 2018, 02:06

I was also waiting for the 3D formula to apply in the game.
User avatar
nnnik
Posts: 3459
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

02 Feb 2018, 04:26

Puzzle:
Goals:
Disconnect the Proxy from the Target
Restrictions:
You may not edit the underlying object in memory with numput/get or similar.
You may only add new code in the marked area.

Code: Select all

;add code here

;don't edit the code below
class Proxy {
	__New() {
		static init := new Proxy()
		if init
			return init
		Proxy := this
	}
	__Call( fn, p* ){
		return Target[fn]( p* )
	}
	__Set( p* ) {
		value := p.pop()
		return Target[p*] := value
	}
	__Get( p* ){
		return Target[p*]
	}
}
class Target {
	__New() {
		static init := new Target()
		if init
			return init
		Target := this
		this.val := 13
	}
	test() {
		Msgbox Hello World!
	}
}
Recommends AHK Studio
Helgef
Posts: 3228
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

02 Feb 2018, 04:49

Thank you nnnik, I will mark this as the current puzzle. You will get one point for submitting a new puzzle.

Cheers.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: Code Puzzle Thread

03 Feb 2018, 17:13

This puzzle strikes my interest, but only in that I would like to learn more about classes. So I can't solve it :(
try it and see
...
Helgef
Posts: 3228
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

12 Mar 2018, 06:41

It seems nnnik's puzzle remains unsolved, great :D. Maybe it is time for a hint?

I have posted a bonus puzzle here, if someone wants to ponder it, it assumes some understanding of c/c++ so it is not fit for this thread.

Cheers
Helgef
Posts: 3228
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Puzzle Thread

25 May 2018, 13:05

I think it is time for a new puzzle, I leave it open to anyone who likes to submit one to do so, please see the rules in the first post and do not hesitate to ask me (via pm) if you have any questions.

This is my (pre-1.1.29.00) suggestion for a solution to nnnik's puzzle,

Code: Select all

;add code here
objrawset(f(), "__set", "")
objrawset(f(), "__get", "")
objrawset(f(), "__call", "")

f(){
	static b := proxy
	return b
}
;don't edit the code below
Cheers.
User avatar
nnnik
Posts: 3459
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Code Puzzle Thread

25 May 2018, 14:06

Well I really searched for a solution myself for quite a while as it was a problem that I couldnt solve myself.
I was rather impressed at the speed he came up with the solution.
Also while Helgefs solution solves this riddle but does not solve the underlying problem for me since the underlying problem does not allow the addition of a static initialisation by writing a new function.
In AHK v1.1.29.00 you can use ObjGetBase and ObjSetBase to solve this.
Recommends AHK Studio

Return to “Offtopic”

Who is online

Users browsing this forum: No registered users and 17 guests