Dig a border trench
$begingroup$
Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*
Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.
For example: With Astanian cells marked A
, Blandic cells marked B
and trench cells marked +
(the map frames are only for clarity):
┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘
Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.
Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.
Test cases
[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]
[
"AAA",
"AAA",
"BBB"
]
becomes:
[
"AAA",
"AAA",
"+++"
]
[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]
* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!
code-golf matrix cellular-automata
$endgroup$
|
show 2 more comments
$begingroup$
Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*
Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.
For example: With Astanian cells marked A
, Blandic cells marked B
and trench cells marked +
(the map frames are only for clarity):
┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘
Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.
Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.
Test cases
[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]
[
"AAA",
"AAA",
"BBB"
]
becomes:
[
"AAA",
"AAA",
"+++"
]
[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]
* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!
code-golf matrix cellular-automata
$endgroup$
17
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
4
$begingroup$
-1 for that<sup><sub><sup><sub><sup><sub><sup><sub>
:-P
$endgroup$
– Luis Mendo
2 days ago
19
$begingroup$
python, 4 bytes:pass
The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
2 days ago
3
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
1
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday
|
show 2 more comments
$begingroup$
Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*
Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.
For example: With Astanian cells marked A
, Blandic cells marked B
and trench cells marked +
(the map frames are only for clarity):
┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘
Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.
Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.
Test cases
[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]
[
"AAA",
"AAA",
"BBB"
]
becomes:
[
"AAA",
"AAA",
"+++"
]
[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]
* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!
code-golf matrix cellular-automata
$endgroup$
Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*
Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.
For example: With Astanian cells marked A
, Blandic cells marked B
and trench cells marked +
(the map frames are only for clarity):
┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘
Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.
Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.
Test cases
[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]
[
"AAA",
"AAA",
"BBB"
]
becomes:
[
"AAA",
"AAA",
"+++"
]
[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]
becomes:
[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]
* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!
code-golf matrix cellular-automata
code-golf matrix cellular-automata
edited 2 days ago
Adám
asked 2 days ago
AdámAdám
29.4k271194
29.4k271194
17
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
4
$begingroup$
-1 for that<sup><sub><sup><sub><sup><sub><sup><sub>
:-P
$endgroup$
– Luis Mendo
2 days ago
19
$begingroup$
python, 4 bytes:pass
The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
2 days ago
3
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
1
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday
|
show 2 more comments
17
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
4
$begingroup$
-1 for that<sup><sub><sup><sub><sup><sub><sup><sub>
:-P
$endgroup$
– Luis Mendo
2 days ago
19
$begingroup$
python, 4 bytes:pass
The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
2 days ago
3
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
1
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday
17
17
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
4
4
$begingroup$
-1 for that
<sup><sub><sup><sub><sup><sub><sup><sub>
:-P$endgroup$
– Luis Mendo
2 days ago
$begingroup$
-1 for that
<sup><sub><sup><sub><sup><sub><sup><sub>
:-P$endgroup$
– Luis Mendo
2 days ago
19
19
$begingroup$
python, 4 bytes:
pass
The plans to build a border trench lead to a government shutdown and nothing happens.$endgroup$
– TheEspinosa
2 days ago
$begingroup$
python, 4 bytes:
pass
The plans to build a border trench lead to a government shutdown and nothing happens.$endgroup$
– TheEspinosa
2 days ago
3
3
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
1
1
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday
|
show 2 more comments
18 Answers
18
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 15 bytes
2#-#~Erosion~1&
Try it online!
Or (39 bytes):
MorphologicalPerimeter[#,Padding->1]+#&
Try it online!
What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}
.
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 23 bytes
{x+x&2{++/'3'0,x,0}/~x}
Try it online!
uses 0 1 2
for "AB+"
{
}
function with argument x
~
logical not
2{
}/
twice do
0,x,0
surround with 0-s (top and bottom of the matrix)3'
triples of consecutive rows+/'
sum each+
transpose
x&
logical and of x
with
x+
add x
to
$endgroup$
add a comment |
$begingroup$
MATL, 11 8 bytes
Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.
EG9&3ZI-
The input is a matrix with Astan represented by 0
and Blandia by 1
. Trench is represented in the output by 2
.
Try it online!
How it works
E % Implicit input. Multiply by 2, element-wise
G % Push input again
9 % Push 9
&3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
- % Subtract, element-wise. Implicit display
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 84 82 bytes
Saved 2 bytes thanks to @Shaggy
Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.
a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()
Try it online!
Commented
a => ( // a = input matrix
g = x => // g = function taking x (initially undefined)
a.map(t = // initialize t to a non-numeric value
(r, Y) => // for each row r at position Y in a:
r.map((v, X) => // for each value v at position X in r:
1 / x ? // if x is defined (this is a recursive call):
t |= // set the flag t if:
(x - X) ** 2 + // the squared Euclidean distance
(y - Y) ** 2 // between (x, y) and (X, Y)
< v // is less than v (3 = Astan, 0 = Blandia)
: // else (this is the initial call to g):
v || // yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
| t // and yield the flag t (0 = no change, 1 = trench)
) // end of inner map()
) // end of outer map()
)() // initial call to g
$endgroup$
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
add a comment |
$begingroup$
Octave, 37 31 26 bytes
This function performs a morphological erosion on the Astan (1-b
) part of the "image" using conv2
imerode
, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!
@(b)2*b-imerode(b,ones(3))
Try it online!
$endgroup$
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 11 bytesSBCS
⊢⌈{2∊⍵}⌺3 3
this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊
in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output
Try it online!
represents 'AB+'
as 2 0 1
respectively
{
}⌺3 3
apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s
2∊⍵
is a 2 present in the argument? return a 0/1 boolean
⊢⌈
per-element max of that and the original matrix
$endgroup$
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias fordisplay
which i forgot to remove. removed now
$endgroup$
– ngn
2 days ago
add a comment |
$begingroup$
Charcoal, 20 bytes
≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι
Try it online! Link is to verbose version of code. Explanation:
≔⪫θ⸿θ
Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.
Pθ
Print the input string without moving the cursor.
Fθ
Loop over each character of the input string.
⎇∧№KMA⁼Bι
If the Moore neighbourhood contains an A
, and the current character is a B
...
+
... then overwrite the B
with a +
...
ι
... otherwise print the current character (or move to the next line if the current character is a carriage return).
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
>.3 3(2 e.,);._3(0|:@,|.)^:4
Try it online!
'AB+'
-> 2 0 1
Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 92 80 bytes
(?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
a
iT`Ba`+`.?a.?
Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any B
s immediately above or below A
s are turned into a
s. This then reduces the problem to checking B
s to the left or right of A
s or a
s. The a
s themselves also need to get turned into +
s of course, but fortunately the i
flag to T
only affects the regex match, not the actual transliteration, so the A
s remain unaffected.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 187 bytes
a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))
Instead of chaining Take()
s, Skip()
s, and Select()
s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5, 58 46 bytes
$m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo
TIO
-12 bytes thanks to @Grimy
/.
/;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
TIO
-p
like-n
but print also
-00
paragraph mode- to get the width-1
/.n/
matches the last character of first line
@{-}
special array the position of start of match of previous matched groups, coerced as string (first element)
s/../+/s&&redo
replace match by+
while matches
/s
flag, so that.
matches also newline character
A(|.{@{-}}.?.?)KB
matches
AB
orA
followed by (width-1) to (width+1) characters folowed byB
K
to keep the left ofB
unchanged
B(?=(?1)A)
,
(?1)
dirverting recursive, to reference previous expression(|.{$m,$o})
(?=..)
lookahead, to match without consuming input
$endgroup$
$begingroup$
-9 bytes with/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO
$endgroup$
– Grimy
20 hours ago
1
$begingroup$
Down to 46:/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO
$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
add a comment |
$begingroup$
Java 8, 169 145 bytes
m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}
-24 bytes thanks to @OlivierGrégoire.
Uses 0
instead of A
and 1
instead of B
, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.
The cells are checked the same as in my answer for the All the single eights challenge.
Try it online.
Explanation:
m->{ // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)// Loop over the rows
for(j=m[i].length;j-->0;) // Inner loop over the columns
for(k=9;m[i][j]==1& // If the current cell contains a 1:
k-->0;) // Inner loop `k` in the range (9, 0]:
try{m[i][j]= // Set the current cell to:
m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
// Else-if `k` is 6, 7, or 8: Look at the next row
// Else (`k` is 3, 4, or 5): Look at the current row
[j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
// Else-if `k` is 2, 5, or 8: Look at the next column
// Else (`k` is 1, 4, or 7): Look at the current column
<1? // And if this cell contains a 0:
2 // Change the current cell from a 1 to a 2
: // Else:
1; // Leave it a 1
}catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
// (try-catch saves bytes in comparison to if-checks)
$endgroup$
1
$begingroup$
I haven't checked much, but is there anything wrong withm[i+k/3-1][j+k%3-1]
? 145 bytes
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
add a comment |
$begingroup$
Javascript, 126 118 bytes
_=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))
Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.
$endgroup$
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
add a comment |
$begingroup$
Ruby, 102 bytes
->a{a+=?.*s=a.size
(s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}
Try it online!
input/output as a newline separated string
$endgroup$
add a comment |
$begingroup$
Python 2, 123 119 bytes
lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate
Try it online!
I/O is a list of lists
$endgroup$
add a comment |
$begingroup$
05AB1E, 29 bytes
_2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+
Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.
Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012
for AB+
respectively.
Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)
Explanation:
_ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
2F # Loop `n` 2 times in the range [0, 2):
Iн # Take the input-matrix, and only leave the first inner list of 0s
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
¸ # Wrap it into a list
# i.e. [0,0,0,0] → [[0,0,0,0]]
.ø # Surround the inverted input with the list of 0s
# i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
# → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
V # Pop and store it in variable `Y`
g # Take the length of the (implicit) input-matrix
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
N+ # Add `n` to it
# i.e. 4 and n=0 → 4
# i.e. 4 and n=1 → 5
F # Inner loop `N` in the range [0, length+`n`):
Y # Push matrix `Y`
N._ # Rotate it `N` times towards the left
# i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
3£ # And only leave the first three inner lists
# i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
} # After the inner loop:
) # Wrap everything on the stack into a list
# → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
€ø # Zip/transpose (swapping rows/columns) each matrix in the list
# → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
O # And take the sum of each inner list
# → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
ø # Zip/transpose; swapping rows/columns the entire matrix again
# i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
# → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
} # After the outer loop:
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
* # Multiple each value with the input-matrix at the same positions,
# which implicitly removes the trailing values
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
Ā # Truthify each value (0 remains 0; everything else becomes 1)
# i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
# → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
+ # Then add each value with the input-matrix at the same positions
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
# (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
JavaScript, 85 bytes
Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.
Input and output is as an array of digit arrays, using 3
for Astan, 0
for Blandia & 1
for the trench.
a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])
Try it online (For convenience, maps from & back to the I/O format used in the challenge)
$endgroup$
add a comment |
$begingroup$
TSQL, 252 bytes
Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.
Golfed:
WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @
Ungolfed:
DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';
WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @
Try it out
TSQL, 127 bytes(Using table variable as input)
Execute this script in management studio - use "query"-"result to text" to make it readable
--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'
DECLARE @ table(x int, r int, c int, v char)
INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number
-- query(127 characters)
SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z
Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method
$endgroup$
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178809%2fdig-a-border-trench%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 15 bytes
2#-#~Erosion~1&
Try it online!
Or (39 bytes):
MorphologicalPerimeter[#,Padding->1]+#&
Try it online!
What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}
.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 15 bytes
2#-#~Erosion~1&
Try it online!
Or (39 bytes):
MorphologicalPerimeter[#,Padding->1]+#&
Try it online!
What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}
.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 15 bytes
2#-#~Erosion~1&
Try it online!
Or (39 bytes):
MorphologicalPerimeter[#,Padding->1]+#&
Try it online!
What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}
.
$endgroup$
Wolfram Language (Mathematica), 15 bytes
2#-#~Erosion~1&
Try it online!
Or (39 bytes):
MorphologicalPerimeter[#,Padding->1]+#&
Try it online!
What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}
.
edited 2 days ago
answered 2 days ago
lirtosiastlirtosiast
16.1k437108
16.1k437108
add a comment |
add a comment |
$begingroup$
K (ngn/k), 23 bytes
{x+x&2{++/'3'0,x,0}/~x}
Try it online!
uses 0 1 2
for "AB+"
{
}
function with argument x
~
logical not
2{
}/
twice do
0,x,0
surround with 0-s (top and bottom of the matrix)3'
triples of consecutive rows+/'
sum each+
transpose
x&
logical and of x
with
x+
add x
to
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 23 bytes
{x+x&2{++/'3'0,x,0}/~x}
Try it online!
uses 0 1 2
for "AB+"
{
}
function with argument x
~
logical not
2{
}/
twice do
0,x,0
surround with 0-s (top and bottom of the matrix)3'
triples of consecutive rows+/'
sum each+
transpose
x&
logical and of x
with
x+
add x
to
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 23 bytes
{x+x&2{++/'3'0,x,0}/~x}
Try it online!
uses 0 1 2
for "AB+"
{
}
function with argument x
~
logical not
2{
}/
twice do
0,x,0
surround with 0-s (top and bottom of the matrix)3'
triples of consecutive rows+/'
sum each+
transpose
x&
logical and of x
with
x+
add x
to
$endgroup$
K (ngn/k), 23 bytes
{x+x&2{++/'3'0,x,0}/~x}
Try it online!
uses 0 1 2
for "AB+"
{
}
function with argument x
~
logical not
2{
}/
twice do
0,x,0
surround with 0-s (top and bottom of the matrix)3'
triples of consecutive rows+/'
sum each+
transpose
x&
logical and of x
with
x+
add x
to
edited 2 days ago
answered 2 days ago
ngnngn
7,06112559
7,06112559
add a comment |
add a comment |
$begingroup$
MATL, 11 8 bytes
Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.
EG9&3ZI-
The input is a matrix with Astan represented by 0
and Blandia by 1
. Trench is represented in the output by 2
.
Try it online!
How it works
E % Implicit input. Multiply by 2, element-wise
G % Push input again
9 % Push 9
&3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
- % Subtract, element-wise. Implicit display
$endgroup$
add a comment |
$begingroup$
MATL, 11 8 bytes
Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.
EG9&3ZI-
The input is a matrix with Astan represented by 0
and Blandia by 1
. Trench is represented in the output by 2
.
Try it online!
How it works
E % Implicit input. Multiply by 2, element-wise
G % Push input again
9 % Push 9
&3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
- % Subtract, element-wise. Implicit display
$endgroup$
add a comment |
$begingroup$
MATL, 11 8 bytes
Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.
EG9&3ZI-
The input is a matrix with Astan represented by 0
and Blandia by 1
. Trench is represented in the output by 2
.
Try it online!
How it works
E % Implicit input. Multiply by 2, element-wise
G % Push input again
9 % Push 9
&3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
- % Subtract, element-wise. Implicit display
$endgroup$
MATL, 11 8 bytes
Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.
EG9&3ZI-
The input is a matrix with Astan represented by 0
and Blandia by 1
. Trench is represented in the output by 2
.
Try it online!
How it works
E % Implicit input. Multiply by 2, element-wise
G % Push input again
9 % Push 9
&3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
- % Subtract, element-wise. Implicit display
edited 2 days ago
answered 2 days ago
Luis MendoLuis Mendo
74.1k886291
74.1k886291
add a comment |
add a comment |
$begingroup$
JavaScript (ES7), 84 82 bytes
Saved 2 bytes thanks to @Shaggy
Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.
a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()
Try it online!
Commented
a => ( // a = input matrix
g = x => // g = function taking x (initially undefined)
a.map(t = // initialize t to a non-numeric value
(r, Y) => // for each row r at position Y in a:
r.map((v, X) => // for each value v at position X in r:
1 / x ? // if x is defined (this is a recursive call):
t |= // set the flag t if:
(x - X) ** 2 + // the squared Euclidean distance
(y - Y) ** 2 // between (x, y) and (X, Y)
< v // is less than v (3 = Astan, 0 = Blandia)
: // else (this is the initial call to g):
v || // yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
| t // and yield the flag t (0 = no change, 1 = trench)
) // end of inner map()
) // end of outer map()
)() // initial call to g
$endgroup$
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
add a comment |
$begingroup$
JavaScript (ES7), 84 82 bytes
Saved 2 bytes thanks to @Shaggy
Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.
a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()
Try it online!
Commented
a => ( // a = input matrix
g = x => // g = function taking x (initially undefined)
a.map(t = // initialize t to a non-numeric value
(r, Y) => // for each row r at position Y in a:
r.map((v, X) => // for each value v at position X in r:
1 / x ? // if x is defined (this is a recursive call):
t |= // set the flag t if:
(x - X) ** 2 + // the squared Euclidean distance
(y - Y) ** 2 // between (x, y) and (X, Y)
< v // is less than v (3 = Astan, 0 = Blandia)
: // else (this is the initial call to g):
v || // yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
| t // and yield the flag t (0 = no change, 1 = trench)
) // end of inner map()
) // end of outer map()
)() // initial call to g
$endgroup$
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
add a comment |
$begingroup$
JavaScript (ES7), 84 82 bytes
Saved 2 bytes thanks to @Shaggy
Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.
a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()
Try it online!
Commented
a => ( // a = input matrix
g = x => // g = function taking x (initially undefined)
a.map(t = // initialize t to a non-numeric value
(r, Y) => // for each row r at position Y in a:
r.map((v, X) => // for each value v at position X in r:
1 / x ? // if x is defined (this is a recursive call):
t |= // set the flag t if:
(x - X) ** 2 + // the squared Euclidean distance
(y - Y) ** 2 // between (x, y) and (X, Y)
< v // is less than v (3 = Astan, 0 = Blandia)
: // else (this is the initial call to g):
v || // yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
| t // and yield the flag t (0 = no change, 1 = trench)
) // end of inner map()
) // end of outer map()
)() // initial call to g
$endgroup$
JavaScript (ES7), 84 82 bytes
Saved 2 bytes thanks to @Shaggy
Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.
a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()
Try it online!
Commented
a => ( // a = input matrix
g = x => // g = function taking x (initially undefined)
a.map(t = // initialize t to a non-numeric value
(r, Y) => // for each row r at position Y in a:
r.map((v, X) => // for each value v at position X in r:
1 / x ? // if x is defined (this is a recursive call):
t |= // set the flag t if:
(x - X) ** 2 + // the squared Euclidean distance
(y - Y) ** 2 // between (x, y) and (X, Y)
< v // is less than v (3 = Astan, 0 = Blandia)
: // else (this is the initial call to g):
v || // yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
| t // and yield the flag t (0 = no change, 1 = trench)
) // end of inner map()
) // end of outer map()
)() // initial call to g
edited yesterday
answered 2 days ago
ArnauldArnauld
73.5k689309
73.5k689309
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
add a comment |
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
$begingroup$
82 bytes?
$endgroup$
– Shaggy
2 days ago
4
4
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
@Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
$endgroup$
– Arnauld
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
$begingroup$
I was only thinking the same thing earlier!
$endgroup$
– Shaggy
2 days ago
add a comment |
$begingroup$
Octave, 37 31 26 bytes
This function performs a morphological erosion on the Astan (1-b
) part of the "image" using conv2
imerode
, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!
@(b)2*b-imerode(b,ones(3))
Try it online!
$endgroup$
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
add a comment |
$begingroup$
Octave, 37 31 26 bytes
This function performs a morphological erosion on the Astan (1-b
) part of the "image" using conv2
imerode
, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!
@(b)2*b-imerode(b,ones(3))
Try it online!
$endgroup$
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
add a comment |
$begingroup$
Octave, 37 31 26 bytes
This function performs a morphological erosion on the Astan (1-b
) part of the "image" using conv2
imerode
, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!
@(b)2*b-imerode(b,ones(3))
Try it online!
$endgroup$
Octave, 37 31 26 bytes
This function performs a morphological erosion on the Astan (1-b
) part of the "image" using conv2
imerode
, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!
@(b)2*b-imerode(b,ones(3))
Try it online!
edited yesterday
answered 2 days ago
flawrflawr
26.7k665188
26.7k665188
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
add a comment |
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
2
2
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
-1 for no convolution :-P
$endgroup$
– Luis Mendo
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
@LuisMendo An earlier version did include a convolution:)
$endgroup$
– flawr
2 days ago
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
$begingroup$
Thanks, updated!
$endgroup$
– flawr
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 11 bytesSBCS
⊢⌈{2∊⍵}⌺3 3
this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊
in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output
Try it online!
represents 'AB+'
as 2 0 1
respectively
{
}⌺3 3
apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s
2∊⍵
is a 2 present in the argument? return a 0/1 boolean
⊢⌈
per-element max of that and the original matrix
$endgroup$
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias fordisplay
which i forgot to remove. removed now
$endgroup$
– ngn
2 days ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 11 bytesSBCS
⊢⌈{2∊⍵}⌺3 3
this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊
in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output
Try it online!
represents 'AB+'
as 2 0 1
respectively
{
}⌺3 3
apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s
2∊⍵
is a 2 present in the argument? return a 0/1 boolean
⊢⌈
per-element max of that and the original matrix
$endgroup$
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias fordisplay
which i forgot to remove. removed now
$endgroup$
– ngn
2 days ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 11 bytesSBCS
⊢⌈{2∊⍵}⌺3 3
this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊
in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output
Try it online!
represents 'AB+'
as 2 0 1
respectively
{
}⌺3 3
apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s
2∊⍵
is a 2 present in the argument? return a 0/1 boolean
⊢⌈
per-element max of that and the original matrix
$endgroup$
APL (Dyalog Unicode), 11 bytesSBCS
⊢⌈{2∊⍵}⌺3 3
this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊
in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output
Try it online!
represents 'AB+'
as 2 0 1
respectively
{
}⌺3 3
apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s
2∊⍵
is a 2 present in the argument? return a 0/1 boolean
⊢⌈
per-element max of that and the original matrix
edited 2 days ago
answered 2 days ago
ngnngn
7,06112559
7,06112559
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias fordisplay
which i forgot to remove. removed now
$endgroup$
– ngn
2 days ago
add a comment |
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias fordisplay
which i forgot to remove. removed now
$endgroup$
– ngn
2 days ago
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
Of course, switching to Stencil would save you more than half of your bytes.
$endgroup$
– Adám
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias for
display
which i forgot to remove. removed now$endgroup$
– ngn
2 days ago
$begingroup$
@Adám an alias for
display
which i forgot to remove. removed now$endgroup$
– ngn
2 days ago
add a comment |
$begingroup$
Charcoal, 20 bytes
≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι
Try it online! Link is to verbose version of code. Explanation:
≔⪫θ⸿θ
Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.
Pθ
Print the input string without moving the cursor.
Fθ
Loop over each character of the input string.
⎇∧№KMA⁼Bι
If the Moore neighbourhood contains an A
, and the current character is a B
...
+
... then overwrite the B
with a +
...
ι
... otherwise print the current character (or move to the next line if the current character is a carriage return).
$endgroup$
add a comment |
$begingroup$
Charcoal, 20 bytes
≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι
Try it online! Link is to verbose version of code. Explanation:
≔⪫θ⸿θ
Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.
Pθ
Print the input string without moving the cursor.
Fθ
Loop over each character of the input string.
⎇∧№KMA⁼Bι
If the Moore neighbourhood contains an A
, and the current character is a B
...
+
... then overwrite the B
with a +
...
ι
... otherwise print the current character (or move to the next line if the current character is a carriage return).
$endgroup$
add a comment |
$begingroup$
Charcoal, 20 bytes
≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι
Try it online! Link is to verbose version of code. Explanation:
≔⪫θ⸿θ
Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.
Pθ
Print the input string without moving the cursor.
Fθ
Loop over each character of the input string.
⎇∧№KMA⁼Bι
If the Moore neighbourhood contains an A
, and the current character is a B
...
+
... then overwrite the B
with a +
...
ι
... otherwise print the current character (or move to the next line if the current character is a carriage return).
$endgroup$
Charcoal, 20 bytes
≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι
Try it online! Link is to verbose version of code. Explanation:
≔⪫θ⸿θ
Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.
Pθ
Print the input string without moving the cursor.
Fθ
Loop over each character of the input string.
⎇∧№KMA⁼Bι
If the Moore neighbourhood contains an A
, and the current character is a B
...
+
... then overwrite the B
with a +
...
ι
... otherwise print the current character (or move to the next line if the current character is a carriage return).
answered 2 days ago
NeilNeil
79.9k744177
79.9k744177
add a comment |
add a comment |
$begingroup$
J, 28 bytes
>.3 3(2 e.,);._3(0|:@,|.)^:4
Try it online!
'AB+'
-> 2 0 1
Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
>.3 3(2 e.,);._3(0|:@,|.)^:4
Try it online!
'AB+'
-> 2 0 1
Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
>.3 3(2 e.,);._3(0|:@,|.)^:4
Try it online!
'AB+'
-> 2 0 1
Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...
$endgroup$
J, 28 bytes
>.3 3(2 e.,);._3(0|:@,|.)^:4
Try it online!
'AB+'
-> 2 0 1
Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...
answered yesterday
Galen IvanovGalen Ivanov
6,52711032
6,52711032
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 92 80 bytes
(?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
a
iT`Ba`+`.?a.?
Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any B
s immediately above or below A
s are turned into a
s. This then reduces the problem to checking B
s to the left or right of A
s or a
s. The a
s themselves also need to get turned into +
s of course, but fortunately the i
flag to T
only affects the regex match, not the actual transliteration, so the A
s remain unaffected.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 92 80 bytes
(?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
a
iT`Ba`+`.?a.?
Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any B
s immediately above or below A
s are turned into a
s. This then reduces the problem to checking B
s to the left or right of A
s or a
s. The a
s themselves also need to get turned into +
s of course, but fortunately the i
flag to T
only affects the regex match, not the actual transliteration, so the A
s remain unaffected.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 92 80 bytes
(?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
a
iT`Ba`+`.?a.?
Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any B
s immediately above or below A
s are turned into a
s. This then reduces the problem to checking B
s to the left or right of A
s or a
s. The a
s themselves also need to get turned into +
s of course, but fortunately the i
flag to T
only affects the regex match, not the actual transliteration, so the A
s remain unaffected.
$endgroup$
Retina 0.8.2, 92 80 bytes
(?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
a
iT`Ba`+`.?a.?
Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any B
s immediately above or below A
s are turned into a
s. This then reduces the problem to checking B
s to the left or right of A
s or a
s. The a
s themselves also need to get turned into +
s of course, but fortunately the i
flag to T
only affects the regex match, not the actual transliteration, so the A
s remain unaffected.
edited 2 days ago
answered 2 days ago
NeilNeil
79.9k744177
79.9k744177
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 187 bytes
a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))
Instead of chaining Take()
s, Skip()
s, and Select()
s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 187 bytes
a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))
Instead of chaining Take()
s, Skip()
s, and Select()
s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 187 bytes
a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))
Instead of chaining Take()
s, Skip()
s, and Select()
s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 187 bytes
a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))
Instead of chaining Take()
s, Skip()
s, and Select()
s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!
Try it online!
edited yesterday
answered 2 days ago
Embodiment of IgnoranceEmbodiment of Ignorance
671115
671115
add a comment |
add a comment |
$begingroup$
Perl 5, 58 46 bytes
$m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo
TIO
-12 bytes thanks to @Grimy
/.
/;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
TIO
-p
like-n
but print also
-00
paragraph mode- to get the width-1
/.n/
matches the last character of first line
@{-}
special array the position of start of match of previous matched groups, coerced as string (first element)
s/../+/s&&redo
replace match by+
while matches
/s
flag, so that.
matches also newline character
A(|.{@{-}}.?.?)KB
matches
AB
orA
followed by (width-1) to (width+1) characters folowed byB
K
to keep the left ofB
unchanged
B(?=(?1)A)
,
(?1)
dirverting recursive, to reference previous expression(|.{$m,$o})
(?=..)
lookahead, to match without consuming input
$endgroup$
$begingroup$
-9 bytes with/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO
$endgroup$
– Grimy
20 hours ago
1
$begingroup$
Down to 46:/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO
$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
add a comment |
$begingroup$
Perl 5, 58 46 bytes
$m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo
TIO
-12 bytes thanks to @Grimy
/.
/;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
TIO
-p
like-n
but print also
-00
paragraph mode- to get the width-1
/.n/
matches the last character of first line
@{-}
special array the position of start of match of previous matched groups, coerced as string (first element)
s/../+/s&&redo
replace match by+
while matches
/s
flag, so that.
matches also newline character
A(|.{@{-}}.?.?)KB
matches
AB
orA
followed by (width-1) to (width+1) characters folowed byB
K
to keep the left ofB
unchanged
B(?=(?1)A)
,
(?1)
dirverting recursive, to reference previous expression(|.{$m,$o})
(?=..)
lookahead, to match without consuming input
$endgroup$
$begingroup$
-9 bytes with/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO
$endgroup$
– Grimy
20 hours ago
1
$begingroup$
Down to 46:/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO
$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
add a comment |
$begingroup$
Perl 5, 58 46 bytes
$m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo
TIO
-12 bytes thanks to @Grimy
/.
/;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
TIO
-p
like-n
but print also
-00
paragraph mode- to get the width-1
/.n/
matches the last character of first line
@{-}
special array the position of start of match of previous matched groups, coerced as string (first element)
s/../+/s&&redo
replace match by+
while matches
/s
flag, so that.
matches also newline character
A(|.{@{-}}.?.?)KB
matches
AB
orA
followed by (width-1) to (width+1) characters folowed byB
K
to keep the left ofB
unchanged
B(?=(?1)A)
,
(?1)
dirverting recursive, to reference previous expression(|.{$m,$o})
(?=..)
lookahead, to match without consuming input
$endgroup$
Perl 5, 58 46 bytes
$m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo
TIO
-12 bytes thanks to @Grimy
/.
/;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
TIO
-p
like-n
but print also
-00
paragraph mode- to get the width-1
/.n/
matches the last character of first line
@{-}
special array the position of start of match of previous matched groups, coerced as string (first element)
s/../+/s&&redo
replace match by+
while matches
/s
flag, so that.
matches also newline character
A(|.{@{-}}.?.?)KB
matches
AB
orA
followed by (width-1) to (width+1) characters folowed byB
K
to keep the left ofB
unchanged
B(?=(?1)A)
,
(?1)
dirverting recursive, to reference previous expression(|.{$m,$o})
(?=..)
lookahead, to match without consuming input
edited 19 hours ago
answered yesterday
Nahuel FouilleulNahuel Fouilleul
1,68228
1,68228
$begingroup$
-9 bytes with/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO
$endgroup$
– Grimy
20 hours ago
1
$begingroup$
Down to 46:/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO
$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
add a comment |
$begingroup$
-9 bytes with/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO
$endgroup$
– Grimy
20 hours ago
1
$begingroup$
Down to 46:/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO
$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
$begingroup$
-9 bytes with
/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO$endgroup$
– Grimy
20 hours ago
$begingroup$
-9 bytes with
/. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s
(literal newline in the first regex). TIO$endgroup$
– Grimy
20 hours ago
1
1
$begingroup$
Down to 46:
/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO$endgroup$
– Grimy
20 hours ago
$begingroup$
Down to 46:
/. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo
. TIO$endgroup$
– Grimy
20 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
$begingroup$
thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
$endgroup$
– Nahuel Fouilleul
19 hours ago
add a comment |
$begingroup$
Java 8, 169 145 bytes
m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}
-24 bytes thanks to @OlivierGrégoire.
Uses 0
instead of A
and 1
instead of B
, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.
The cells are checked the same as in my answer for the All the single eights challenge.
Try it online.
Explanation:
m->{ // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)// Loop over the rows
for(j=m[i].length;j-->0;) // Inner loop over the columns
for(k=9;m[i][j]==1& // If the current cell contains a 1:
k-->0;) // Inner loop `k` in the range (9, 0]:
try{m[i][j]= // Set the current cell to:
m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
// Else-if `k` is 6, 7, or 8: Look at the next row
// Else (`k` is 3, 4, or 5): Look at the current row
[j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
// Else-if `k` is 2, 5, or 8: Look at the next column
// Else (`k` is 1, 4, or 7): Look at the current column
<1? // And if this cell contains a 0:
2 // Change the current cell from a 1 to a 2
: // Else:
1; // Leave it a 1
}catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
// (try-catch saves bytes in comparison to if-checks)
$endgroup$
1
$begingroup$
I haven't checked much, but is there anything wrong withm[i+k/3-1][j+k%3-1]
? 145 bytes
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
add a comment |
$begingroup$
Java 8, 169 145 bytes
m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}
-24 bytes thanks to @OlivierGrégoire.
Uses 0
instead of A
and 1
instead of B
, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.
The cells are checked the same as in my answer for the All the single eights challenge.
Try it online.
Explanation:
m->{ // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)// Loop over the rows
for(j=m[i].length;j-->0;) // Inner loop over the columns
for(k=9;m[i][j]==1& // If the current cell contains a 1:
k-->0;) // Inner loop `k` in the range (9, 0]:
try{m[i][j]= // Set the current cell to:
m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
// Else-if `k` is 6, 7, or 8: Look at the next row
// Else (`k` is 3, 4, or 5): Look at the current row
[j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
// Else-if `k` is 2, 5, or 8: Look at the next column
// Else (`k` is 1, 4, or 7): Look at the current column
<1? // And if this cell contains a 0:
2 // Change the current cell from a 1 to a 2
: // Else:
1; // Leave it a 1
}catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
// (try-catch saves bytes in comparison to if-checks)
$endgroup$
1
$begingroup$
I haven't checked much, but is there anything wrong withm[i+k/3-1][j+k%3-1]
? 145 bytes
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
add a comment |
$begingroup$
Java 8, 169 145 bytes
m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}
-24 bytes thanks to @OlivierGrégoire.
Uses 0
instead of A
and 1
instead of B
, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.
The cells are checked the same as in my answer for the All the single eights challenge.
Try it online.
Explanation:
m->{ // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)// Loop over the rows
for(j=m[i].length;j-->0;) // Inner loop over the columns
for(k=9;m[i][j]==1& // If the current cell contains a 1:
k-->0;) // Inner loop `k` in the range (9, 0]:
try{m[i][j]= // Set the current cell to:
m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
// Else-if `k` is 6, 7, or 8: Look at the next row
// Else (`k` is 3, 4, or 5): Look at the current row
[j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
// Else-if `k` is 2, 5, or 8: Look at the next column
// Else (`k` is 1, 4, or 7): Look at the current column
<1? // And if this cell contains a 0:
2 // Change the current cell from a 1 to a 2
: // Else:
1; // Leave it a 1
}catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
// (try-catch saves bytes in comparison to if-checks)
$endgroup$
Java 8, 169 145 bytes
m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}
-24 bytes thanks to @OlivierGrégoire.
Uses 0
instead of A
and 1
instead of B
, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.
The cells are checked the same as in my answer for the All the single eights challenge.
Try it online.
Explanation:
m->{ // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)// Loop over the rows
for(j=m[i].length;j-->0;) // Inner loop over the columns
for(k=9;m[i][j]==1& // If the current cell contains a 1:
k-->0;) // Inner loop `k` in the range (9, 0]:
try{m[i][j]= // Set the current cell to:
m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
// Else-if `k` is 6, 7, or 8: Look at the next row
// Else (`k` is 3, 4, or 5): Look at the current row
[j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
// Else-if `k` is 2, 5, or 8: Look at the next column
// Else (`k` is 1, 4, or 7): Look at the current column
<1? // And if this cell contains a 0:
2 // Change the current cell from a 1 to a 2
: // Else:
1; // Leave it a 1
}catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
// (try-catch saves bytes in comparison to if-checks)
edited 18 hours ago
answered yesterday
Kevin CruijssenKevin Cruijssen
36.5k555192
36.5k555192
1
$begingroup$
I haven't checked much, but is there anything wrong withm[i+k/3-1][j+k%3-1]
? 145 bytes
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
add a comment |
1
$begingroup$
I haven't checked much, but is there anything wrong withm[i+k/3-1][j+k%3-1]
? 145 bytes
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
1
1
$begingroup$
I haven't checked much, but is there anything wrong with
m[i+k/3-1][j+k%3-1]
? 145 bytes$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
I haven't checked much, but is there anything wrong with
m[i+k/3-1][j+k%3-1]
? 145 bytes$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
@OlivierGrégoire Dang, that's so much easier.. Thanks!
$endgroup$
– Kevin Cruijssen
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
I think it's also valid for your answers of previous challenges given that they seem to have the same structure
$endgroup$
– Olivier Grégoire
18 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
$begingroup$
@OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
$endgroup$
– Kevin Cruijssen
17 hours ago
add a comment |
$begingroup$
Javascript, 126 118 bytes
_=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))
Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.
$endgroup$
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
add a comment |
$begingroup$
Javascript, 126 118 bytes
_=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))
Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.
$endgroup$
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
add a comment |
$begingroup$
Javascript, 126 118 bytes
_=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))
Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.
$endgroup$
Javascript, 126 118 bytes
_=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))
Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.
edited 15 hours ago
answered yesterday
M DirrM Dirr
212
212
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
add a comment |
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
I think this works for 120 bytes.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
Or 116 bytes returning an array of character arrays.
$endgroup$
– Shaggy
18 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
$begingroup$
@Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
$endgroup$
– M Dirr
15 hours ago
add a comment |
$begingroup$
Ruby, 102 bytes
->a{a+=?.*s=a.size
(s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}
Try it online!
input/output as a newline separated string
$endgroup$
add a comment |
$begingroup$
Ruby, 102 bytes
->a{a+=?.*s=a.size
(s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}
Try it online!
input/output as a newline separated string
$endgroup$
add a comment |
$begingroup$
Ruby, 102 bytes
->a{a+=?.*s=a.size
(s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}
Try it online!
input/output as a newline separated string
$endgroup$
Ruby, 102 bytes
->a{a+=?.*s=a.size
(s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}
Try it online!
input/output as a newline separated string
answered 2 days ago
Level River StLevel River St
20.2k32579
20.2k32579
add a comment |
add a comment |
$begingroup$
Python 2, 123 119 bytes
lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate
Try it online!
I/O is a list of lists
$endgroup$
add a comment |
$begingroup$
Python 2, 123 119 bytes
lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate
Try it online!
I/O is a list of lists
$endgroup$
add a comment |
$begingroup$
Python 2, 123 119 bytes
lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate
Try it online!
I/O is a list of lists
$endgroup$
Python 2, 123 119 bytes
lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate
Try it online!
I/O is a list of lists
edited yesterday
answered yesterday
TFeldTFeld
14.6k21241
14.6k21241
add a comment |
add a comment |
$begingroup$
05AB1E, 29 bytes
_2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+
Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.
Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012
for AB+
respectively.
Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)
Explanation:
_ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
2F # Loop `n` 2 times in the range [0, 2):
Iн # Take the input-matrix, and only leave the first inner list of 0s
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
¸ # Wrap it into a list
# i.e. [0,0,0,0] → [[0,0,0,0]]
.ø # Surround the inverted input with the list of 0s
# i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
# → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
V # Pop and store it in variable `Y`
g # Take the length of the (implicit) input-matrix
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
N+ # Add `n` to it
# i.e. 4 and n=0 → 4
# i.e. 4 and n=1 → 5
F # Inner loop `N` in the range [0, length+`n`):
Y # Push matrix `Y`
N._ # Rotate it `N` times towards the left
# i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
3£ # And only leave the first three inner lists
# i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
} # After the inner loop:
) # Wrap everything on the stack into a list
# → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
€ø # Zip/transpose (swapping rows/columns) each matrix in the list
# → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
O # And take the sum of each inner list
# → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
ø # Zip/transpose; swapping rows/columns the entire matrix again
# i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
# → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
} # After the outer loop:
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
* # Multiple each value with the input-matrix at the same positions,
# which implicitly removes the trailing values
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
Ā # Truthify each value (0 remains 0; everything else becomes 1)
# i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
# → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
+ # Then add each value with the input-matrix at the same positions
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
# (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 29 bytes
_2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+
Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.
Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012
for AB+
respectively.
Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)
Explanation:
_ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
2F # Loop `n` 2 times in the range [0, 2):
Iн # Take the input-matrix, and only leave the first inner list of 0s
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
¸ # Wrap it into a list
# i.e. [0,0,0,0] → [[0,0,0,0]]
.ø # Surround the inverted input with the list of 0s
# i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
# → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
V # Pop and store it in variable `Y`
g # Take the length of the (implicit) input-matrix
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
N+ # Add `n` to it
# i.e. 4 and n=0 → 4
# i.e. 4 and n=1 → 5
F # Inner loop `N` in the range [0, length+`n`):
Y # Push matrix `Y`
N._ # Rotate it `N` times towards the left
# i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
3£ # And only leave the first three inner lists
# i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
} # After the inner loop:
) # Wrap everything on the stack into a list
# → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
€ø # Zip/transpose (swapping rows/columns) each matrix in the list
# → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
O # And take the sum of each inner list
# → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
ø # Zip/transpose; swapping rows/columns the entire matrix again
# i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
# → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
} # After the outer loop:
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
* # Multiple each value with the input-matrix at the same positions,
# which implicitly removes the trailing values
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
Ā # Truthify each value (0 remains 0; everything else becomes 1)
# i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
# → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
+ # Then add each value with the input-matrix at the same positions
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
# (and output the result implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 29 bytes
_2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+
Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.
Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012
for AB+
respectively.
Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)
Explanation:
_ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
2F # Loop `n` 2 times in the range [0, 2):
Iн # Take the input-matrix, and only leave the first inner list of 0s
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
¸ # Wrap it into a list
# i.e. [0,0,0,0] → [[0,0,0,0]]
.ø # Surround the inverted input with the list of 0s
# i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
# → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
V # Pop and store it in variable `Y`
g # Take the length of the (implicit) input-matrix
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
N+ # Add `n` to it
# i.e. 4 and n=0 → 4
# i.e. 4 and n=1 → 5
F # Inner loop `N` in the range [0, length+`n`):
Y # Push matrix `Y`
N._ # Rotate it `N` times towards the left
# i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
3£ # And only leave the first three inner lists
# i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
} # After the inner loop:
) # Wrap everything on the stack into a list
# → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
€ø # Zip/transpose (swapping rows/columns) each matrix in the list
# → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
O # And take the sum of each inner list
# → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
ø # Zip/transpose; swapping rows/columns the entire matrix again
# i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
# → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
} # After the outer loop:
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
* # Multiple each value with the input-matrix at the same positions,
# which implicitly removes the trailing values
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
Ā # Truthify each value (0 remains 0; everything else becomes 1)
# i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
# → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
+ # Then add each value with the input-matrix at the same positions
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
# (and output the result implicitly)
$endgroup$
05AB1E, 29 bytes
_2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+
Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.
Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012
for AB+
respectively.
Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)
Explanation:
_ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
2F # Loop `n` 2 times in the range [0, 2):
Iн # Take the input-matrix, and only leave the first inner list of 0s
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
¸ # Wrap it into a list
# i.e. [0,0,0,0] → [[0,0,0,0]]
.ø # Surround the inverted input with the list of 0s
# i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
# → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
V # Pop and store it in variable `Y`
g # Take the length of the (implicit) input-matrix
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
N+ # Add `n` to it
# i.e. 4 and n=0 → 4
# i.e. 4 and n=1 → 5
F # Inner loop `N` in the range [0, length+`n`):
Y # Push matrix `Y`
N._ # Rotate it `N` times towards the left
# i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
3£ # And only leave the first three inner lists
# i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
# → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
} # After the inner loop:
) # Wrap everything on the stack into a list
# → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
€ø # Zip/transpose (swapping rows/columns) each matrix in the list
# → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
O # And take the sum of each inner list
# → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
ø # Zip/transpose; swapping rows/columns the entire matrix again
# i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
# → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
} # After the outer loop:
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
* # Multiple each value with the input-matrix at the same positions,
# which implicitly removes the trailing values
# i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
Ā # Truthify each value (0 remains 0; everything else becomes 1)
# i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
# → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
+ # Then add each value with the input-matrix at the same positions
# i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
# and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
# → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
# (and output the result implicitly)
answered yesterday
Kevin CruijssenKevin Cruijssen
36.5k555192
36.5k555192
add a comment |
add a comment |
$begingroup$
JavaScript, 85 bytes
Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.
Input and output is as an array of digit arrays, using 3
for Astan, 0
for Blandia & 1
for the trench.
a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])
Try it online (For convenience, maps from & back to the I/O format used in the challenge)
$endgroup$
add a comment |
$begingroup$
JavaScript, 85 bytes
Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.
Input and output is as an array of digit arrays, using 3
for Astan, 0
for Blandia & 1
for the trench.
a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])
Try it online (For convenience, maps from & back to the I/O format used in the challenge)
$endgroup$
add a comment |
$begingroup$
JavaScript, 85 bytes
Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.
Input and output is as an array of digit arrays, using 3
for Astan, 0
for Blandia & 1
for the trench.
a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])
Try it online (For convenience, maps from & back to the I/O format used in the challenge)
$endgroup$
JavaScript, 85 bytes
Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.
Input and output is as an array of digit arrays, using 3
for Astan, 0
for Blandia & 1
for the trench.
a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])
Try it online (For convenience, maps from & back to the I/O format used in the challenge)
edited 18 hours ago
answered yesterday
ShaggyShaggy
19.3k21666
19.3k21666
add a comment |
add a comment |
$begingroup$
TSQL, 252 bytes
Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.
Golfed:
WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @
Ungolfed:
DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';
WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @
Try it out
TSQL, 127 bytes(Using table variable as input)
Execute this script in management studio - use "query"-"result to text" to make it readable
--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'
DECLARE @ table(x int, r int, c int, v char)
INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number
-- query(127 characters)
SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z
Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method
$endgroup$
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
add a comment |
$begingroup$
TSQL, 252 bytes
Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.
Golfed:
WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @
Ungolfed:
DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';
WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @
Try it out
TSQL, 127 bytes(Using table variable as input)
Execute this script in management studio - use "query"-"result to text" to make it readable
--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'
DECLARE @ table(x int, r int, c int, v char)
INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number
-- query(127 characters)
SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z
Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method
$endgroup$
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
add a comment |
$begingroup$
TSQL, 252 bytes
Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.
Golfed:
WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @
Ungolfed:
DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';
WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @
Try it out
TSQL, 127 bytes(Using table variable as input)
Execute this script in management studio - use "query"-"result to text" to make it readable
--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'
DECLARE @ table(x int, r int, c int, v char)
INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number
-- query(127 characters)
SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z
Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method
$endgroup$
TSQL, 252 bytes
Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.
Golfed:
WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @
Ungolfed:
DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';
WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @
Try it out
TSQL, 127 bytes(Using table variable as input)
Execute this script in management studio - use "query"-"result to text" to make it readable
--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'
DECLARE @ table(x int, r int, c int, v char)
INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number
-- query(127 characters)
SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z
Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method
edited 14 hours ago
answered 19 hours ago
t-clausen.dkt-clausen.dk
1,834314
1,834314
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
add a comment |
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
What makes you think that you can't take a table as argument?
$endgroup$
– Adám
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám not a bad idea to create the code using a table as argument as well - I will get right on it
$endgroup$
– t-clausen.dk
18 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
$begingroup$
@Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
$endgroup$
– t-clausen.dk
17 hours ago
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178809%2fdig-a-border-trench%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
17
$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
2 days ago
4
$begingroup$
-1 for that
<sup><sub><sup><sub><sup><sub><sup><sub>
:-P$endgroup$
– Luis Mendo
2 days ago
19
$begingroup$
python, 4 bytes:
pass
The plans to build a border trench lead to a government shutdown and nothing happens.$endgroup$
– TheEspinosa
2 days ago
3
$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
2 days ago
1
$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
yesterday