Author Topic: Halloween Trick or Treat  (Read 7367 times)

B+

  • Guest
Halloween Trick or Treat
« on: October 17, 2018, 03:26:39 pm »
Use your Word Search code to count the bats in this:

Quote
ABBBBBATBTAABTABTBBB
TTTTAATATTTBTATABTAA
BBAATTTBBTTAABBAAATT
AATBAAAABTTBATBABAAT
ABATTBATBTTTATTTTBTT
BBTBATABTAAAAATTTTBA
BBBATBAABTTTTBBBBABA
TABABTTTABAATABTATBT
TABBTTBBBBTATBTTTTBT
TBAABTBBTAABBTTTAATT
BAABBTTTAAABTBATBATA
ATBBBABAABTBABTABTBB
TTTTAABAAAAATTATBBBA
TABTBAABABBABBAABBAA
TBATTABABTTTATATTTBT
BTBTTTTTBBBATTABAABB
BBBTAABTBBATTAATAABT
TTATBATTATBATATBBABT
TTTBBBBBTBTATABTTBBA
TBABBAATATTABTBATBBA


Or count the number of TRICKs and TREATs in my Halloween bag:
Code: [Select]
KTIREKITRATECCCRTIAKIRTTTTRRAR
CTKRAAKCAIERIECITCRTTCTRKRITCT
TTCARTTACIKKIKTRERETIKCTCICRER
ERRTTATTTCTCTKIETAITTRKKCIECET
AIEATREAIRRIRITRTTTTAKTIEREKTR
ATTRATIECTREATTITARETRICKTIATI
TTTRERTRTIRTRICKCAEKRITITTKARR
TRRIRRETTTRICKTATEAITCCETARTIC
RIATTERTKARATTTIRTATRTCRCRTKRE
TETCATRTAETREATKTRICKKTREATRCI
KTRRTRTCTETRTEKAATCTCIECTRKTCT
TKKTTEIRAITTIEERERRITCCTTRRERR
CATATRICKKCTKTETIRRTERTKTRCRTA
TTTERCATTTKTITRKTAERTEERRTAERE
RCIAKERRATARKRRRRCCTCRTIRTETKK
TTRACRRCRCTEIRATCCREETRKTKTCRR
EEIRITKTCTCARCRTTTKTITTTIRRRCK
IIEETKETEARTTATRTRTRTRECTETEET
ITREIREETACETECAITKIRKTKTKARTR
CRRRTTIRITERTIRERRKATTCTRIRKAR

« Last Edit: October 17, 2018, 03:29:45 pm by B+ »

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #1 on: October 17, 2018, 05:06:45 pm »
Code: [Select]
print("""ABBBBBATBTAABTABTBBB
TTTTAATATTTBTATABTAA
BBAATTTBBTTAABBAAATT
AATBAAAABTTBATBABAAT
ABATTBATBTTTATTTTBTT
BBTBATABTAAAAATTTTBA
BBBATBAABTTTTBBBBABA
TABABTTTABAATABTATBT
TABBTTBBBBTATBTTTTBT
TBAABTBBTAABBTTTAATT
BAABBTTTAAABTBATBATA
ATBBBABAABTBABTABTBB
TTTTAABAAAAATTATBBBA
TABTBAABABBABBAABBAA
TBATTABABTTTATATTTBT
BTBTTTTTBBBATTABAABB
BBBTAABTBBATTAATAABT
TTATBATTATBATATBBABT
TTTBBBBBTBTATABTTBBA
TBABBAATATTABTBATBBA""".count("BAT"))

14

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #2 on: October 17, 2018, 05:16:35 pm »
Code: [Select]
puts "KTIREKITRATECCCRTIAKIRTTTTRRAR
CTKRAAKCAIERIECITCRTTCTRKRITCT
TTCARTTACIKKIKTRERETIKCTCICRER
ERRTTATTTCTCTKIETAITTRKKCIECET
AIEATREAIRRIRITRTTTTAKTIEREKTR
ATTRATIECTREATTITARETRICKTIATI
TTTRERTRTIRTRICKCAEKRITITTKARR
TRRIRRETTTRICKTATEAITCCETARTIC
RIATTERTKARATTTIRTATRTCRCRTKRE
TETCATRTAETREATKTRICKKTREATRCI
KTRRTRTCTETRTEKAATCTCIECTRKTCT
TKKTTEIRAITTIEERERRITCCTTRRERR
CATATRICKKCTKTETIRRTERTKTRCRTA
TTTERCATTTKTITRKTAERTEERRTAERE
RCIAKERRATARKRRRRCCTCRTIRTETKK
TTRACRRCRCTEIRATCCREETRKTKTCRR
EEIRITKTCTCARCRTTTKTITTTIRRRCK
IIEETKETEARTTATRTRTRTRECTETEET
ITREIREETACETECAITKIRKTKTKARTR
CRRRTTIRITERTIRERRKATTCTRIRKAR".scan(/TRICK/).length

5 TRICKs
« Last Edit: October 17, 2018, 05:30:21 pm by Tomaaz »

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #3 on: October 17, 2018, 05:28:01 pm »
Code: [Select]
(println (- (length (parse "KTIREKITRATECCCRTIAKIRTTTTRRAR
CTKRAAKCAIERIECITCRTTCTRKRITCT
TTCARTTACIKKIKTRERETIKCTCICRER
ERRTTATTTCTCTKIETAITTRKKCIECET
AIEATREAIRRIRITRTTTTAKTIEREKTR
ATTRATIECTREATTITARETRICKTIATI
TTTRERTRTIRTRICKCAEKRITITTKARR
TRRIRRETTTRICKTATEAITCCETARTIC
RIATTERTKARATTTIRTATRTCRCRTKRE
TETCATRTAETREATKTRICKKTREATRCI
KTRRTRTCTETRTEKAATCTCIECTRKTCT
TKKTTEIRAITTIEERERRITCCTTRRERR
CATATRICKKCTKTETIRRTERTKTRCRTA
TTTERCATTTKTITRKTAERTEERRTAERE
RCIAKERRATARKRRRRCCTCRTIRTETKK
TTRACRRCRCTEIRATCCREETRKTKTCRR
EEIRITKTCTCARCRTTTKTITTTIRRRCK
IIEETKETEARTTATRTRTRTRECTETEET
ITREIREETACETECAITKIRKTKTKARTR
CRRRTTIRITERTIRERRKATTCTRIRKAR" "TREAT")) 1))

3 TREATs

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #4 on: October 17, 2018, 06:08:03 pm »
If you you want to play Hi, Lo then Lo, Lo, Lo  ;D

Wait, you know there are 8 directions for Word Search from start or end of word: https://en.wikipedia.org/wiki/Word_search

Code: [Select]
D  D  D
 R R R
  OOO
DROWORD
  OOO
 R R R
D  D  D
« Last Edit: October 17, 2018, 06:18:16 pm by B+ »

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #5 on: October 18, 2018, 12:12:33 am »
Wait, you know there are 8 directions for Word Search from start or end of word: https://en.wikipedia.org/wiki/Word_search

OK. I didn't get it. I was even about to ask you what was so exciting about finding sub-strings. ;D You should have included this link in the first post. But, since replaying without understanding the task is something normal here, you need to forgive me. ;)

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #6 on: October 18, 2018, 01:19:18 am »
Tomaaz you get a B+ for being first to reply to this puzzle in 3 forums!

I thought everyone knew about Word Search.

Should I give some hints on how to put together some code for it?

BTW, I think you might have .reverse method available so .reverse.count("BAT") add any more to counts?
Then you could turn the array 90 degrees... and repeat methods...

But can you turn an array 45 degrees? hmm... I think that is a good Rosetta challenge!

Oh course this isn't how to put together Word Search code. :)
« Last Edit: October 18, 2018, 01:37:29 am by B+ »

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #7 on: October 18, 2018, 12:49:42 pm »
Kudos to John for being the 2nd public display I've found attempting my Halloween puzzle. Same wrong answers as Tomaaz but John did reference the Rosetta rules for Word Search, so why didn't he employ all tools for the job?

I did NOT know Rosetta actually had a Word Search challenge, This part of their description was followed in my puzzle:
Quote
A word search puzzle typically consists of a grid of letters in which words are hidden.

There are many varieties of word search puzzles. For the task at hand we will use a rectangular grid in which the words may be placed horizontally, vertically, or diagonally. The words may also be spelled backwards.

The words may overlap but are not allowed to zigzag, or wrap around.

zigzag probably means start going one direction and then changing course mid-word.

Here is my BASIC strategy for finding words:

For each word in search list
1. Take the first letter and run through the grid of letters for a match.
2. For a matching first letter, check each of 8 directions and see if the length of the word will fit the grid in that direction.
3. If so, build the word from the grid in that direction and length (or check letter by letter and bug out if mismatch).
4. See if it matches the word being searched, if so, count it, display it and/or record the find.
5. Repeat until all words have been searched over the entire grid ie continue with remaining directions then continue with remaining grid, unless you know there is only one word positioned in the puzzle (but my challenge is a twist on the this standard newspaper puzzle).

A BASIC strategy for the 8 directions:
I set up two arrays (or use one of vectors). I called them DX() and DY() because they are the changes (D is for Delta) of position when added to (x, y) coordinate of a location.

A normal word direction (say direction 1) is from left to right due East  change X by +1 and Y by 0  DX(1) = 1,  DY(1) = 0
SouthEast call direction 2, DX(2) = 1, DY(2) = 1 AKA diagonal down to the right
South call direction 3, DX(3) = 0, DY(3) = 1       AKA down vertically
SouthWest call direction 4, DX(4) = -1, DY(4) = 1 AKA diagonal down to left
...
DX(1) = 1 : DY(1) = 0
DX(2) = 1 : DY(2) = 1
DX(3) = 0 : DY(3) = 1
DX(4) = -1 : DY(4) = 1
DX(5) = -1 : DY(5) = 0
DX(6) = -1 : DY(6) = -1
DX(7) = 0 : DY(7) = -1
DX(8) = 1 : DY(8) = -1  (well I guess the forum editor thinks the NE direction is cool!)

There's a good chunk of the code done for you now!

Now there is another challenge, find the words by rotating and/or reflecting the grid of letters. Sound like fun?
Eh, probably not for Beginner's ASIC.  ;D

Ha! maybe only B+ thinks that might be fun?
« Last Edit: October 18, 2018, 01:01:25 pm by B+ »

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #8 on: October 18, 2018, 03:38:56 pm »
Oh hey, I think I have a faster way to attack this problem AND rotate the array of letters!
« Last Edit: October 18, 2018, 03:40:52 pm by B+ »

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #9 on: October 18, 2018, 07:18:07 pm »
Code: [Select]
tekst = "KTIREKITRATECCCRTIAKIRTTTTRRAR
CTKRAAKCAIERIECITCRTTCTRKRITCT
TTCARTTACIKKIKTRERETIKCTCICRER
ERRTTATTTCTCTKIETAITTRKKCIECET
AIEATREAIRRIRITRTTTTAKTIEREKTR
ATTRATIECTREATTITARETRICKTIATI
TTTRERTRTIRTRICKCAEKRITITTKARR
TRRIRRETTTRICKTATEAITCCETARTIC
RIATTERTKARATTTIRTATRTCRCRTKRE
TETCATRTAETREATKTRICKKTREATRCI
KTRRTRTCTETRTEKAATCTCIECTRKTCT
TKKTTEIRAITTIEERERRITCCTTRRERR
CATATRICKKCTKTETIRRTERTKTRCRTA
TTTERCATTTKTITRKTAERTEERRTAERE
RCIAKERRATARKRRRRCCTCRTIRTETKK
TTRACRRCRCTEIRATCCREETRKTKTCRR
EEIRITKTCTCARCRTTTKTITTTIRRRCK
IIEETKETEARTTATRTRTRTRECTETEET
ITREIREETACETECAITKIRKTKTKARTR
CRRRTTIRITERTIRERRKATTCTRIRKAR"

calosc = " "

tablica = tekst.split("\n").to_a.map! {|y| y.split("").to_a}
tablica.each {|y| calosc = calosc + y.join + " "}
(1..tablica[1].length).each do |x|
(1..tablica.length).each do |y|
calosc = calosc + tablica[y - 1][x - 1]
end
calosc = calosc + " "
end


(1..tablica[1].length).each do |z|
x = z
y = 1
while x <= tablica[1].length and y <= tablica.length  do
calosc = calosc + tablica[y - 1][x - 1]
x += 1
y += 1
end
calosc = calosc + " "
end

(1..tablica[1].length).to_a.reverse.each do |z|
x = z
y = 1
while x >= 1 and y <= tablica.length  do
calosc = calosc + tablica[y - 1][x - 1]
x -= 1
y += 1
end
calosc = calosc + " "
end

(1..tablica[1].length).each do |z|
x = z
y = tablica.length
while x <= tablica[1].length and y >= 1  do
calosc = calosc + tablica[y - 1][x - 1]
x += 1
y +- 1
end
calosc = calosc + " "
end

(1..tablica[1].length).to_a.reverse.each do |z|
x = z
y = tablica.length
while x >= 1 and y >= 1  do
calosc = calosc + tablica[y - 1][x - 1]
x -= 1
y +- 1
end
calosc = calosc + " "
end

print calosc.scan(/TRICK/).length, " TRICKs\n"
print calosc.scan(/TREAT/).length, " TREATs\n"

6 and 4.

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #10 on: October 18, 2018, 07:33:51 pm »
No it's not 6 and 4. I give up. Show me the solution.  ;)

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #11 on: October 18, 2018, 08:37:18 pm »
Code: [Select]
tekst = "KTIREKITRATECCCRTIAKIRTTTTRRAR
CTKRAAKCAIERIECITCRTTCTRKRITCT
TTCARTTACIKKIKTRERETIKCTCICRER
ERRTTATTTCTCTKIETAITTRKKCIECET
AIEATREAIRRIRITRTTTTAKTIEREKTR
ATTRATIECTREATTITARETRICKTIATI
TTTRERTRTIRTRICKCAEKRITITTKARR
TRRIRRETTTRICKTATEAITCCETARTIC
RIATTERTKARATTTIRTATRTCRCRTKRE
TETCATRTAETREATKTRICKKTREATRCI
KTRRTRTCTETRTEKAATCTCIECTRKTCT
TKKTTEIRAITTIEERERRITCCTTRRERR
CATATRICKKCTKTETIRRTERTKTRCRTA
TTTERCATTTKTITRKTAERTEERRTAERE
RCIAKERRATARKRRRRCCTCRTIRTETKK
TTRACRRCRCTEIRATCCREETRKTKTCRR
EEIRITKTCTCARCRTTTKTITTTIRRRCK
IIEETKETEARTTATRTRTRTRECTETEET
ITREIREETACETECAITKIRKTKTKARTR
CRRRTTIRITERTIRERRKATTCTRIRKAR"

licznik = 0

tlo = Array.new(50)
(0..49).each do |x|
tlo[x] = Array.new(50, "@")
end

tablica = tekst.split("\n").to_a.map! {|y| y.split("").to_a}
(0..tablica.length - 1).each do |x|
(0..tablica[1].length - 1).each do |y|
tlo[x + 10][y + 10] = tablica[x][y]
end
end

(10..tablica.length + 9).each do |x|
(10..tablica[1].length + 9).each do |y|
if tlo[x][y] == "T" and tlo[x + 1][y] == "R" and tlo[x + 2][y] == "I" and tlo[x + 3][y] == "C" and tlo[x + 4][y] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x + 1][y + 1] == "R" and tlo[x + 2][y + 2] == "I" and tlo[x + 3][y + 3] == "C" and tlo[x + 4][y + 4] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x][y + 1] == "R" and tlo[x][y + 2] == "I" and tlo[x][y + 3] == "C" and tlo[x][y + 4] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x - 1][y + 1] == "R" and tlo[x - 2][y + 2] == "I" and tlo[x - 3][y + 3] == "C" and tlo[x - 4][y+ 4] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x - 1][y] == "R" and tlo[x - 2][y] == "I" and tlo[x - 3][y] == "C" and tlo[x - 4][y] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x - 1][y - 1] == "R" and tlo[x - 2][y - 2] == "I" and tlo[x - 3][y - 3] == "C" and tlo[x - 4][y - 4] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x][y - 1] == "R" and tlo[x][y - 2] == "I" and tlo[x][y - 3] == "C" and tlo[x][y - 4] == "K" then
licznik += 1
elsif tlo[x][y] == "T" and tlo[x + 1][y - 1] == "R" and tlo[x + 2][y - 2] == "I" and tlo[x + 3][y - 3] == "C" and tlo[x + 4][y - 4] == "K" then
licznik += 1
end
end
end

print licznik

Well, it looks like there is 6 TRICKs, but the previous code may be giving a wrong result in some circumstances. I wanted it to be easily reusable, but finally gave up and created this one. To avoid your step 2 I've created a 2D array within a bigger 2D array, so I can test in any direction without a risk of "leaving" the array.

EDIT It's sad that, apart from you and me, no one's interested in these little pieces of code.  :(
« Last Edit: October 18, 2018, 08:50:43 pm by Tomaaz »

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #12 on: October 19, 2018, 12:06:52 am »
No it's not 6 and 4. I give up. Show me the solution.  ;)

6 tricks is what I got but you are light on treats 1 more than tricks.

You've done some work you deserve to see what I have. Direction d as described above and word start coordinates:
Treats:
d1(10, 6) (I can't read my scribbles, so take with a grain of salt, OK double checked)
d3(8, 8 )
d7(5, 9)
d1(11, 10)
d1(23, 10)
d3(12, 14)
d5(21, 14)

Tricks: (mostly d1, easiest to find)
d1(21, 6)
d1(12, 7)
d1(10, 8 )
d1(17, 10)
d4(9, 11)
d1(5, 13)

This is sort of a boring puzzle and I would welcome constructive advice on building a directions rich puzzle.
This was built by randomly plugging in the letters from MID$("TRICKTREAT", INT(RND * 10) + 1, 1)

And then inserting at random x, y alternately TRICK or TREAT at random direction. Do too many and they start overlapping and canceling each other out. Surely there is a better way!

Here is my word finder code:

Code: [Select]
_TITLE "TRICK or TREAT counter" ' for QB64 B+ 2018-10-16
'TRICK TREAT counter from word search.txt for JB [B+=MGA] 2018-10-16

DIM SHARED xCols AS INTEGER, yRows AS INTEGER 'for square block of letters xy is one side of square
xCols = 30: yRows = 20
DIM SHARED L$(1 TO xCols, 1 TO yRows)
DIM SHARED DX(1 TO 8) AS INTEGER, DY(1 TO 8) AS INTEGER
DX(1) = 1: DY(1) = 0
DX(2) = 1: DY(2) = 1
DX(3) = 0: DY(3) = 1
DX(4) = -1: DY(4) = 1
DX(5) = -1: DY(5) = 0
DX(6) = -1: DY(6) = -1
DX(7) = 0: DY(7) = -1
DX(8) = 1: DY(8) = -1

OPEN "TRICK TREAT count word search.txt" FOR INPUT AS #1
FOR y = 1 TO yRows
    INPUT #1, fline$
    FOR x = 1 TO xCols
        L$(x, y) = MID$(fline$, x, 1)
    NEXT
NEXT
CLOSE #1
showPuzzle
FOR y = 1 TO yRows
    FOR x = 1 TO xCols
        IF L$(x, y) = "T" THEN
            FOR D = 1 TO 8
                'will TRICK or TREAT fit? from "T" at x, y with 4 moves in one direction
                b$ = ""
                b1 = 4 * DX(D) + x > 0 AND 4 * DX(D) + x <= xCols
                b2 = 4 * DY(D) + y > 0 AND 4 * DY(D) + y <= yRows
                IF b1 AND b2 THEN
                    b$ = "T": xx = x + DX(D): yy = y + DY(D)
                    FOR i = 2 TO 5
                        b$ = b$ + L$(xx, yy)
                        xx = xx + DX(D): yy = yy + DY(D)
                    NEXT
                    IF b$ = "TRICK" OR b$ = "TREAT" THEN 'show our result
                        CLS
                        xx = x: yy = y
                        FOR i = 1 TO 5
                            LOCATE yy, 2 * xx - 1: PRINT L$(xx, yy);
                            xx = xx + DX(D): yy = yy + DY(D)
                        NEXT
                        IF b$ = "TRICK" THEN trickCount = trickCount + 1 ELSE treatCount = treatCount + 1
                        LOCATE yRows + 2, 5: PRINT "Trick or Treat at ("; x; ", "; y; "), Trick count = "; trickCount; ", Treat count = "; treatCount
                        LOCATE yRows + 4, 5: INPUT "OK, press enter when ready... "; wate$
                        showPuzzle
                    END IF
                END IF
            NEXT
        END IF
    NEXT
NEXT
LOCATE yRows + 4, 5: PRINT "Total TRICKs found = "; trickCount; ", Total TREATs found = "; treatCount;
END

SUB showPuzzle
    CLS
    LOCATE 1, 1
    FOR y = 1 TO yRows
        FOR x = 1 TO xCols
            PRINT L$(x, y); " ";
        NEXT
        PRINT
    NEXT
    LOCATE yRows + 2, 5: INPUT "OK, press enter when ready... "; wate$
END SUB

Screenshot of the only interesting TRICK in the whole batch of tricks.
« Last Edit: October 19, 2018, 03:45:53 am by B+ »

B+

  • Guest
Re: Halloween Trick or Treat
« Reply #13 on: October 19, 2018, 02:24:28 pm »
Update:

Turns out that array rotations was pretty easy but translating a word found using INSTR() back to the original Letters$() array is a little more tricky. Still writing functions for that before some timed tests to compare methods (assuming it will work, pretty safe assumption, I think).



Quote
EDIT It's sad that, apart from you and me, no one's interested in these little pieces of code. 

@ Tomaaz, I know my interest in Word Search was about 0 too until I looked at it as a coding challenge.
Don't be sad, allot of people who might be interested just don't have the time or it's not their time yet to visit this one little star in the universe of adventures.
« Last Edit: October 19, 2018, 02:27:52 pm by B+ »

Tomaaz

  • Guest
Re: Halloween Trick or Treat
« Reply #14 on: October 19, 2018, 03:49:21 pm »
Don't be sad, allot of people who might be interested just don't have the time or it's not their time yet to visit this one little star in the universe of adventures.

I want to believe you're right, but find it a bit hard. Just try to talk about ScriptBasic's flaws and you'll see that it's not a lack of time that is the problem...

EDIT 6 TREATs.
« Last Edit: October 19, 2018, 03:57:25 pm by Tomaaz »