Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

75%
+4 −0
Challenges Coat of Many Colours

JavaScript (Node.js), 153 147 bytes -6 bytes thanks to Shaggy! a=>a.sort((x,y)=>g(x)-g(y),g=x=>"y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l ru g pu w p o b".split` `.find...

posted 3y ago by Moshi‭  ·  edited 3y ago by Moshi‭

Answer
#3: Post edited by user avatar Moshi‭ · 2021-08-30T23:47:43Z (over 3 years ago)
  • # [JavaScript (Node.js)], 153 bytes
  • ```
  • (a,g=x=>"re y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l r g pu w p o b".split` `.findIndex(t=>x.startsWith(t)))=>a.sort((x,y)=>g(x)-g(y))
  • ```
  • [Try it online!][TIO-ksy1go2e]
  • Inspired by [user's solution](https://codegolf.codidact.com/posts/283376/283384#answer-283384), I found the minimum number of initial characters that uniquely identified each color. I then golfed if further by realizing the ones at the end of the list don't actually have to uniquely identify the color, because the ones it couldn't determine would have been caught already. (E.g. `green` and `grey` need the `gree` to determine green, but if it isn't `green` then it must be `grey` and we can catch that with just `g`)
  • ----
  • # Old answer, 219 bytes
  • ```javascript
  • a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
  • ```
  • [Try it online!][TIO-ksawomhk]
  • [TIO-ksy1go2e]: https://tio.run/##ZVFNb9wgED3Hv@LJl2DJ8Q9I5L3n1GMP6UrBLLZpMIMAfzXqb99i2Gir9sIM772ZNwM/@cK9cMqGJ0MXee3bK@P10G7tqXQSOwYnJToHL9BpDhKwEm7uQBoLemiFgSBGTBCxQMDDETg0HAbYGSssCF3ZeKtVeMd70ytzeTUXubHQnrbGB@6C/67CyEJVVe2JN55cYGyr93gb2FY9DWyvqqsg4wOC9MGjxVvx8Hau41EeM5qyRtnpWR7RyUu6OlpNedOQTljU7rcov7i4TsTKXWpNa0xSeVb@V5yNer6muI4qJEf@a3bZmrzMo3DxcSR2dlYniBw3Q8q80ot0SZ6s75NrFeuOJP7J5CmZWGU@MiT5lERyyoybvZfh77VIqyU1WhTpTE18zpAYSZDmeeCvZWN7ycV4f66jiRjzMl5wd@tyvMm5OL8URU8O7P4VoD5/SYXP4uHAo3GjaWDlq7FzeI7VB1@9/MN@m8ON7lkS1Hj8YR6j7vf1Dw "JavaScript (Node.js) – Try It Online"
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ksawomhk]: https://tio.run/##ZVJLbgIxDF0zp7CyYUaic4Ai2HfVA1AWIXggbUhG@QyiVc9ObQdE1W4Sx37@vOe860knE@2Yn3zY43VYXfVqrSLu4YLOhTMcIqKHXQxnD8no6DDDzmnzAcEcI8KI2hwhlt0FgrMTwmQDYwZNCc4SEg7B7cEcgwlOZ4STLgQzEfWJTntKgSpbN2GEGBKC/ixU1@GJ/LGkRMVoiAuMJY4O4Xy0VGS0niaI2h@Qximo@jQ6m9s5zLt@sC5jbO1qrXvrjSt7TK3tuqsJPmXImHKCFWya2Wa7oEMJSbUAJaXoJgHkybTVDUMs2Mez3G68x5i@WqgqGRmSXpH/kmsjlodvYcOGsJbWpEEdhURmo/Jmq/Jlq@olcGn9mFwkZ@MmrVQgsaqLNBcQi1uzWd/ftGSJbNQ9siULk/z7DvlxJ0vl@Qs85OIi/DVkzvpj7pJum@2yaYYQoX2sAsJQV9LBVzNjPzXuXTi06sWPJT9TNse75Z/oa8m38NAKYAHzNz8n3Pf1Bw "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~153~~ 147 bytes
  • *-6 bytes thanks to Shaggy!*
  • ```
  • a=>a.sort((x,y)=>g(x)-g(y),g=x=>"y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l ru g pu w p o b".split` `.findIndex(t=>!x.indexOf(t)))
  • ```
  • [Try it online!][TIO-ksy1go2e]
  • Inspired by [user's solution](https://codegolf.codidact.com/posts/283376/283384#answer-283384), I found the minimum number of initial characters that uniquely identified each color. I then golfed if further by realizing the ones at the end of the list don't actually have to uniquely identify the color, because the ones it couldn't determine would have been caught already. (E.g. `green` and `grey` need the `gree` to determine green, but if it isn't `green` then it must be `grey` and we can catch that with just `g`)
  • ----
  • # Old answer, 219 bytes
  • ```javascript
  • a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
  • ```
  • [Try it online!][TIO-ksawomhk]
  • [TIO-ksy1go2e]: https://tio.run/##ZVHLbtwwDDzHXzH1JTbg@AMaeO855QOSBSJrZVuNLAqS/GrRb9/qscEW7cWkZ4YcUvzBVua4lcY/abqI69BdWXdirSPrq2pvjro7jdVeP43VUTdjt3en8sBohUBv4Th6xUAcRsAuPUhhxQAlMRL4hBncCnA4WAKDCiKMMAs2GBD6snVGSf@Bj3aQ@vKiL2KvfHf6trcy5q9D5eu6vnLSzsML5x06vBUPb@cmfMo4iC4blL1aRIxWXNKvpU2XNw2phAXtcYviiwszB6w8hFK0hSSVZ@V/xdloYFuK2yR9cmQ/F5utyYk8CuOfMTGLNSpBZJkeU@akWoVN8mR9n1zJUBeTcI3ZUTIxUn9mSLA5icScGbs4J/zfa5GSa2q0SlKZmtmSIT4RJ8XywF/LhvaC8en@XLEJn/IyjjN76xLf5Fycn4tiIIvqfgrQkE9S41fxEPFg3Coaq/JFm8V/D9WRr5//YV8Xf6PDeaOgweO7fgy639c/ "JavaScript (Node.js) – Try It Online"
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ksawomhk]: https://tio.run/##ZVJLbgIxDF0zp7CyYUaic4Ai2HfVA1AWIXggbUhG@QyiVc9ObQdE1W4Sx37@vOe860knE@2Yn3zY43VYXfVqrSLu4YLOhTMcIqKHXQxnD8no6DDDzmnzAcEcI8KI2hwhlt0FgrMTwmQDYwZNCc4SEg7B7cEcgwlOZ4STLgQzEfWJTntKgSpbN2GEGBKC/ixU1@GJ/LGkRMVoiAuMJY4O4Xy0VGS0niaI2h@Qximo@jQ6m9s5zLt@sC5jbO1qrXvrjSt7TK3tuqsJPmXImHKCFWya2Wa7oEMJSbUAJaXoJgHkybTVDUMs2Mez3G68x5i@WqgqGRmSXpH/kmsjlodvYcOGsJbWpEEdhURmo/Jmq/Jlq@olcGn9mFwkZ@MmrVQgsaqLNBcQi1uzWd/ftGSJbNQ9siULk/z7DvlxJ0vl@Qs85OIi/DVkzvpj7pJum@2yaYYQoX2sAsJQV9LBVzNjPzXuXTi06sWPJT9TNse75Z/oa8m38NAKYAHzNz8n3Pf1Bw "JavaScript (Node.js) – Try It Online"
#2: Post edited by user avatar Moshi‭ · 2021-08-30T02:50:53Z (over 3 years ago)
  • # [JavaScript (Node.js)], 219 bytes
  • ```javascript
  • a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
  • ```
  • [Try it online!][TIO-ksawomhk]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ksawomhk]: https://tio.run/##ZVJLbgIxDF0zp7CyYUaic4Ai2HfVA1AWIXggbUhG@QyiVc9ObQdE1W4Sx37@vOe860knE@2Yn3zY43VYXfVqrSLu4YLOhTMcIqKHXQxnD8no6DDDzmnzAcEcI8KI2hwhlt0FgrMTwmQDYwZNCc4SEg7B7cEcgwlOZ4STLgQzEfWJTntKgSpbN2GEGBKC/ixU1@GJ/LGkRMVoiAuMJY4O4Xy0VGS0niaI2h@Qximo@jQ6m9s5zLt@sC5jbO1qrXvrjSt7TK3tuqsJPmXImHKCFWya2Wa7oEMJSbUAJaXoJgHkybTVDUMs2Mez3G68x5i@WqgqGRmSXpH/kmsjlodvYcOGsJbWpEEdhURmo/Jmq/Jlq@olcGn9mFwkZ@MmrVQgsaqLNBcQi1uzWd/ftGSJbNQ9siULk/z7DvlxJ0vl@Qs85OIi/DVkzvpj7pJum@2yaYYQoX2sAsJQV9LBVzNjPzXuXTi06sWPJT9TNse75Z/oa8m38NAKYAHzNz8n3Pf1Bw "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], 153 bytes
  • ```
  • (a,g=x=>"re y gree br sc bla oc pe rub ol v f li go ch m cre c s ro a l r g pu w p o b".split` `.findIndex(t=>x.startsWith(t)))=>a.sort((x,y)=>g(x)-g(y))
  • ```
  • [Try it online!][TIO-ksy1go2e]
  • Inspired by [user's solution](https://codegolf.codidact.com/posts/283376/283384#answer-283384), I found the minimum number of initial characters that uniquely identified each color. I then golfed if further by realizing the ones at the end of the list don't actually have to uniquely identify the color, because the ones it couldn't determine would have been caught already. (E.g. `green` and `grey` need the `gree` to determine green, but if it isn't `green` then it must be `grey` and we can catch that with just `g`)
  • ----
  • # Old answer, 219 bytes
  • ```javascript
  • a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
  • ```
  • [Try it online!][TIO-ksawomhk]
  • [TIO-ksy1go2e]: https://tio.run/##ZVFNb9wgED3Hv@LJl2DJ8Q9I5L3n1GMP6UrBLLZpMIMAfzXqb99i2Gir9sIM772ZNwM/@cK9cMqGJ0MXee3bK@P10G7tqXQSOwYnJToHL9BpDhKwEm7uQBoLemiFgSBGTBCxQMDDETg0HAbYGSssCF3ZeKtVeMd70ytzeTUXubHQnrbGB@6C/67CyEJVVe2JN55cYGyr93gb2FY9DWyvqqsg4wOC9MGjxVvx8Hau41EeM5qyRtnpWR7RyUu6OlpNedOQTljU7rcov7i4TsTKXWpNa0xSeVb@V5yNer6muI4qJEf@a3bZmrzMo3DxcSR2dlYniBw3Q8q80ot0SZ6s75NrFeuOJP7J5CmZWGU@MiT5lERyyoybvZfh77VIqyU1WhTpTE18zpAYSZDmeeCvZWN7ycV4f66jiRjzMl5wd@tyvMm5OL8URU8O7P4VoD5/SYXP4uHAo3GjaWDlq7FzeI7VB1@9/MN@m8ON7lkS1Hj8YR6j7vf1Dw "JavaScript (Node.js) – Try It Online"
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ksawomhk]: https://tio.run/##ZVJLbgIxDF0zp7CyYUaic4Ai2HfVA1AWIXggbUhG@QyiVc9ObQdE1W4Sx37@vOe860knE@2Yn3zY43VYXfVqrSLu4YLOhTMcIqKHXQxnD8no6DDDzmnzAcEcI8KI2hwhlt0FgrMTwmQDYwZNCc4SEg7B7cEcgwlOZ4STLgQzEfWJTntKgSpbN2GEGBKC/ixU1@GJ/LGkRMVoiAuMJY4O4Xy0VGS0niaI2h@Qximo@jQ6m9s5zLt@sC5jbO1qrXvrjSt7TK3tuqsJPmXImHKCFWya2Wa7oEMJSbUAJaXoJgHkybTVDUMs2Mez3G68x5i@WqgqGRmSXpH/kmsjlodvYcOGsJbWpEEdhURmo/Jmq/Jlq@olcGn9mFwkZ@MmrVQgsaqLNBcQi1uzWd/ftGSJbNQ9siULk/z7DvlxJ0vl@Qs85OIi/DVkzvpj7pJum@2yaYYQoX2sAsJQV9LBVzNjPzXuXTi06sWPJT9TNse75Z/oa8m38NAKYAHzNz8n3Pf1Bw "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-08-13T22:15:04Z (over 3 years ago)
# [JavaScript (Node.js)], 219 bytes

```javascript
a=>"red yellow green brown scarlet black ochre peach ruby olive violet fawn lilac gold chocolate mauve cream crimson silver rose azure lemon russet grey purple white pink orange blue".split(' ').filter(i=>a.includes(i))
```

[Try it online!][TIO-ksawomhk]

[JavaScript (Node.js)]: https://nodejs.org
[TIO-ksawomhk]: https://tio.run/##ZVJLbgIxDF0zp7CyYUaic4Ai2HfVA1AWIXggbUhG@QyiVc9ObQdE1W4Sx37@vOe860knE@2Yn3zY43VYXfVqrSLu4YLOhTMcIqKHXQxnD8no6DDDzmnzAcEcI8KI2hwhlt0FgrMTwmQDYwZNCc4SEg7B7cEcgwlOZ4STLgQzEfWJTntKgSpbN2GEGBKC/ixU1@GJ/LGkRMVoiAuMJY4O4Xy0VGS0niaI2h@Qximo@jQ6m9s5zLt@sC5jbO1qrXvrjSt7TK3tuqsJPmXImHKCFWya2Wa7oEMJSbUAJaXoJgHkybTVDUMs2Mez3G68x5i@WqgqGRmSXpH/kmsjlodvYcOGsJbWpEEdhURmo/Jmq/Jlq@olcGn9mFwkZ@MmrVQgsaqLNBcQi1uzWd/ftGSJbNQ9siULk/z7DvlxJ0vl@Qs85OIi/DVkzvpj7pJum@2yaYYQoX2sAsJQV9LBVzNjPzXuXTi06sWPJT9TNse75Z/oa8m38NAKYAHzNz8n3Pf1Bw "JavaScript (Node.js) – Try It Online"