Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 1373 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

The resources are 1, 2, 3, 4, 5 where 5 represents OreOre.

The idea is to first count the resources by type, then reduce all counts of BB, LL, WW, and SS by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources (This is actually achieved by adding an extra OO (5) and reducing allall five counts by 11). Next we integer-divide all these values by four to see how many units we may trade for with each of our remaining counts by resource typestype while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). AddLastly we add up the values and check if itthe result is greater than or equal to zero (here greater than -1 can be used since we always have integers):.

The resources are 1, 2, 3, 4, 5 where 5 represents Ore.

The idea is to first count the resources by type, then reduce all counts of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources (This is actually achieved by adding an extra O and reducing all counts by 1). Next we integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

The resources are 1, 2, 3, 4, 5 where 5 represents Ore.

The idea is to first count the resources by type, then reduce all counts of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources (This is actually achieved by adding an extra O (5) and reducing all five counts by 1). Next we integer-divide all these values by four to see how many units we may trade for with each of our remaining counts by resource type while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Lastly we add up the values and check if the result is greater than or equal to zero (here greater than -1 can be used since we always have integers).

added 1373 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, 13(削除) 13 (削除ここまで) 12 bytes

ċÐ5ドル’‘5¦;5ċÐ5ドル’:4S>-

Try it online! Try it online! or see the test-suite test-suite (using the OP IO).

FirstThe idea is to first count the resources by type. Then, then reduce all counts of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources (This is actually achieved by adding an extra O and reducing all counts by 1). IntegerNext we integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

ċÐ5ドル’‘5¦;5ċÐ5ドル’:4S>- - Link: list of numbers (BLWSO:12345) e.g. [3,2,2,2,2,2,5,5,5,5] (WLLLLLOOOO)
;5 5 - concatenate -a literalfive 5
 Ѐ - map across implicit range(5) = [1 [3,2,32,42,2,2,5,5,5,5,5]:
ċ  5 - count  literal 5
 Ѐ - map across implicit range(5) [= 0[1, 52, 13, 04, 4]5]:
 ċ  - decrement (vectorises) count [-1, 4, 0,-1, 3]
  ¦  - sparse application:
 [ 0, 5 -, ...to1, indices:0, 55]
   - ...action: increment decrement (vectorises) [-1, 4, 0,-1, 4]
 :4 - integer divide by four [-1, 1, 0,-1, 1]
 S - sum 0
 - - literal -1 -1
 > - greater than? 1

Jelly, 13 bytes

ċÐ5ドル’‘5¦:4S>-

Try it online! or see the test-suite (using the OP IO).

First count the resources by type. Then reduce all of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources. Integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

ċÐ5ドル’‘5¦:4S>- - Link: list of numbers (BLWSO:12345) e.g. [3,2,2,2,2,2,5,5,5,5] (WLLLLLOOOO)
 5 - literal 5
 Ѐ - map across implicit range(5) = [1,2,3,4,5]:
ċ  - count  [ 0, 5, 1, 0, 4]
  - decrement (vectorises) [-1, 4, 0,-1, 3]
  ¦  - sparse application:
 5 - ...to indices: 5
  - ...action: increment  [-1, 4, 0,-1, 4]
 :4 - integer divide by four [-1, 1, 0,-1, 1]
 S - sum 0
 - - literal -1 -1
 > - greater than? 1

Jelly, (削除) 13 (削除ここまで) 12 bytes

;5ċÐ5ドル’:4S>-

Try it online! or see the test-suite (using the OP IO).

The idea is to first count the resources by type, then reduce all counts of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources (This is actually achieved by adding an extra O and reducing all counts by 1). Next we integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

;5ċÐ5ドル’:4S>- - Link: list of numbers (BLWSO:12345) e.g. [3,2,2,2,2,2,5,5,5,5] (WLLLLLOOOO)
;5 - concatenate a five  [3,2,2,2,2,2,5,5,5,5,5]
 5 - literal 5
 Ѐ - map across implicit range(5) = [1,2,3,4,5]:
 ċ - count [ 0, 5, 1, 0, 5]
  - decrement (vectorises) [-1, 4, 0,-1, 4]
 :4 - integer divide by four [-1, 1, 0,-1, 1]
 S - sum 0
 - - literal -1 -1
 > - greater than? 1
added 1373 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

How?

First count the resources by type. Then reduce all of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources. Integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

ċÐ5ドル’‘5¦:4S>- - Link: list of numbers (BLWSO:12345) e.g. [3,2,2,2,2,2,5,5,5,5] (WLLLLLOOOO)
 5 - literal 5
 Ѐ - map across implicit range(5) = [1,2,3,4,5]:
ċ - count [ 0, 5, 1, 0, 4]
 ’ - decrement (vectorises) [-1, 4, 0,-1, 3]
 ¦ - sparse application:
 5 - ...to indices: 5
 ‘ - ...action: increment [-1, 4, 0,-1, 4]
 :4 - integer divide by four [-1, 1, 0,-1, 1]
 S - sum 0
 - - literal -1 -1
 > - greater than? 1

How?

First count the resources by type. Then reduce all of B, L, W, and S by one - if we counted none for any of these four then they will now have entries of -1 - we need to acquire them from our remaining resources. Integer-divide all these values by four to see how many units we may trade for with each of our resource types while not affecting the -1 and 0 counts (note that -1 integer-divided by four is -1, not 0). Add up the values and check if it is greater than or equal to zero (here greater than -1 can be used since we always have integers):

ċÐ5ドル’‘5¦:4S>- - Link: list of numbers (BLWSO:12345) e.g. [3,2,2,2,2,2,5,5,5,5] (WLLLLLOOOO)
 5 - literal 5
 Ѐ - map across implicit range(5) = [1,2,3,4,5]:
ċ - count [ 0, 5, 1, 0, 4]
 ’ - decrement (vectorises) [-1, 4, 0,-1, 3]
 ¦ - sparse application:
 5 - ...to indices: 5
 ‘ - ...action: increment [-1, 4, 0,-1, 4]
 :4 - integer divide by four [-1, 1, 0,-1, 1]
 S - sum 0
 - - literal -1 -1
 > - greater than? 1
Post Undeleted by Jonathan Allan
Post Deleted by Jonathan Allan
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading

AltStyle によって変換されたページ (->オリジナル) /