12

MATL, 15 bytes t4*:tB!s&=Rs-=s Try it online! Or verify all test cases. This makes use of the following result: Claim \$a(n) \le 3n/4\$ for \$n \geq 3\$. Here is a graphical illustration. The bound is quite loose, but it is sufficient to establish that only a finite number of values of \$k\$ need to be tested for a given \$n\$ (more on that in “...


11

Java 10, 374 371 364 bytes import java.util.*;m->{var L=new HashSet<Set>();int l=m.length,R=l*l,r,c,d;for(Set S;R-->0;S.add(r==R/l&c==R%l?-1:-2),L.add(S))for(S=new TreeSet(),r=R/l,c=R%l;S.add(r*l+c);r=(r-(d-d%3*d)/2+l)%l,c=(c-(d<2?1-2*d:0)+l)%l)d=m[r][c];for(Set z:L){if(z.remove(-1)){c=0;for(Set q:L)if(q.remove(-2)){if(q.containsAll(z))c++...


8

Charcoal, 96 bytes F…¹X²N«F¬⁼KK#⊞υ⟦ⅉⅈ⟧##¿&ι⊗&ι±ι↷↶»↷≔⁰ζFυ«J⊟ι⊟ι≔¹ηW⬤ur⁼№KD⊗η✳λ#⊗η≦⊕ηFη¿κ«↗↗≧⁺⬤dl⁼№KD⊗κ✳λ#⊗κζ»»⎚Iζ Try it online! Link is to verbose version of code. Explaantion: F…¹X²N« Loop over all the turns of the nᵗʰ iteration of the dragon. F¬⁼KK#⊞υ⟦ⅉⅈ⟧ If we haven't visited this cell yet then record its position. ## Print a segment of the dragon. ¿&...


7

K (ngn/k), 99 bytes {l,'@[&*/s;c;+;1][h]-l:#'c h:*'?c:{x@<x:(x?a@*|x)_x}'{?x,a x}'/a::s/s!'(+(,/-:\|:\!2)@,/x)+!s:2##x} Try it online!


6

JavaScript (ES6), 69 bytes Expects (a)(b). a=>g=(b,i=(t=0)-.1,p)=>~i?g(b,a.indexOf(b,i+1),i,q=b[i-p]?t-=~!q:0):t Try it online! How? Given the last position i of b in a, we use a.indexOf(b, i + 1) to get the position of the next occurrence. We keep track of the previous position in p and figure out whether they overlap by testing if b[i - p] is ...


6

J, 35 33 31 24 bytes 1#.1<1#.#@[>|@-//~@I.@E. Try it online! -7 bytes after reading Luis Mendo's idea and realizing I could adapt it to J I.@E. Indexes of matches |@-//~@ Table of pairwise absolute differences #@[> Is it less than the substring length? (produces 0-1 table) 1#. Sum rows 1< Greater than 1? (produces 0-1 list) 1#. Sum


6

MATL, 13 bytes yXf&-|wn<s1>s Inputs are in reverse order: b, then a. Try it online! Or verify all test cases. Explanation Consider inputs 'lol' and 'trololololol' as an example. y % Implicit inputs: b, a. Duplicate second-top element in stack % STACK: 'lol', 'trololololol', 'lol' Xf % Find second string in first string. Produces a row ...


5

Python 3, 98 96 bytes Saved 2 bytes thanks to Surculose Sputum!!! lambda n:sum(n==sum(bin(i).count('1')!=bin(k).count('1')for i in range(1,k))for k in range(5*n)) Try it online! Brute force and very slow.


4

Jelly,  44  41 bytes -3 as the grid is guaranteed to be square. ŒṪœịı*ÆiƊ+⁸ʋƬ⁺⁹ɗ€⁸%LḞQ€LÞḢṢƲ€¹ƙ$Ẉ€µḟṂLṭṂ) A monadic Link accepting a list of lists of integers which yields a list of lists of integers (each being a [loop_size, tributary_count] - i.e. \$(n_i, t_i)\$). The directions in the input are: ^ 2 > 1 V 4 < 3 Try it online! ...


4

JavaScript (ES6),  272 ... 244  239 bytes This is quite slow for \$n>6\$ but was verified locally up to \$n=8\$. k=>(b=[],k=1<<k,g=d=>k--?g(d+(g[b.push(n++,n++),x+=--d%2,y+=--d%2,[x-!d,y-(d>0)]]|=d&1||2,n&-n&n/2?1:3)&3):b.map(y=>b.map(x=>b.map(w=>o+=(h=d=>d--?g[[X=x-n/2+d,Y=y-n/2]]&g[[X,Y+w]]&2&&...


4

Python 3.8 (pre-release), 102 bytes lambda a,b:sum(b==a[i-1:i+(T:=len(b)-1)]*(b in a[i+~T:i+T-1]or b in a[i:i+T*2])for i in range(len(a))) Try it online! Explanation: it's pretty readable already. Just that i+~T == i-T-1, and the := is equivalent to an assignment but can be inserted in a lambda (to make the code shorter)


4

Jelly, 15 bytes wÐƤẹ1ạ€`<L}S>1S A dyadic Link accepting a on the left and b on the right which yields the count. Try it online! Or see the test-suite. How? wÐƤẹ1ạ€`<L}S>1S - Link: a, b ÐƤ - for postfixes (of a): w - first 1-indexed index (of b) ẹ1 - indices of 1 (i.e. X = a list of starts of b in a) ...


4

APL (Dyalog Unicode), 29 bytes (SBCS) Mutually dependent lambdas P←{≢⍸⍵=⎕CR'Q'} Q←{≢⍸⍵=⎕CR'P'} Try it online! P←… establish a function with the following definition: {…} dfn; the argument (a character) is ⍵:  ⎕CR'…' Character Representation of the function called … (as a character matrix)  ⍵= 2D mask indicating where the character is equal to elements of ...


4

PowerShell, 63 bytes Inspired by @Wasif's awesome PowerShell answer -6 bytes thanks to mazzy! $P={$Q-replace"[^$args]"|% le*} $Q={$P-replace"[^$args]"|% le*} Try it online! PowerShell, Independent Functions, 109 103 93 77 bytes This version does not require the functions to be in the same file; they are completely independent of one ...


3

APL (Dyalog Extended), 11 10 bytes ⊢/⎕⍴∘⍸⍣≡,1 Try it online! -1 byte by converting to a full program, thanks to Adám. Also works in Dyalog APL 18.0. (TIO's version is 17.1, so Extended is used for the extended functionality of ⍸.) A port of randomra's J answer as a full program. ⊢/⎕⍴∘⍸⍣≡,1 ⍣≡ ⍝ Repeat until convergence ,1 ⍝ with the ...


3

APL (Dyalog Classic), 24 19 bytes {⍵=1:1⋄1+∇⍵-∇ ∇⍵-1} -5 bytes from ngn's suggestion. Try it online! Same as the recursive function used in the PHP answer. {⍵=1:1⋄1+∇(⍵-∇(∇(⍵-1)))} ⍵(left arg) is n 1+∇(⍵-∇(∇(⍵-1))) recursive step: return 1 + f(n - f(f(n - 1))) if n is not 1. ⍵=1:1 if n is 1, then return 1.


3

Brainetry -w 0, 63 bytes Golfed version: a b c d a b c d e f g h a b c d e f g a b c d a b c d e f g h i We use -w 0 otherwise we can only count up to 255. If we add the --numeric-io flag we can also get the output as actual integers instead of codepoints. The golfed version was adapted from the program at the end of this answer. To try this online you can ...


3

K (ngn/k), 17 bytes {|/+/'~~':'x>/:x} Try it online! Modeled after Zgarb's APL answer. x>/:x build matrix storing whether or not each value of x is larger than each other value of x ~~':' for each "row", determine where the values change (i.e. differ each) +/' take the sum of each row |/ take/return the maximum sum


3

APL (Dyalog Unicode), 70 66 bytes D←⊢-1∘⌽ +/,{16=×/|D⍵/⍨×⍵×D⍵}¨(⍉1↓⌽)⍣4×(⊢-{⊂(,⍵)[8(-,⊢)3,⍳3]}⌺3 3)⎕ Try it online! -4 bytes thanks to @Bubbler and @ngn Full program that requires ⎕IO←0 ⍝ Helper function D: cyclic differences of a list D←⊢-1∘⌽ ⊢ ⍝ Each element - ⍝ Subtract 1∘⌽ ⍝ The one after ⍝ Main code +/,{16=×/|D⍵/⍨×⍵×D⍵}¨¯2 ¯2↓1⊖1⌽×(⊢-{⊂...


3

Python 2, 250 247 235 bytes Thanks Arnauld for pointing out that the grid is guaranteed to be square, saving 12 bytes! G=input() n=len(G) N={} T={} t=0 exec"j=t%n;i=t/n;A=[]\nwhile(i,j)not in A:A=[(i,j)]+A;d=G[i][j];i+=d/3-1;j+=d%3-1;i%=n;j%=n\nk=A.index((i,j))+1;x=min(A[:k]);N[x]=k;T[x]=T.get(x,[])+A[k:];t+=1;"*n*n for x in N:print N[x],len(set(T[x])) ...


3

JavaScript (Node.js), 196 bytes if width=height m=>m[Q='flatMap'](U=(y,I)=>y[Q]((x,j)=>U[T=[...m+0,Y=m.length,i=I][Q](_=>0+[j=(c=m[i][j]-2,j+Y-~c%2)%Y,i=(i+Y+c%2)%Y])[k=0,Q](S=c=>(S[c]=-~S[c])==2&&++k&&c).sort()]?(++U[T][0],[]):[U[T]=[1-k,k]])) Try it online! JavaScript (Node.js), 205 bytes m=>m[Q='flatMap'](U=(y,I)=>...


3

Retina 0.8.2, 43 bytes (?=(.+)(.*¶)\1$)(.(?!\2))+?((?=\1)|(?<=\1)) Try it online! Takes input on separate lines but link includes test suite that splits on comma for convenience. Explanation: The program consists of a single match stage that outputs the count of matches of the pattern within the string a. For each match: (?=(.+)(.*¶)\1$) The remainder ...


3

R, 90 89 bytes function(x,y,z=rle(diff(el(gregexpr(paste0("(?=",y,")"),x,,T)))<nchar(y)))sum(z$l[z$v]+1) A byte saved by Dominic van Essen. Try it online!


3

PowerShell, 128 122 bytes function P($x){cd Function:;(gc Q)-replace"[^$($x)]"|% le*} function Q($x){(gc $psCommandPath)[0]-replace"[^$($x)]"|% le*} Try it online! -6 bytes Thanks to mazzy and Zaelin Goodman Two functions work in different ways. P() will read Q()'s source code and count occurrences of the given character. Q() will read ...


3

Jelly, 24 bytes P “ṾċḷḤ”Ṿċḷ“”Ḥ Try it online! Q “ṾċḷḤ”Ṿċḷ”“Ḥ Try it online! P and Q are very similar, they both contain two of each of the same set of six characters. The only difference is “” vs ”“ (empty list vs a single open quote character). How? “ṾċḷḤ”Ṿċḷ..Ḥ - Link: character, C “ṾċḷḤ” - list of characters = ['Ṿ', 'ċ', 'ḷ', 'Ḥ'] Ṿ - ...


2

APL (Dyalog Extended), 12 bytes ≢⍸1=∘.∨⍨⍳100 Try it online! Original: (+/1=∘,⍳∘.∨⍳)100 Explanation: (+/1=∘, ⍳∘.∨⍳ )100 ⍝ ^~~~~~v ^~~~v ^~~~v ⍝ | | 100 x 100 ⍝ | greatest common divisor table of dimensions ⍝ flatten, count all values equal to one in


2

Haskell, 32 bytes sum[1|1<-gcd<$>r<*>r] r=[1..100] Try it online! Tries to follow the spirit of actually computing the result by having a "100" that can be replaced with any number.


2

CJam, 7 bytes X{_p)}h Really feels like there should be a way to take off one or two bytes, but oh well. X Initialize the stack with 1 { Begin loop _p Duplicate top of stack and print ) Add 1 }h Loop while top of stack is truthy (non-consuming)


2

Python 2, 63 bytes lambda x:max(sum(a-m^a-n<0for m,n in zip(x,x[1:]))for a in x)+1 Try it online! I'm back 6 years in the future to golf the Python answers by Sp3000 and Jakube! The improvement is shorten the core expression of (a+.5-m)*(a+.5-n)<0 or min(t)<a<=max(t) with t for m,n to: a-m^a-n<0 This is parsed as (a-m)^(a-n)<0. Due the ...


Only top voted, non community-wiki answers of a minimum length are eligible