Skip to main content
[Edit removed during grace period]
Source Link
Vitruvie
  • 251
  • 1
  • 4
saved 6 bytes
Source Link
Vitruvie
  • 251
  • 1
  • 4

Bash, 5454 48 bytes

-6 bytes by adapting Dominic van Essen's output-prevention trick

echo "trap 'echo \$s'printf EXIT":|:
((s+=p<$1?$1-2*p:$1))
p=$1"p=$1
echo \$s"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts, luckily Bash has just the trick. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!Try it online!

Explanation

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

trap 'echo $s' EXIT:|:
((s+=p<3?3-2*p:3))
p=3
trap 'echo $s'echo EXIT$s:|:
((s+=p<10?10-2*p:10))
p=10
echo $s
  • trap:|: runsprevents any echo only at the end of the program, and only oncefrom a previous fragment from outputting
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

Bash, 54 bytes

echo "trap 'echo \$s' EXIT
((s+=p<$1?$1-2*p:$1))
p=$1"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts, luckily Bash has just the trick. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!

Explanation

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

trap 'echo $s' EXIT
((s+=p<3?3-2*p:3))
p=3
trap 'echo $s' EXIT
((s+=p<10?10-2*p:10))
p=10
  • trap runs echo only at the end of the program, and only once
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

Bash, 54 48 bytes

-6 bytes by adapting Dominic van Essen's output-prevention trick

printf ":|:
((s+=p<$1?$1-2*p:$1))
p=$1
echo \$s"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!

Explanation

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

:|:
((s+=p<3?3-2*p:3))
p=3
echo $s:|:
((s+=p<10?10-2*p:10))
p=10
echo $s
  • :|: prevents any echo from a previous fragment from outputting
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

added 18 characters in body
Source Link
Vitruvie
  • 251
  • 1
  • 4

Bash, 54 bytes

echo "trap 'echo \$s' EXIT
((s+=p<$1?$1-2*p:$1))
p=$1"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts, luckily Bash has just the trick. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!

Explanation

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

trap 'echo $s' EXIT
((s+=p<3?3-2*p:3))
p=3
trap 'echo $s' EXIT
((s+=p<10?10-2*p:10))
p=10
  • trap runs echo only at the end of the program, and only once
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

Bash, 54 bytes

echo "trap 'echo \$s' EXIT
((s+=p<$1?$1-2*p:$1))
p=$1"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts, luckily Bash has just the trick. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

trap 'echo $s' EXIT
((s+=p<3?3-2*p:3))
p=3
trap 'echo $s' EXIT
((s+=p<10?10-2*p:10))
p=10
  • trap runs echo only at the end of the program, and only once
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

Bash, 54 bytes

echo "trap 'echo \$s' EXIT
((s+=p<$1?$1-2*p:$1))
p=$1"

This assumes the program must not have any excess output, which turns out to be one of the tricker parts, luckily Bash has just the trick. I expect one of the golfing languages that will output by default at the end of the program would do best here.

Try it online!

Explanation

Example output for 3 concatenated with output for 10 (the concatenated program outputs 7):

trap 'echo $s' EXIT
((s+=p<3?3-2*p:3))
p=3
trap 'echo $s' EXIT
((s+=p<10?10-2*p:10))
p=10
  • trap runs echo only at the end of the program, and only once
  • s is the sum value so far (implicitly 0 at the start of the program)
  • p is the previous value

Each fragment assumes that the current value is not less than the next value, and thus adds it to the sum. It also checks whether the previous fragment was wrong to assume this (ie. the previous value was less than the current value), and if so corrects the sum by subtracting double the previous value.

added 15 characters in body
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading
added explanation
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading
saved 1 byte
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading
added 62 characters in body
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading
added 279 characters in body
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading
Source Link
Vitruvie
  • 251
  • 1
  • 4
Loading