Saturday, 14 October 2017

My CodeInGame Adventures - Power of Thor - Episode 1

My first CodeInGame experience was running Thor across a grassy field. We had quite the adventure, Thor and I. If it were not for me, he would have died. I'd say he owes me one!

Instructions from CodeInGame :

Thor moves on a map which is 40 wide by 18 high. Note that the coordinates (X and Y) start at the top left! This means the most top left cell has the coordinates "X=0,Y=0" and the most bottom right one has the coordinates "X=39,Y=17".

Once the program starts you are given:
the variable lightX: the X position of the light of power that Thor must reach.
the variable lightY: the Y position of the light of power that Thor must reach.
the variable initialTX: the starting X position of Thor.

the variable initialTY: the starting Y position of Thor.

At the end of the game turn, you must output the direction in which you want Thor to go among:

N (North)
NE (North-East)
E (East)
SE (South-East)
S (South)
SW (South-West)
W (West)
NW (North-West)

Each movement makes Thor move by 1 cell in the chosen direction.



Here's the code I submitted:



import sys
import math

# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
current_x = initial_tx
current_y = initial_ty

# game loop
while True:
    remaining_turns = int(input())  # The remaining amount of turns Thor can move. 
    
    first = ""
    second = ""
    final = ""
    
    EAST = (light_x - current_x)
    WEST = (current_x - light_x)
    NORTH = (current_y - light_y)
    SOUTH = (light_y - current_y)
    
    if NORTH > SOUTH:
        first = "N"
        current_y -=1
        if ( EAST > NORTH or WEST > NORTH ):
            first = ""
            current_y +=1
    elif NORTH < SOUTH:
        first = "S"
        current_y +=1
        if ( EAST > SOUTH or WEST > SOUTH ):
            first = ""
            current_y -=1

    if EAST > WEST:
        second = "E"
        current_x +=1
        if ( NORTH > EAST or SOUTH > EAST ):
            second = ""
            current_x -=1
    elif EAST < WEST:
        second = "W"
        current_x -=1
        if ( NORTH > WEST or SOUTH > WEST ):
            second = ""
            current_x +=1

    final = first + second    
    print(final)

    #print("Debug messages...", file=sys.stderr)
    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr)
    # A single line providing the move to be made: N NE E SE S SW W or NW



Drop me a line if you wanna talk programming.

~ Friar Greg

My CodeInGame Adventures - Horse Racing

Another CodeInGame puzzle I solved. This one wasn't too hard, only an afternoon. Don't tease me, I'm still getting back into the swing of things. What bugs me most is learning all the built-ins and special quirks.

Input
Line 1: Number N of horses

The N following lines: the strength Pi of each horse. Pi is an integer.
Output
The difference D between the two closest strengths. D is an integer greater than or equal to 0.
Constraints
1 < N < 100000
0 < Pi ≤ 10000000
Example
Input
9

Output
1
And the code I submitted:

import sys
import math

## Helper functions
def mycompare(horse_a, horse_b):
    if horse_a > horse_b:
        return horse_a - horse_b, horse_b
    elif horse_a < horse_b:
        return horse_b - horse_a, horse_a
    else:
        return 0, horse_b

## get all the data
last_horse, closest = 99999999,99999999
horse_list=[]
horses = int(input())
for horse in range(horses):
    this_horse = int(input())
    horse_list.append(this_horse)

## let's sort the strengths
temp = sorted(horse_list,reverse=True)

## test for differences in strength
for this_horse in temp:
    ## compare the differences
    this_compare, last_horse = mycompare(this_horse,last_horse)
    if this_compare < closest:
        ## put the lessor difference into our flag
        closest = this_compare

print(str(closest))


Again, if you wanna talk code, just drop a comment below.
~ Friar Greg

Friday, 13 October 2017

My CodeInGame Adventures - Chuck Norris

So... [cough, cough, too much dust here...]

I've been messing around with a site called www.codeingame.com and I'm having a blast working out the rust in my old programming skills while building new skills in python 3 which I never really pushed beyond the basic learning. I want to thank my old buddy Milo (labrat) for encouraging me in this. 

Anyway, This entry is about a puzzle I completed called Chuck Norris.

The instructions were relatively easy, even if it took me a couple tries to figure it out.

Here is the encoding principle:


The input message consists of ASCII characters (7-bit)
The encoded output message consists of blocks of 0
A block is separated from another block by a space
Two consecutive blocks are used to produce a series of same value bits (only 1 or 0 values):
  • First block: it is always 0 or 00. If it is 0, then the series contains 1, if not, it contains 0
  • Second block: the number of 0 in this block is the number of bits in the series

Example

Let’s take a simple example with a message which consists of only one character: Capital C. C in binary is represented as 1000011, so with Chuck Norris’ technique this gives:

0 0 (the first series consists of only a single 1)
00 0000 (the second series consists of four 0)
0 00 (the third consists of two 1)
So C is coded as: 0 0 00 0000 0 00

Second example, we want to encode the message CC (i.e. the 14 bits 10000111000011) :

0 0 (one single 1)
00 0000 (four 0)
0 000 (three 1)
00 0000 (four 0)
0 00 (two 1)
So CC is coded as: 0 0 00 0000 0 000 00 0000 0 00


Here's the code I submitted for the win:

import sys
import math

message = input()
# Convert to binary phrases and rejoin as string
bin_message = ' '.join(format(ord(x), 'b') for x in message)

# debugging examples
#print("from input() = %s" % message, file=sys.stderr)
#print("bin_message from input() = %s" % bin_message, file=sys.stderr)

####################### copy and paste between here only ##################################
## this is just a note for me when going back and forth to IDLE


## they thought they could get me by throwing in ASCII characters with leading 0's
def fix_short_bin(raw_ord):
    #temp = raw_ord.split(" ")
    buffered = []
    for item in raw_ord.split(" "):
        buffered += item.rjust(7, "0") + " "
    return buffered[:-1]

## meat of the app, scan through entire string and count instances of each "stringed" bit
def encode_msg(binary_string):
    first_block = { 1 : "0 ", 0 : "00 " }
    count = 0
    last_read = ""
    half_encoded = ""
    for byte_char in binary_string:
        for bit_char in byte_char:
            # another debugging example
            # print("count = %d   last_read = %s" % (count, last_read))
            if last_read == "": ## if this is the first character count it and keep going
                last_read = bit_char
                count += 1
            elif last_read.isspace(): 
                continue
                ## ignore the spaces between byte_char's
                ## only left them in for debugging asthetics
            elif bit_char == last_read: ## keep counting if they're the same
                count += 1
            elif (bit_char == "1" or bit_char == "0") and (bit_char != last_read):
                ## I had an issue with this code counting spaces as well
                half_encoded += (first_block[int(last_read)] + ("0" * count) + " ")
                count = 1 ## restart the count for the next ord
                last_read = bit_char
    ## since the append is driven by the next char, last char needs to be added at eol
    half_encoded += (first_block[int(last_read)] + ("0" * count))
        
 #   print(half_encoded, file=sys.stderr)
    #print(half_encoded)
    return half_encoded


####################### copy and paste between here only ##################################


print(encode_msg(fix_short_bin(bin_message)))

#print("returned = %s" % encode_msg(bin_message), file=sys.stderr)


If you have any questions about this puzzle then by all means, go to CodeInGame and try it out yourself. If you want to discuss my code specifically, then just comment below.

~ Friar Greg

Saturday, 8 August 2015

Cardinal visits Mom's Garden

As you can see here, my mother's garden attracts attention from all sorts.

Also: Go JAYS!!! Nice job against NY last night. Maybe we'll see another World Series!!

Friar Greg

Thursday, 3 April 2014

The End

Soldiers, sailors, sages who know;
King, baker, taxer, server, ho;
All skilled of targe, tool or tongue,
Take care,
The end's begun.

Man, woman, gaurdian, babe;
Angel, deamon, squirrel in glade;
There is no distance to run,
Beware,
The end's begun.

Of blood, scale, feather, shell;
Who swim, fly, walk, delve;
All songs singing and sung,
DESPAIR!!!
THE END HAS COME!!!

------
Friar Greg