Page 4 of 7

Posted: Sun Oct 24, 2010 7:56 am
by Knochnkopf
I guess this one earns me the award for the most bloated approach:

Code: Select all

#!/bin/bash

file=val.txt
i=0
 
while IFS= read -r -n1 val_char
  do
	x_array[i]=$val_char	
	let i++
  done  < "$file"

j=0
array_count=0

while [ ${x_array[j]} != "" ]
  do
    if [ ${x_array[j]} != 'x' ]
      then
	final_array[$array_count]=${x_array[j]}
	let array_count++
	let j++
      else
	num1=`expr $array_count - 2`
	num2=`expr $array_count - 1`
	array_count_plus=`expr $array_count + 1`

	final_array[$array_count]=${final_array[$num1]}
	final_array[$array_count_plus]=${final_array[$num2]}
	let j++

	array_count=`expr $array_count + 2`
    fi
  done

k=0
l=`expr $i - 1`

index=`expr ${#final_array[@]} - 1`

for k in $(seq 0 $index );
  do    
    result=`expr $result + ${final_array[$k]}`	
  done

echo "Result: " $result

Posted: Mon Nov 22, 2010 12:31 am
by smagazor
Using 'Dynamic Memory Allocation' in C.

Code: Select all

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(){
    struct Valuation{
           char value;
           struct Valuation *urm,*ant;}start,*nod;
    start.urm=NULL;
    start.ant=NULL;
    nod=&start;
    char x[]="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
    int i=0,sum=0;
    while(i<strlen(x)){
        nod->urm=(struct Valuation *)malloc(sizeof(struct Valuation));
        nod->urm->value=x[i];
        nod->urm->ant=nod;
        nod=nod->urm;
        nod->urm=NULL; 
        i++;           }
        
    nod->urm=(struct Valuation *)malloc(sizeof(struct Valuation));
    nod->urm->ant=nod;
    nod=nod->urm;
    nod->urm=NULL;
    nod->value='E';
    
    nod=start.urm;
    while(nod->urm){
        if (isdigit(nod->value)){
            sum+=nod->value-'0';
             nod=nod->urm;       }
        else if (nod->value=='x'){
            nod=nod->ant->ant;    
            nod->urm->urm->urm->ant=nod->urm;
            nod->urm->urm=nod->urm->urm->urm; 
            }
                }
    printf("Sum is: %d",sum);
    getch();
    return 0;
          }
smagazor

Java

Posted: Sun Dec 05, 2010 4:05 pm
by Kladdyman
Some simple Java with comments.

Code: Select all

public static void main(String[] args) {
    int count = 0, a = 0, b;
    char c;
    String s = "---"; // The string
    while (a < s.length()) {
        c = s.charAt(a); // Get the a-th character in the string
        if (c == 'x') {
            s = s.replaceFirst("x", ""); // Remove the x
            a -= 3; // Move a back 3 spaces
            c = 48; // So that c - 48 = 0
        }
        b = c - 48; // Get the actual number from the char code
        count += b; // Add the current number
        a++; // Move a forward 1 space
    }
    System.out.println(count);
}

Powershell can do it too

Posted: Thu Feb 03, 2011 2:32 pm
by Karuto

Code: Select all

$string  = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"
$sum = 0
$i = 0
while($i -lt $string.length){
    if($string[$i] -match "[0-9]") {
        $sum = $sum + [Int32]::Parse($string[$i])
        $i++
    } else {
        if ($string[$i] -eq "x") {
            $string = $string.Substring(0,$i) + $string.Substring($i+1,$string.length - $i -1)
            $i = $i -2
        } 
    }
}

$sum

dead end!!

Posted: Mon Mar 28, 2011 1:02 pm
by bodjo
well it is confusing not to have a path after this challenge it s in the middle of the map
did i have a problem with my map or it is really a dead end?

Posted: Wed Mar 30, 2011 2:24 pm
by uws8505
bodjo wrote:well it is confusing not to have a path after this challenge it s in the middle of the map
did i have a problem with my map or it is really a dead end?
Unfortunately yes, it is currently a dead end.

Posted: Tue Apr 12, 2011 6:48 pm
by Schnapphahn
Thougt this is an easy job using javascript. But after hours of coding i gave up, edited the 'x' by hand and let my unfinished program calculate the sum.
I got mad by the way JS uses strings and that.

Groovy Solution

Posted: Wed May 18, 2011 2:51 pm
by Mokor
It is possibly not the best Groovy solution, because it looks to much like plain old Java, but it works fine.

Code: Select all

def text = "93752xxx746x27x1754xx90x93xxxxx"
def result = 0
def pointer = 0

textAsChars = text.toList()
while(pointer < textAsChars.size()) {
	curChar = textAsChars[pointer]
	if(curChar.isInteger()) {
		result += curChar.toInteger()
		pointer++
	}
	else {
		textAsChars.remove(pointer)
		pointer -= 2
	}
}

println result

Posted: Sat Jun 18, 2011 10:46 pm
by turing_bot
I did it with Matlab :P

Code: Select all

% valuation challenge on Hacker.org

% First we initialize the string to be decoded
source = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';
sourcelen = length(source);

i=1;
sum=0;

while i<(length(source)+1)
    read=source(i);
    if read=='x'
        source=strcat(source(1:i-1),source((i+1):length(source)));
        i=i-2;
    end
    read=source(i);
    if read~='0'
        if str2num(read)~=0
            sum=sum+str2double(read);
        end
    end
    i=i+1;
end

sum

Posted: Wed Jun 22, 2011 6:52 pm
by General_Payne
Python. Believe its as simple as can be :-)

Code: Select all

x = "93752xxx74..."
count = 0
total = 0
while count < len(x):
	if str(x[count]) == "x":
		x = str(x[:count]) + str(x[(count+1):])
		total += (int(x[count-2]) + int(x[count-1]))
	else:
		total += int(str(x[count]))
		count += 1
print total

Posted: Wed Jun 22, 2011 7:42 pm
by CodeX
You don't need to wrap all uses of x with str such as str(x[count]) as they are string objects anyway, you can also make it shorter by replacing the x = x[:count] + x[count+1:] with a pop and list. Here's a trimmed version:

Code: Select all

x = [ord(b)-48 for b in "93752xxx7..."]
total = count = 0
while count < len(x):
   if x[count] == 72:
      x.pop(count)
      total += sum(x[count-2:count])
   else:
      total += x[count]
      count += 1
print(total)
The first line changes the string into a list where each character b is transformed to ord(b)-48 which converts the ascii numbers to their numerical value ('0'..'9' = 0x30..0x39 = 48..57) and x to 0x48, my thinking on that was:
X = 0x58, x = X+0x20, x-0x30 = 0x48 = 72
It helps to know ASCII and all that achieves is a few less bytes in the code.
Line 2 is a lazy assignment which saves a whole 2-3 bytes. The sum part replaces the converting to ints (as that is already done) and adding two adjacent items in the list. Finally I threw some brackets around print's parameters as in Python 3 print is no longer a statement but a function.
And now it's arguably a tiny bit simpler :P

Posted: Wed Jun 22, 2011 10:58 pm
by Schippi

Code: Select all

public static void main(String a[]){
		String aa="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
		int sum=0;
		int horst=aa.length();
		int i=0;
		while(i<horst){
			if(aa.charAt(i)==120){
				aa=aa.substring(0,i)+aa.substring(i+1);
				i-=2;
				horst--;
			}else{
				sum+=(aa.charAt(i)-48);
				i++;
			}
		}
		System.out.println(sum);
	}
my small java thingy, the most time consumed looking up how substring worked again :>

1492

Posted: Fri Jun 24, 2011 10:16 pm
by jackieben
Python2

This part transforms the string to a list:

Code: Select all

string='93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
a=0
list=[]
while a!=len(string):
  list.append(string[a])
  a=a+1
And this part sums up the stuff into "c":

Code: Select all

list=['9', '3', '7', '5', '2', 'x', 'x', 'x', '7', '4', '6', 'x', '2', '7', 'x', '1', '7', '5', '4', 'x', 'x', '9', '0', 'x', '9', '3', 'x', 'x', 'x', 'x', 'x', '2', '3', '8', 'x', '4', '4', 'x', '7', '5', 'x', 'x', '0', '8', '7', '5', '0', '9', '1', '2', '7', '3', '8', 'x', '8', '4', '6', '1', 'x', '8', '7', '5', '9', '3', '8', '3', 'x', 'x', '3', '2', '8', 'x', '4', 'x', '4', '9', '3', '5', '9', '0', '3', 'x', '6', 'x', '5', '5', '5', '0', '3', '6', '0', '5', '3', '5', '0', '0', '4', 'x', '0', 'x', 'x', '9', '4', '5', '9', '5', '8', '9', '6', '1', '2', '9', '6', 'x', '2', '6', '7', 'x', '8', '8', '4', '2', 'x', 'x', 'x', '5', 'x', '6', 'x', 'x', '6', '1', 'x', '4', 'x', '4', '8', '4', '8', '2', 'x', '8', '0', 'x', 'x', 'x', '8', '3', '3', '1', '6', '8', '4', '3', 'x', '7', 'x', '4', 'x', '8', '3', 'x', '9', '5', '2', '1', '7', '3', '1', 'x', 'x', 'x', '2', '5', 'x', '5', '1', 'x', 'x', '4', '5', '7', 'x', '6', 'x', '5', 'x', '9', '6', '9', '8', '2', '2', '2', 'x', '7', '7', '1', '2', '3', '7', '7', '4', '5', '0', '3', '4', 'x', '5', '1', '3', '3', '5', '9', '2', 'x', '2', '7', 'x', 'x', '8', 'x', '8', '7', 'x', 'x', '3', '5', '2', '2', '1', 'x', '3', '6', 'x', '0', 'x', '5', '0', 'x', '2', '3', 'x', '7', 'x', '6', '3', 'x', '9', '9', '8', '4', '1', '8', 'x', 'x']
b=0
c=0
while b!=len(list):
  if list[b]!='x':
    c,b=c+int(list[b]),b+1
    print 'c incrased to:',c
  else:
    del list[b]
    b=b-2
    print 'went back'

print c
I'm just a beginer yet to coding

Posted: Fri Aug 05, 2011 12:39 pm
by avrrobot
well, I think I'm one of the first solving it with c++:

#include <iostream>
int ergebnis;
int zahl1;
int zahl2;
char analys[265] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
int main()
{
for(int i=0;i<256;i++)
{
if(analys[i] == 'x')
{
ergebnis += zahl1;
ergebnis += zahl2;
}
else
{
zahl2 = zahl1;
zahl1 = analys[i] - 48;
ergebnis = ergebnis + analys[i] - 48;
}
}
printf("Das Ergebnis ist: %i\n",ergebnis);
return 0;
}

Posted: Fri Aug 05, 2011 1:16 pm
by AMindForeverVoyaging
avrrobot wrote:well, I think I'm one of the first solving it with c++
Except that your code really is C ;)