Skip to main content
The code should actually work now. (Also it's a byte shorter.)
Source Link
Not a tree
  • 3.2k
  • 1
  • 14
  • 24

Mathematica (version 9), 166165 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&""<>#&/@("o"MorphologicalComponents[#"o"MorphologicalTransform[MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#>"ConvexHull"],Max[#(1-#[[2,2]])CrossMatrix@1]&]+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#"o"MorphologicalTransform[MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" #,Max[#(1-#[[2,2]])CrossMatrix@1]&]+"#"# then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

[Edit, thanks to Greg Martin:] If we're allowed to use arrays of characters instead of lists of strings, we can trim this down to 145144 bytes:

f@m_:=m"o"MorphologicalTransform[MorphologicalComponents[#,Method->"ConvexHull"],Max[#(1-m[[2#[[2,2]])CrossMatrix@1;"o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#CrossMatrix@1]&]+"#"#/.{0->"."}&[#/.{"."->0,"#"->1}]&

Mathematica (version 9), 166 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&/@("o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" # then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

[Edit, thanks to Greg Martin:] If we're allowed to use arrays of characters instead of lists of strings, we can trim this down to 145 bytes:

f@m_:=m(1-m[[2,2]])CrossMatrix@1;"o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."}&[#/.{"."->0,"#"->1}]&

Mathematica (version 9), 165 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

""<>#&/@("o"MorphologicalTransform[MorphologicalComponents[#,Method->"ConvexHull"],Max[#(1-#[[2,2]])CrossMatrix@1]&]+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o"MorphologicalTransform[MorphologicalComponents[#,Method->"ConvexHull"],Max[#(1-#[[2,2]])CrossMatrix@1]&]+"#"# then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary. We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

[Edit, thanks to Greg Martin:] If we're allowed to use arrays of characters instead of lists of strings, we can trim this down to 144 bytes:

"o"MorphologicalTransform[MorphologicalComponents[#,Method->"ConvexHull"],Max[#(1-#[[2,2]])CrossMatrix@1]&]+"#"#/.{0->"."}&[#/.{"."->0,"#"->1}]&
Using different input and output formats cuts out 21 bytes
Source Link
Not a tree
  • 3.2k
  • 1
  • 14
  • 24

Mathematica (version 9), 166 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&/@("o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" # then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

[Edit, thanks to Greg Martin:] If we're allowed to use arrays of characters instead of lists of strings, we can trim this down to 145 bytes:

f@m_:=m(1-m[[2,2]])CrossMatrix@1;"o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."}&[#/.{"."->0,"#"->1}]&

Mathematica (version 9), 166 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&/@("o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" # then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

Mathematica (version 9), 166 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&/@("o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" # then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}

[Edit, thanks to Greg Martin:] If we're allowed to use arrays of characters instead of lists of strings, we can trim this down to 145 bytes:

f@m_:=m(1-m[[2,2]])CrossMatrix@1;"o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."}&[#/.{"."->0,"#"->1}]&
Source Link
Not a tree
  • 3.2k
  • 1
  • 14
  • 24

Mathematica (version 9), 166 bytes

The nice, short ConvexHullMesh function that Greg Martin used was only introduced in Mathematica version 10, so I thought I'd make an attempt without it, using my ancient Mathematica version 9. I managed to get it slightly shorter, though! It's a function that takes and returns a list of strings (with ., # and o as the symbols).

f@m_:=m(1-m[[2,2]])CrossMatrix@1;""<>#&/@("o"MorphologicalComponents[#,Method->"ConvexHull"]~MorphologicalTransform~f+"#"#/.{0->"."})&[Characters@#/.{"."->0,"#"->1}]&

Explanation:

  • First, Characters@# /. {"."->0, "#"->1} turns the input into a matrix of 0s and 1s.
  • "o" MorphologicalComponents[#, Method->"ConvexHull"] ~MorphologicalTransform~ f + "#" # then uses Mathematica's powerful (but extremely byte-heavy…) image processing capabilities to first fill in the island's convex hull (which is the shape you'd get if you stretched a piece of string around it), and then take its boundary (using the function f@m_:=m(1-m[[2,2]])CrossMatrix@1). We then multiply this matrix by the string "o" to get a matrix of 0s and "o"s (thanks to Mathematica's impressive adaptability about types), and add this to "#" times the matrix of the island.
  • Finally, ""<>#& /@ (... /. {0->"."}) turns this matrix of "o"s, "#"s and 0s into a matrix of "o"s, "#"s and "."s, and joins each row into a string.

When we test this on example B, we get the output

{"....oo..",
 "...o##o.",
 "..o####o",
 ".o###..o",
 "o#####o.",
 "o#####o.",
 ".o##oo..",
 "..oo...."}