Valuation

Discussion of challenges you have already solved
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

AMindForeverVoyaging wrote:
avrrobot wrote:well, I think I'm one of the first solving it with c++
Except that your code really is C ;)
Aside from the header that is
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

CodeX wrote:Aside from the header that is
I guess they intended to write <stdio.h>. :)
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

iostream is used for cout << x & cin >> x style streams in C++ so is usually included in programs but usually with using namespace std; too so you don't have to do std::cout << x all over the place. That technically makes it C++ even if it isn't used as a C compiler wouldn't know what to do with it, otherwise it is normal C89 as even the variables are declared at the top of blocks.

Funnily enough though I think that code works on GCC because it coerces printf from the standard libraries even though it isn't explicitly told to do so (as iostream doesn't actually contain printf) so doesn't really use any C++ features at all, aside from being backward compatible with C. Hooray for technicalities
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

CodeX wrote: Funnily enough though I think that code works on GCC because it coerces printf from the standard libraries even though it isn't explicitly told to do so
I just tried to compile avrrobot's code with gcc and I get linker errors. ("undefined reference to")
However with g++ instead of gcc and the very same options it compiles and links fine. And with stdio.h instead of iostream it compiles fine with gcc.

If you change the source code filename ending from .cpp to .c, that changes the behaviour too it seems... at least as long as you leave out the #include.

Technicalities indeed :)
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

arg, I forgot gcc was c and g++ was the cpp compiler, I tested it on www.codepad.org
BosseBL
Posts: 6
Joined: Tue Aug 09, 2011 10:47 am
Location: Stockholm

Post by BosseBL »

c++ solved it for me:

Code: Select all

int chartoint(char ch) {
	ch = ch - 48;
	return int(ch);
}
int main() {
	char buff1 = '0';
	char buff2 = '0';
	char ch;
	int ans;
	
	cin.get(ch);
	while (ch != '\n') {
		if (isdigit(ch)) {
			ans += chartoint(ch);
		}
		else if (ch == 'x') {
			ans += chartoint(buff1) + chartoint(buff2);
			cin.get(ch);
			continue;
		}
		else {}
		buff2 = buff1;
		buff1 = ch;
		cin.get(ch);
	}
	cout << endl << ans;
        
   cin.get();
   return 0;
}
gunn4r
Posts: 6
Joined: Thu Aug 11, 2011 11:44 pm

Post by gunn4r »

Thought about doing this in the HVM because it seemed like it would be right up it's alley. then I decided to use python and vi. 2 minutes later:

Code: Select all

!/usr/bin/python
import sys,io

def calculate (s):
  l = list(s)
  sum = 0
  i = 0
  while i < len(l):
    if l[i] == 'x':
      del l[i]
      i -= 2
    else:
      print l[i]
      sum += int(l[i])
      i += 1
  return sum
s = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
print calculate(s)

[/code]
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

I just knocked up some HVM code to solve this as it seemed like a good idea thanks to gunn4r:

Code: Select all

001^<83*5*:57*    ?1^<86*-2v1+2v2^+1^<47*    ?049+4*-g2^+3^+1v1+1v049+3*-gp!
it's a bit long as I used a very rough linker made just to get the job done before I finish a more complete tool set, and also made no attempt to make the code smaller. To make up for that the smallest code possible, at least as far as I have figured at the moment, is:

Code: Select all

67*9*5-4*P
:P
Mad Mike
Posts: 11
Joined: Tue Jun 21, 2011 4:38 pm

Post by Mad Mike »

A challenger appears: C#

Code: Select all

namespace Xe
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder input = new StringBuilder("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx");
            //StringBuilder input = new StringBuilder("93752xxx"); // testing
            int sum = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == 'x')
                {
                    input.Remove(i,1);
                    i-=3;
                }
                else
                {
                        sum += (Convert.ToInt16(input[i]) - 48);
                }
            }
            Console.WriteLine(sum);
            Console.ReadKey();
        }
    }
}
chown -R us ./base
bazuzy
Posts: 1
Joined: Thu Sep 29, 2011 2:19 pm

Post by bazuzy »

CopyPast in vim
:%s/x/1^1^/g
Adding enough +++++ and a p at the end of the line.

Copy Paste This in HVM
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

CodeX wrote:To make up for that the smallest code possible, at least as far as I have figured at the moment, is:

Code: Select all

67*9*5-4*P
:P
Reminds me of this :)[/url]
There is no spoon.
Smee80
Posts: 2
Joined: Mon Oct 24, 2011 7:14 pm

Post by Smee80 »

I like using Python for string manipulation - writing the program takes as long as it takes to type it on the keyboard (simply rewrite the sentences in code):

Code: Select all

s='...'
sum=0
i=0
while i < len(s):
	if s[i].isdigit():
		sum += int(s[i])
		i+=1
	elif s[i] == 'x':
		s = s[0:i]+s[i+1:len(s)]
		i-=2
print sum
Last edited by Smee80 on Sun Nov 27, 2011 6:03 pm, edited 1 time in total.
Shurakai
Posts: 1
Joined: Fri Jul 01, 2011 8:56 pm

Post by Shurakai »

my solution with visual c++

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int l = 0;
int sum = 0;
int number = 0;

String ^test = "x";
String ^string2;
String ^string1 = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
Console::WriteLine(L"Problem Valuation:");

l = string1->Length;
for (int i=0; i < l; i++)
{
if (string1->Substring(i,1) == test)
{
string1 = string1->Remove(i,1);
l = string1->Length;
i -= 3;
}
else
{
string2 = string1->Substring(i,1);
number = Convert::ToInt32(string2);
sum += number;
}

}

Console::WriteLine();
Console::WriteLine("The sum is: "+sum);

Console::ReadLine();
return 0;
}
june232
Posts: 1
Joined: Wed Nov 30, 2011 5:01 am
Location: Hong Kong

my c++ code

Post by june232 »

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
	char str[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
	int len = strlen(str);
	int *flag = new int[len];
	int i=0;
	int sum = 0;

	memset(flag, 0, len*sizeof(int));
	while( i<len )
	{
		if (flag[i])
		{
			i++;
			continue;
		}
		else if (str[i]>='0' && str[i]<='9')
		{
			sum += str[i]-'0';
			i++;
		}
		else if (str[i]=='x')
		{
			flag[i] = 1;
			i--;
			while(flag[i]) i--;
			i--;
			while(flag[i]) i--;
		}
	}

	printf("%d\n", sum);
}
:)
AlbusShin
Posts: 9
Joined: Fri Nov 18, 2011 2:39 pm
Location: Lost in binary

Post by AlbusShin »

C++ worked for me. A string and some codes
Post Reply