Page 3 of 7

Posted: Fri Dec 25, 2009 12:17 pm
by neo1985
Did it in VBScript:

Code: Select all

'==========================================================================
'
' NAME: Decrypter Hackers.org
'
' AUTHOR: Neo1985
' DATE  : 25/12/2009
'
' COMMENT: 
'
'==========================================================================

Dim Inputstring, y, z, OutputString

'Inputstring = "123x456"
Inputstring = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"

x = 1
Do Until x = Len(Inputstring)+1

	y= Mid(Inputstring,x,1)
	WScript.Echo "Processing: " & y
	If y = "x" Then
		z = Mid(Inputstring,x-2,2)
		WScript.Echo "x found, replacing by: " & z
		OutputString = OutputString & z
		Inputstring = RebuildInputString(x,z)
	End If
	x = x +1
Loop 

WScript.Echo "Final String: " & Inputstring
WScript.Echo "Final Sum: " & GetTheSum(Inputstring)


Function RebuildInputString(x, z)
	Dim temp
	
	temp = Mid(Inputstring,1,x-1) & z
	temp = temp & Mid(Inputstring,x+1 , Len(Inputstring) - x)
	WScript.Echo "New inputstring: " & temp
	RebuildInputString = temp
End Function

Function GetTheSum(sumString)
Dim sum, x , y

For x = 1 To Len(sumString)
	y = Mid(sumString,x,1)
	y = CInt(y)
	sum = sum + y

Next

GetTheSum = sum

End function

Posted: Fri Feb 19, 2010 10:48 pm
by midnightman
void main(void)
{
char *str ="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xxy";
int i;
int x=0;

for(i=0;1;i++)
{
if(str=='y') // y=end
break;
if(str=='x')
{
memmove(&str,&str[i+1],strlen(&str[i+1]));
i-=2;
}
x+=(str-'0'); // (- ascii "0")
}
printf("%d",x);
}

Posted: Fri Mar 19, 2010 11:23 pm
by jeffrem2
I wrote a "witty one-liner" in bash/sed:

Code: Select all

expr `sed -re : -e 's/([0-9][0-9])x/\1\1/g' -e t -e 's/[0-9]/& + /g' Valuationtxt` 0
(Note: The zero at the end is because of a trailing '+' at the end of the sed output)
I admit, this is a refined version of my original script.
Comments/Observations/Questions?

P.S.
teebee: That is impressive. VERY. IMPRESSIVE. That's what I call hacking! Now for me to find out how it actually works...! Very nice indeed!

Posted: Thu Apr 01, 2010 2:21 pm
by zuo
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
char s[1000] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
int length = strlen(s);
int sum = 0;
for(int i = 0; s != '\0';) {
if(s >= '0' && s <= '9') {
sum = sum + s - '0';
i++;
}
else if(s == 'x') {
for(int j = i; s[j] != '\0'; j++)
s[j] = s[j+1];
i-=2;
}
}
cout << sum << endl;
return 0;
}
Here is my code with c++

Posted: Thu Apr 01, 2010 5:40 pm
by megabreit
teebee's version is impressive, indeed, but it doesn't work like it is printed in the forum :?

Teebee: In what environment did this work?? My perl 5.10 doesn't like it.
BTW Jeffrem2: Solving this with sed is the same level of "insanity" :lol: Congratulations!

I tried to understand teebee's version too and came out with this (working) perl script:

Code: Select all

#!/usr/bin/perl
$_="93752xxx74...";
while (s/(..)x/$1$1/) {1};
for (split("")) {$x+=$_};
print $x;
It obviously does the same, is far easier to read (hm, not so obvious) and (hopefully) easier to understand... and finally you can still write it in one line :wink:

Posted: Thu Apr 01, 2010 7:36 pm
by teebee
megabreit wrote:Teebee: In what environment did this work?? My perl 5.10 doesn't like it.
Hm, it works for me using perl v5.10.1 (*) built for i686-cygwin-thread-multi-64int. What is the exact problem you have encountered?
megabreit wrote:It obviously does the same, is far easier to read (hm, not so obvious) and (hopefully) easier to understand... and finally you can still write it in one line :wink:
Yes, that's all correct. I tried to give a short solution...

Posted: Thu Apr 01, 2010 11:12 pm
by megabreit
I have to correct myself... it's not perl 5.10; it's the OS it's running on... or more correct, the Windows "Shell" I tried to run the oneliner in. I rarely use Windows AND perl all together, and I probably will not again in the future :-)
If you run this in Windows' cmd, you'll get the error:
Can't find string terminator "'" anywhere before EOF at -e line 1.
It seems that there have to be some characters escaped. But I don't really want to know which ones.
It's running fine on Solaris and Linux and probably any UNIX shell.

Sorry for the noise!

Posted: Fri Apr 02, 2010 12:37 pm
by teebee
Thanks for the noise! While thinking about your more readable version I found a way to omit the for loop:

Code: Select all

perl -le '$_="93752xxx74...";$x+=$2while s/(..)x|(.)/$1$1/;print$x'

Another solution in Perl

Posted: Thu May 20, 2010 8:22 pm
by markobr
I used an array:

$text = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
@text = split(//, $text);
$i = 0;
while ($i < @text) {
$wert = $text[$i];
if ($wert eq "x") {
splice(@text, $i, 1);
$i -= 2;
}
else {
$summe += $wert;
++$i;
}
}
print "$summe\n";

Posted: Tue Jun 22, 2010 7:24 pm
by keiya
Yours are all WAY overcomplicated: you're adhering to the definition too closely and actually removing the xes and going back. It's easier if you don't:

Code: Select all

a = 0
pp = 0
p = 0
vs = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'

for c in vs:
    if c == 'x':
        a = a + p + pp
    else:
        pp = p
        p = int(c)
        a = a + int(c)

print a

Posted: Mon Jul 19, 2010 7:09 am
by Raizdecimal
I used Python :D

Code: Select all

spam = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
counter,count,minusone = 0,1,0

while count <= 256-minusone:
    if spam[count-1].isnumeric() == True:
        counter += int(spam[count-1])
        count += 1
    else:
        spam = spam.replace('x','',1)
        minusone += 1
        count -= 2

print(counter)
There u go! Can't become more readable than that xD

Posted: Mon Aug 02, 2010 3:47 pm
by Acitran
i love my solution... took me 3 mins runtime insta ;)

Code: Select all

		String src="93752xxx746x...............";
		char[] srcc=src.toCharArray();
		List chars = new List();
		for(int i=0;i<srcc.length;i++) chars.add(Character.toString(srcc[i]));
		int count=0;
		for (int i=0;i<chars.getItemCount();i++)
		{
			if (Character.isDigit(chars.getItem(i).toCharArray()[0]))
			{
				count+=Integer.parseInt(""+chars.getItem(i));
			} else
			{
				count+=Integer.parseInt(""+chars.getItem(i-1));
				count+=Integer.parseInt(""+chars.getItem(i-2));
				chars.remove(i);
				i--;
			};
		}
		System.out.println(count);
	}

Posted: Sat Aug 28, 2010 9:29 pm
by Curiosu
I wanted to see the string too, so two passes was made, ugly but it works
php

Code: Select all

$orig = '93752xxx746......';
echo $orig;
$str=str_split($orig);
    $a=0;$b=0;
    foreach($str as $char)
     {
        if (is_numeric($char)) {$a=$b;$b=$char;};
        if ($char == 'x') {$orig=preg_replace('/x/',$a.$b,$orig,1);};
     }
  echo '<br/>'.$orig; 
  $str=str_split($orig); 
$sum=0;
	foreach($str as $char)
	   {  if (is_numeric($char)) $sum +=$char;
     }

Posted: Sat Oct 16, 2010 10:21 pm
by junkpete
C 8)

Code: Select all

	char str[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
	int sum, j, k;
	
	for (int i=0; i<strlen(str); i++) {
		if(str[i] == 'x' && (j=i)) {
			k=0;
			while (k<2 && j--) {
				if(str[j]!='x') {
					sum += str[j] - '0';
					k++;					
				}
			}
		} 
		else sum+=str[i]- '0';
	}
	printf("%d", sum);

Posted: Sun Oct 17, 2010 2:11 pm
by Joeb27
Bash shell script:

Code: Select all

#!/bin/bash

STRING="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"

SUM=0
MEM1=0
MEM2=0

while read -r -n 1 CHAR ; do

        if [ "$CHAR" == "x" ] ; then
                SUM=$((SUM+$MEM1+$MEM2))
        elif [ -z "$CHAR" ] ; then
                echo $SUM
                exit 0
        else
                SUM=$((SUM+$CHAR))
                MEM1=$MEM2
                MEM2=$CHAR
        fi

done <<< $STRING