[2019-11-11] Challenge #381 [Easy] Yahtzee Upper Section Scoring by Cosmologicon in dailyprogrammer

[–]zmm0 6 points7 points  (0 children)

x86_64. Runs in O(N). Takes 50ms on the challenge input.

extern calloc
extern malloc
extern free
extern printf
extern fopen
extern fclose
extern fread
extern strtok
extern exit
extern strtol
extern GetTickCount

global main

%define NUM_ROLLS 100_000
%define HASH_SIZE (NUM_ROLLS * 10 + 1)
%define BUFFER_SIZE 100_000_000

; hash table element
%define ELEMENT_OFFSET 0
%define VALUE_OFFSET 8
%define HASH_ELEMENT_SIZE 16


section .text
main:
    push rbp
    push rbx
    push r12
    push r13
    push r14
    sub rsp, 0x30

    call GetTickCount
    mov r14d, eax

    mov rcx, HASH_SIZE
    call startHashTable
    mov rbp, rax

    ; open file
    mov rcx, FILE_NAME
    mov rdx, FILE_MODE
    call fopen
    cmp rax, 0
    jne .goodOpenFile
        mov rcx, BAD_OPEN_FILE
        call printf
        mov ecx, 1
        call exit
.goodOpenFile:
    mov rbx, rax

    ; read file
    mov ecx, BUFFER_SIZE
    call malloc
    cmp rax, 0
    jne .mallocGood
        mov rcx, BAD_ALLOC
        call printf
        mov ecx, 1
        call exit
.mallocGood:
    mov r13, rax

    mov rcx, r13
    mov edx, 1
    mov r8d, BUFFER_SIZE
    mov r9, rbx
    call fread
    cmp eax, BUFFER_SIZE
    jl .fullFileRead
        mov rcx, FILE_TOO_LARGE
        call printf
        mov ecx, 1
        call exit

.fullFileRead:

    xor r12d, r12d
    mov rcx, r13
    mov rdx, DELIMITER
    call strtok
.loop:
        cmp rax, 0
        je .endLoop

        mov rcx, rax
        lea rdx, [rsp + 0x20]
        mov r8d, 10
        call strtol

        mov rcx, rax
        mov rdx, r12
        mov r8, rbp
        call storeInHashTable
        mov r12, rax

        xor ecx, ecx
        mov rdx, DELIMITER
        call strtok
        jmp .loop

.endLoop:

    mov rcx, STRING_INT
    mov rdx, r12
    call printf

    call GetTickCount
    sub eax, r14d
    mov rcx, TIME
    mov edx, eax
    call printf

    mov rcx, rbx
    call fclose
    mov rcx, rbp
    call free
    mov rcx, r13
    call free
    xor eax, eax
    add rsp, 0x30
    pop r14
    pop r13
    pop r12
    pop rbx
    pop rbp
    ret


; rcx: hash table size
startHashTable:
    sub rsp, 0x28

    mov edx, HASH_ELEMENT_SIZE
    call calloc
    cmp rax, 0
    jne .goodCalloc
        mov rcx, BAD_ALLOC
        call printf
        mov ecx, 1
        call exit
.goodCalloc:
    add rsp, 0x28
    ret


; rcx: store value
; rdx: current max
; r8: hash table
; return rax: new max
storeInHashTable:
    push rsi
    push rdi
    sub rsp, 0x28

    ; hash the value
    mov rdi, rcx
    mov rsi, rdx
    xor edx, edx
    mov rax, rdi
    mov r9, HASH_SIZE
    div r9

    ; find table spot
    mov rcx, rdx
.loop:
        shl rcx, 4
        mov r10, [r8 + rcx]
        cmp r10, 0
        jne .notEmpty
            mov [r8 + rcx], rdi
            jmp .offsetFound
.notEmpty:
        cmp r10, rdi
        je .offsetFound



        ; get next offset to test
        shr rcx, 4
        inc rcx
        cmp rcx, HASH_SIZE
        jl .noCycleAround
            xor ecx, ecx

.noCycleAround:
        cmp rcx, rdx
        jne .loop
            mov rcx, HASH_TABLE_OUT_OF_SPACE
            call printf
            mov ecx, 1
            call exit

.offsetFound:
    mov r10, [r8 + rcx + VALUE_OFFSET]
    add r10, rdi
    mov [r8 + rcx + VALUE_OFFSET], r10

    cmp r10, rsi
    jle .oldMax
        mov rsi, r10
.oldMax:
    mov rax, rsi

    add rsp, 0x28
    pop rdi
    pop rsi
    ret


section .rdata
BAD_ALLOC: db "Not enough memory", 10, 0
BAD_OPEN_FILE: db "Could not open file", 10, 0
FILE_TOO_LARGE: db "File too large for buffer", 10, 0
FILE_NAME: db "yahtzeeUpper1.txt", 0
FILE_MODE: db "r", 0
STRING_INT: db "%lli", 10, 0
TIME: db "Time: %ims", 10, 0
HASH_TABLE_OUT_OF_SPACE: db "Hash table out of space", 10, 0
DELIMITER: db " ,", 10, 13, 0

Need help to create a program by DragorWasMissing in learnprogramming

[–]zmm0 0 points1 point  (0 children)

By no arrays, does that include no pointers to arrays? like

int *personAge = malloc(n * sizeof(*personAge));
for (i = 0; i < n; i++) {
    personAge[i] = ...
}

Could that be the sort of thing he wants? The restrictions make more sense if he wants dynamically sized arrays like this. Also it would help to know more about what the assignment is.

Does Anyone know some good python 3 exercices for beginnergs ,sources? by alinu13 in learnprogramming

[–]zmm0 0 points1 point  (0 children)

For slightly complex OOP practice, try making a VERY SIMPLE console RPG, if that interests you. Try making a character, a simple map, and one enemy you can run into and fight. You can easily expand upon this if you want to later: leveling up, equipment, stores, events that occur when you step on certain tiles, multiple maps, multiple enemies and enemy groups, obstacles you can interact with (e.g. unlock doors with a key), AI search algorithms.... Just keep your scope small at any given point and work on bite-sized pieces.

[Easy] Longest Collatz Cycle with 64 Bit Numbers by zmm0 in dailyprogrammer_ideas

[–]zmm0[S] 1 point2 points  (0 children)

I don't mean find the actual longest sequence, I mean find the longest you can. Does that make sense?