• What are you working on? V10
    2,002 replies, posted
I need some help with Project Euler 8. Here's my code, but apparently I'm getting the wrong answer. [cpp] #define NUM_PATH "C:\\Users\\Bryce\\Documents\\num.txt" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream inFile; string s; inFile.open(NUM_PATH, ios::in); if(!inFile) { cerr << "Error reading file!" << endl; system("pause"); return 1; } while(!inFile.eof()) getline(inFile, s); inFile.close(); long ans = 0; for(int i = 0; i < s.length() - 5; i++) { long sum = 1; for(int j = 0; j < 5; j++) sum *= s[i+j]; cout << sum << endl; if(sum > ans) ans = sum; } cout << "Answer: " << ans << endl; system("pause"); return 0; }[/cpp]
Diagonals. Also, you have to make sure you aren't reading from one side of the block to the other. Wait I'm being :downs:, I was thinking of a different challenge. Disregard this.
For those who didn't know, the challenge is to find the greatest product of 5 consecutive numbers in a thousand digit number.
I just pressed ctrl-f and searched for 9's... automerge
[QUOTE=ryandaniels;22207346]Diagonals. Also, you have to make sure you aren't reading from one side of the block to the other.[/QUOTE] Fuck. I didn't think of that. I had it all on one line. Are diagonals consecutive? [editline]10:19PM[/editline] [QUOTE=ryandaniels;22207355]automerge[/QUOTE] this.
[QUOTE=Agent766;22207353]For those who didn't know, the challenge is to find the greatest product of 5 consecutive numbers in a thousand digit number.[/QUOTE] Ah, yeah, sorry, was remembering some other problem. [editline]08:20PM[/editline] [QUOTE=Agent766;22207360]Fuck. I didn't think of that. I had it all on one line. Are diagonals consecutive? [editline]10:19PM[/editline] this.[/QUOTE] No, you are right (at least with what I was saying), I was wrong. [editline]08:21PM[/editline] Number 11, I was thinking of number 11. [editline]08:24PM[/editline] I think I know what's going wrong, you are reading in characters; to get their value you have to subtract 48 from them. Actually, that shouldn't make a difference either, seeing how they are still larger than each other in the right way.
Yeah, I just realized that. Thanks anyway ^^
When you are reading the file to s, you are overwriting s each time with the new line.
you can type shit in [media]http://www.youtube.com/watch?v=qr28bNTERn8[/media]
[QUOTE=i300;22205733]I thought this was that one place with smart people not that one place with people trollin around like its nobodies business.[/QUOTE] It used to be. Well, at least the trolls were intelligent ones.
Yay, I set up a getline routine that reads entry in from the keyboard and stores it in a buffer beginning at DS:SI There's a few problems, firstly being buffer overflow (in which case the pointer will wrap back around and start overwriting executable code :/), the second being that sufficiently long input could collide with the number stack, and the third being that I only have 100 bytes left before the thing isn't able to fit in a floppy's bootsector anymore. I don't really care about the first two issues, but I'll be damned if I'm going to have to write a whole bootloader for this thing if it runs over my 510 byte limit. [editline]03:43PM[/editline] Schweet, it can actually calculate now! You can type in numbers, and it'll push it to the number stack. Typing . peeks the stack and typing + pops two numbers off the stack, adds them and pushes the result back. They're the only operations implemented at the moment because it currently weighs up at 509 bytes. Although a floppy's sectors are 512 bytes long, for technical reasons I'm limited to 510 bytes for my code. Now I'm going to optimize this thing for size and see if I can fit any more operations in. [img]http://ahb.me/2OG[/img] Here's the source if anyone's interested: [code] ;; keys: ; escape = 27 ; backsp = 8 ; enter = 13 use16 org 0x7c00 mov ax, 0xb800 mov es, ax mov ax, 0x7e00 mov fs, ax mov sp, 0x7bff push cs pop ds mov si, hello call putstr call nextline call nextline main: mov si, 0x200 call getline ; try parse a number mov al, [ds:si] cmp al, '0' jl .nan cmp al, '9' jg .nan mov [sum], word 0 .numloop: lodsb or al, al jz .endloop mov cl, al ; multiply sum by 10 mov ax, [sum] xor dx, dx mov bx, 10 mul bx ; get number from current char sub cl, '0' xor ch, ch ; add to sum add ax, cx ; write back mov [sum], ax jmp .numloop .endloop: mov ax, [sum] call pushax call putax call nextline jmp main .nan: cmp al, '.' je .showpop cmp al, '+' je .doadd jmp main .showpop: call popax call pushax jmp .end .doadd: call popax mov bx, ax call popax add ax, bx call pushax .end: call putax call nextline jmp main sum dw 0 jmp $ hello db 'RPN16 by turb_', 0 prompt db '>> ', 0 ;eval db '=> ', 0 ; Set SI to where you want the string to be stored getline: pusha mov bx, si mov cx, bx mov si, prompt call putstr .loop: call getkey or ax, ax jz .loop cmp ax, 8 je .backspace cmp ax, 27 je .escape cmp ax, 13 je .enter jmp .character .backspace: call backspace cmp bx, cx je .loop dec bx jmp .loop .escape: call clearline mov si, prompt call putstr mov bx, cx jmp .loop .enter: call nextline mov [ds:bx], byte 0 popa ret .character: mov [ds:bx], al call putchar inc bx jmp .loop ; RETURNS KEY PRESSED IN AX getkey: pusha mov ah, 0 int 0x16 mov ah, 0 mov [retval], ax popa mov ax, [retval] ret pushax: pusha mov bx, [stackpos] sub bx, 2 mov [fs:bx], ax mov [stackpos], bx popa ret ; RETURNS STACK NUMBER IN AX popax: pusha mov bx, [stackpos] mov ax, [fs:bx] add bx, 2 mov [stackpos], bx mov [retval], ax popa mov ax, [retval] ret stackpos dw 0xffff backspace: pusha mov bx, [column] cmp bx, 8 jl .return sub bx, 2 mov [column], bx mov al, ' ' call putchar mov [column], bx call putcursor .return: popa ret clearline: pusha mov bx, 80*2*24 .clr: mov [es:bx], word (7 << 8) add bx, 2 cmp bx, 80*2*25 jl .clr mov [column], byte 0 call putcursor popa ret putcursor: pusha mov dx, [column] shr dl, 1 mov ah, 0x02 mov bh, 0x00 mov dh, 24 int 0x10 popa ret nextline: pusha xor bx,bx .loop: mov ax, [es:bx+(80*2)] mov [es:bx], ax add bx, 2 cmp bx, 80*2*24 jl .loop mov ax, [column] mov ax, 0 mov [column], ax call clearline call putcursor popa ret putchar: pusha mov bx, [column] mov [es:bx+(80*2*24)], al add bx, 2 cmp bx, 160 jl .writeback call nextline popa ret .writeback: mov [column], bx call putcursor popa ret putstr: pusha .loop: lodsb or al, al jz .return call putchar jmp .loop .return: popa ret putax: pusha mov cx, sp push 42 mov bx, 10 .a: xor dx, dx div bx push dx or ax, ax jz .b jmp .a .b: pop ax cmp ax, 42 je .c add ax, '0' call putchar jmp .b .c: mov sp, cx popa ret column dw 0 retval dw 0 times 510-($-$$) db 0 db 0x55 db 0xAA [/code] Runs on real hardware too, which is pretty cool: [img]http://anyhub.net/file/img_2479_small.jpg[/img]
[QUOTE=NovembrDobby;22196140]Oh, thanks, those are the only bits I translated so far (just to show it off) and my French is a bit rusty. Are you sure about 'Jeu libre' though, because it's defining the size of 'free' games so I thought it'd be plural?[/QUOTE] I am. Can't explain why, but it's just like that (it's my native language). If you ever needed help translating, I'll be glad to, just PM me.
I have been working on a mud system for lua, not a multi user dungeon with lua scripting one that is made with 100% lua, no real content yet but here is some code making a simple world. [code] local room1 = "Portal Zone" local room2 = "Field" local room3 = "Warehouse" local room4 = "House" local plyname = "Vbitz" world.addRoom( room1 ) world.setRoomDescription( room1, "Field with a portal where you entered the game" ) world.addRoom( room2 ) world.setRoomDescription( room2, "Field where you can see a house, portal, warehouse in the distince" ) world.addRoom( room3 ) world.setRoomDescription( room3, "Warehouse full of random stuff in boxes floor to roof" ) world.addRoom( room4 ) world.setRoomDescription( room4, "Small Family House" ) world.linkRoom( room1, room2, "travel to field", "travel to portal" ) world.linkRoom( room2, room3, "enter warehouse", "exit warehouse" ) world.linkRoom( room2, room4, "enter house", "exit house" ) [/code]
I've always wanted to create a text adventure that generates its own descriptions, but I think that accurate and sufficiently varying and interesting descriptions would require that the world is already modeled in a way which could allow more accurate methods of displaying things So it'd actually just be an ordinary adventure game with an UI that translates the world data into inefficient descriptions and advanced input into more basic commands.
Anyway to format this and not have it look like ass? [code]int ptr = ReadInt( ReadInt( ReadInt( ReadInt( ReadInt(g_pHost) ) + 0x78 ) ) + 0x10 ) + 0x4434;[/code] Also been trying to get into the habit of documenting how I found something that wasn't common sense when reversing. Cause I always come back to my notes a month later and end up not understanding a thing.
nvm
Cut the size of my RPN calculator down by 71 bytes (quite significant) by using neat tricks like stack modification and bastardizing registers by using them in ways they were never intended for. Instead of returning values by saving them in RAM before the POPA and moving the value back into AX, I've just saved them onto the stack at the location that PUSHA stored AX. I'm using BP (base pointer, stack related register) as a 5th register in the code that parses a string into a number, and I'm using GS (a segment register) to hold the current column that the cursor is on.
[QUOTE=turb_;22211164]Cut the size of my RPN calculator down by 71 bytes (quite significant) by using neat tricks like stack modification and bastardizing registers by using them in ways they were never intended for. Instead of returning values by saving them in RAM before the POPA and moving the value back into AX, I've just saved them onto the stack at the location that PUSHA stored AX. I'm using BP (base pointer, stack related register) as a 5th register in the code that parses a string into a number, and I'm using GS (a segment register) to hold the current column that the cursor is on.[/QUOTE] wow that's so exciting. [highlight](User was banned for this post ("Why reply?" - SteveUK))[/highlight]
[QUOTE=diplo;22211549]wow that's so exciting.[/QUOTE] Go back to Web Programming troll.
[QUOTE=ryandaniels;22207603]When you are reading the file to s, you are overwriting s each time with the new line.[/QUOTE] It's all on one line in the text file. [editline]06:34AM[/editline] Should I be subtracting 44 (or whatever that number was) or using atoi to convert from ASCII to an int?
[QUOTE=diplo;22211549]wow that's so exciting.[/QUOTE] It is for me, because it gives me enough space to implement subtraction, multiplication and division. I'll listen to your baseless criticism when you post impressive content of your own, until then, get the fuck out.
[QUOTE=Agent766;22211782]It's all on one line in the text file. [editline]06:34AM[/editline] Should I be subtracting 44 (or whatever that number was) or using atoi to convert from ASCII to an int?[/QUOTE] [url]http://www.asciitable.com/[/url] Subtract 48 I had assumed you were using multiple lines because of the while loop. And yes, as you are saying, I was right with my earlier statement about the ascii, as I somehow forgotten you were going right for the correct answer, not finding where it takes place; ie, I was thinking that the correct string would end up giving you the largest answer, however the answer itself would be wrong. I must have been really tired last night :v:
[QUOTE=turb_;22211955]It is for me, because it gives me enough space to implement subtraction, multiplication and division. I'll listen to your baseless criticism when you post impressive content of your own, until then, get the fuck out.[/QUOTE] just ignore the trolls man.
Why don't the trolls here ever get banned like they do in other sections :< That's how it seems, anyway.
Just report the post and hopefully something will get done about it
[url=http://love2d.org/]Goddamnit[/url]. As soon as they're back up I'm downloading the entire API reference, I can't rely on their website when I need it :saddowns:
[QUOTE=Clavus;22212440][url=http://love2d.org/]Goddamnit[/url]. As soon as they're back up I'm downloading the entire API reference, I can't rely on their website when I need it :saddowns:[/QUOTE] [url]http://love2d.org/docs/[/url] it's just the main page.
[QUOTE=efeX;22212470][url]http://love2d.org/docs/[/url] it's just the main page.[/QUOTE] Look at the version. That's the old 0.3.2 page, the 0.6.2 docs are on the wiki, which is down.
[QUOTE=Agent766;22207283]I need some help with Project Euler 8. Here's my code, but apparently I'm getting the wrong answer. [cpp] #define NUM_PATH "C:\\Users\\Bryce\\Documents\\num.txt" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream inFile; string s; inFile.open(NUM_PATH, ios::in); if(!inFile) { cerr << "Error reading file!" << endl; system("pause"); return 1; } while(!inFile.eof()) getline(inFile, s); inFile.close(); long ans = 0; for(int i = 0; i < s.length() - 5; i++) { long sum = 1; for(int j = 0; j < 5; j++) sum *= s[i+j]; cout << sum << endl; if(sum > ans) ans = sum; } cout << "Answer: " << ans << endl; system("pause"); return 0; }[/cpp][/QUOTE] I think your problem is from loading the number from the text file. Try hard coding the number in and see if you get the right answer. I did it quick like this: [cpp] #include <stdio.h> #include <string> void main() { const char fullNumber[1001] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"; int greatestProduct = 0; int greatestProductPosition = 0; for (int i=0;i<995;i++) { int product = 1; for (int j=0;j<5;j++) { product *= fullNumber[i+j]-48; } if (product >= greatestProduct) { greatestProduct=product; greatestProductPosition = i; } } char finalDigits[6]; memcpy(&finalDigits[0],&fullNumber[greatestProductPosition],sizeof(char)*5); finalDigits[5]=0; printf("The greatest product of five consecutive digits is %d (%s) \n",greatestProduct,finalDigits); return; } [/cpp]
[QUOTE=NovembrDobby;22203822]Have you posted this before? It looks good, tell us more :smile:[/QUOTE] Working on it but I keep being lazy and stopping for a week.
Sorry, you need to Log In to post a reply to this thread.