| Bytes | Lang | Time | Link |
|---|---|---|---|
| 094 | Python3 | 250517T015337Z | Ajax1234 |
| 070 | JavaScript ES6 | 160922T112357Z | Neil |
| 020 | 05AB1E | 160922T090743Z | Emigna |
| 088 | Julia | 160922T082523Z | nyro_0 |
Python3, 94 bytes
lambda a,b,h:sum(min((L:=abs(A-h[-(i+1)]))*a,L*b)for i,A in enumerate(h[:len(h)//2+len(h)%2]))
Python3, 246 bytes
Brute-force version with some optimizations:
def f(a,b,h):
q,s,S=[(h,0)],-1,[]
for h,c in q:
if h==h[::-1]:s=c;continue
for x in range(len(h)):
for C,O in[(a,1),(b,-1)]:
H=[*h];H[x]+=O
if all(a!=H or C+c<u for a,u in S)and(s==-1 or C+c<s):S+=[(H,c+C)];q+=[(H,c+C)]
return s
JavaScript (ES6), 70 bytes
f=(a,g,s)=>a.reduce((t,h,i)=>t+Math.abs(a[a.length+~i]-h),0)*(g>s?s:g)/2
Double-counts the differences, but we only need to make half the changes, which is achieved by dividing by 2 at the end.
05AB1E, 20 bytes
WU|vy#‚ø€¥˜Ä2ä`OX*,
Explanation
WU # store minimum cost in X
|v # for each testcase
y# # convert to list split on spaces
‚ø # zip with its own reverse
€¥ # deltas of each pair
˜Ä # deep flatten and take absolute value
2ä # split into 2 pieces
` # flatten (leaves the smaller piece on top of the stack)
O # sum
X* # multiply with minimum cost
, # print
Julia, 88 Bytes
f(l,A,B)=reduce(+,map(abs,map(sum,[zip(-l,reverse(l))...][1:fld(end,2)])))*min([A,B]...)
Explained:
If I am not mistaken, all we have to do is either grow all necessary plants or shrink them (depending on which operations is less expensive)
- zip input list with a reverse of itself (align corresponding heights)
- only look at first half of the list (floor when length is odd)
- sum the differences
- multiply with minimum cost (A or B)
Example
f([1,6,2,3,4],1,2) -> 6 (grow plant 1 and 4, yields [4,6,2,6,4])
f([1,6,2,3,4],3,2) -> 12 (shrink plant 2 and 5, yields [1,3,2,3,1])
Note:
As there is still a discussion about the input format, I disregarded it for the moment.
Update:
Updated to output energy, as I think that outputing the actual palindrome is more challenging I attach my previous code here (76 Bytes):
f(l,A,B)=reduce(hcat,map(x->sort([x...]),[zip(l,reverse(l))...]))[A>B?1:2,:]