Page 5 of 7

Posted: Fri Aug 05, 2011 2:37 pm
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

Posted: Fri Aug 05, 2011 2:41 pm
by AMindForeverVoyaging
CodeX wrote:Aside from the header that is
I guess they intended to write <stdio.h>. :)

Posted: Fri Aug 05, 2011 5:01 pm
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

Posted: Fri Aug 05, 2011 6:34 pm
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 :)

Posted: Fri Aug 05, 2011 7:16 pm
by CodeX
arg, I forgot gcc was c and g++ was the cpp compiler, I tested it on www.codepad.org

Posted: Mon Aug 15, 2011 3:58 pm
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;
}

Posted: Wed Sep 07, 2011 7:40 am
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]

Posted: Wed Sep 07, 2011 8:39 am
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

Posted: Sat Sep 10, 2011 3:12 pm
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();
        }
    }
}

Posted: Sat Oct 08, 2011 1:25 pm
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

Posted: Sat Oct 08, 2011 4:36 pm
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]

Posted: Tue Nov 22, 2011 1:43 am
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

Posted: Thu Nov 24, 2011 9:07 am
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;
}

my c++ code

Posted: Tue Dec 06, 2011 2:21 am
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);
}
:)

Posted: Wed Dec 07, 2011 5:44 am
by AlbusShin
C++ worked for me. A string and some codes