Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)
New Posts  All Forums:Forum Nav:

Programming Challenge (Out-of-Date) - Page 25  

Poll Results: Are you interested in participating in and/or helping organise and post these programming challenges?

 
  • 100% (2)
    I want to participate.
  • 0% (0)
    I want to contribute by helping posting and organise these challenges.
  • 0% (0)
    I'll only take part if other people are willing to participate.
  • 0% (0)
    I can help and participate - I love programming!
  • 0% (0)
    I do not wish to participate or help.
2 Total Votes  
post #241 of 306
I just don't pick my brain for fun, too much work, lol!

Well, I use to, then I went broke, so I decided it was time to get paid for my brain!

Now, I stopped coding mostly and started running my own computer business...

Looking back, I'm very happy I stopped programming, whoosh!

I can't join the competition, not that advanced, but it was nice watching all of you code like, very impressive.
The Rock
(15 items)
 
  
The Rock
(15 items)
 
  
post #242 of 306
Here's mine. I'm not done, but I probably won't have time to finish before tomorrow. It should only need the code to load the square holder and to do the clicks.

I tried not looking at any of your guys' code, just to see if I could come up with something unique. I really wanted to just have to keep track of one square and have it do everything from there, but I figured this would be easier to debug.

I don't read input. It's just loaded in the main method right now.

Code:
import scala.collection.mutable.{MutableList => MList }

case class Square(intVal: Int) {

var top:Square= Square.empty;
var bottom:Square = Square.empty
var left:Square = Square.empty
var right:Square = Square.empty
var dirty = false

def clear() 
    {
top = Square.empty
bottom = Square.empty
right = Square.empty
left = Square.empty
}

def listRep =  List(top , right , bottom , left);

def intEquals(sqr:Square) =  sqr.intVal == intVal;

def canConstrict() =  listRep.filter(x => intEquals(x)).size > 0

def doConstrict() { listRep.filter(x => intEquals(x) && !x.dirty).foreach{y => y.dirty = true; y.doConstrict()}  }

override def toString() = intVal + "<=>"  + (for( i <- listRep) yield i.intVal).toList.toString; 

}

object Square
{
val emptySquare = new Square(-1)
def empty():Square = return emptySquare;

  def main(args : Array[String]) : Unit = {
   println("woohoo");
   var sh = new SquareHolder();
   
   sh.addRow(List(1,1,2,2));
   sh.addRow(List(2,1,2,2));
   sh.addRow(List(2,2,2,1));
   sh.printSquares();
   //sh.doMove(2,2);
   sh.doMove(1,1);
   sh.doMove(1,1);
   
  }

}
class SquareHolder()
{
def addRow(numList:List[Int]) 
{  
for(i <- 0 to (numList.size - buildList.size))
{buildList += new MList[Square]()}
numList.zip(0 to numList.size).foreach{x =>    new Square(x._1) +=: buildList(x._2); };
    yList = (for(col <- buildList) yield List() ++ col).toList 
}

def clear() { yList = List[List[Square]](); buildList = new MList[MList[Square]](); }
var buildList = new MList[MList[Square]]();
var yList = List[List[Square]]()
def click(x:Int, y:Int) { val sq = (yList(x-1))(y-1);
  if(!sq.canConstrict()){ println("Click would do nothing.")}
  else
   { sq.doConstrict(); }   
  }

def printSquares() {
val mx = (yList.map(x => x.size)).max(Ordering[Int])
var st = for(  ht <- (0 to mx).reverse; ls <- yList) yield getOrBlank(ls,ht)
for(line <- st.grouped(yList.size))
{ for(n  <- line) { print(n);}
println("");}
} 

def getOrBlank(sqList:List[Square], idx:Int): String =
{
if(idx >= sqList.size) { return " "; }
else
{ sqList(idx).intVal  + "" }
}

def link() { 
//clear
yList.foreach{ x => x.foreach{ y => y.clear } }
//link left and right squares
yList.sliding(2,1).foreach{ x => 
    if(x.size == 2) {
var leftList = x(0)
var rightList = x(1)
val min = (x.map(y => y.size)).min(Ordering[Int])
for(i <- 0 to min-1)
{
leftList(i).right  = rightList(i)
rightList(i).left = leftList(i)
}
    }
}
//link top and bottom squares
yList.foreach{ x => x.sliding(2,1).foreach{ y => if(y.size == 2) {y(0).top = y(1); y(1).bottom = y(0) } } }

}
def clearDirty():List[List[Square]] = {
val newY = for(y <- yList ) yield y.filter{x:Square => !x.dirty}
newY.toList
}
def removeEmptyColumns():List[List[Square]] = {
val newY = for(y <- yList if(y.filter{x => x != Square.empty }.size > 0)) yield y
newY.toList
}

def remaining():Int = yList.map(x => x.size).sum

def doMove(x:Int, y:Int) { 

link();
click(x,y); 
yList=clearDirty();
yList = removeEmptyColumns();
if(remaining == 0)
{ println("Game Won"); }
else
{printSquares();}

}
}

Edited by impatient - 2/8/11 at 3:05pm
post #243 of 306
I generated a 2000 x 2000 grid, just to see how long the individual things took to process. It tries 100 clicks.

If you need the size to be at the top, you will need to manually add it. It is about 8 megs unzipped. The game won't complete or anything, I just did timing on how long each click took and to see where to speed up my code.

http://f5complex.com/data.txt.bz2
Edited by impatient - 2/9/11 at 8:54am
post #244 of 306
Here is the new one. Sorry it is late I forgot about it and was running imps massive grid to watch it break

Code:
Problem C - Machined Surfaces

An imaging device furnishes digital images of two machined surfaces that eventually will be assembled in contact with each other.The roughness of this final contact is to be estimated.
A digital image is composed of the two characters, "X" and " " (space). There are always 25 columns to an image, but the number of rows, N, is variable. Column one (1) will always have an "X" in it and will be part of the left surface. The left surface can extend to the right from column one (1) as contiguous X's. Similarly, column 25 will always have an "X" in it and will be part of the right surface. The right surface can extend to the left from column 25 as contiguous X's.

Digital-Image View of Surfaces
       Left         Right

       XXXX         XXXXX  <-- 1
       XXX        XXXXXXX
       XXXXX         XXXX
       XX          XXXXXX
       .                .
       .                .
       .                .
       XXXX          XXXX
       XXX         XXXXXX  <-- N
       |                |
       1               25
In each row of the image, there can be zero or more space characters separating the left surface from the right surface. There will never be more than a single blank region in anyrow.
Foreach image given, you are to determine the total "void" that will exist after the left surface has been brought into contact with the right surface. The "void" is the total count of the spaces that remains between the left and right surfaces after they have been brought into contact.

The two surfaces are brought into contact by displacing them strictly horizontally towards each other until a rightmost "X" of the left surface of some row is immediately to the left of the leftmost "X" of the right surface of that row. There is no rotation or twisting of these two surfaces as they are brought into contact; they remain rigid, and only move horizontally.

Note: The original image may show the two surfaces already in contact, in which case no displacement enters into the contact roughness estimation.

Input

The input consists of a series of digital images. Each image data set has the following format:
First line -- A single unsigned integer, N, with value greater than zero (0) and less than 13. The first digit of N will be the first character on a line.
Next N lines -- Each line has exactly 25 characters; one or more X's, then zero or more spaces, then one or more X's.

The end of data is signaled by a null data set having a zero on the first line of an image data set and no further data.

Output

For each image you receive as a data set, you are to reply with the total void (count of spaces remaining after the surfaces are brought into contact). Use the default output for a single integer on a line.
Sample Input

4
XXXX                XXXXX
XXX               XXXXXXX
XXXXX                XXXX
XX                 XXXXXX
2
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXX              XX
0

Output for the Sample Input

4
0
0
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #245 of 306
Quote:
Originally Posted by Midpipps View Post
Here is the new one. Sorry it is late I forgot about it and was running imps massive grid to watch it break
sure sure. you beat mine
post #246 of 306
Quote:
Originally Posted by Midpipps View Post
Here is the new one. Sorry it is late I forgot about it and was running imps massive grid to watch it break

Code:
Problem C - Machined Surfaces
I guess this is technically a one liner... a really long one. The last bit, where I assign vals seems like cheating.

Code:
io.Source.fromFile(new java.io.File("data.txt")).getLines().map{x:String => if(('0' to '9').mkString.contains(x)) { "~"} else{ x.filter( y => y.equals('X')).size} }.mkString(" ").split("~").filter(x => x.size > 0).foreach{x:String => val sz = x.split(" ").filter{y:String => y.length > 0}.map( _.toInt); val mx = sz.max(Ordering[Int]); println(sz.map( mx - _).sum)}

Edited by impatient - 2/10/11 at 7:18am
post #247 of 306
here is my take on it not as schnazzy as imps 1 liner but it works mine is 1 line as long as it is hidden by a spoiler tag at least until you click it


Code:
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class MachinedSurface {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(new FileReader("machinedin.txt"));
while(in.hasNext() && !in.hasNext("0")){
int rows = Integer.parseInt(in.nextLine());
int[] highAndLow = {Integer.MAX_VALUE, 0};//if there are more spaces then max value program will break regardless
for (int i = 0; i < rows; i++){
String temp = in.nextLine();
int spaces = 0;
if (temp.contains(" "))
spaces = temp.lastIndexOf(' ') - temp.indexOf(' ') + 1;
if (highAndLow[0] > spaces)
highAndLow[0] = spaces;
highAndLow[1] += spaces;
}
System.out.println(highAndLow[1] - (highAndLow[0] * rows));
}
in.close();
}
}
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #248 of 306
Could have chopped out a few lines, but wanted to play with regex


Code:
import java.io.*;
import java.util.regex.*;

public class machinedSurfaces{
    public static void main(String[] args){
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            int count = 0;
            String line = in.readLine();
            while ((count = Integer.parseInt(line)) != 0){
                int min = 25, sum = 0;
                for (int i=0; i< count; i++){
                    line = in.readLine();
                    if (line == null || line.length() != 25)
                        throw new Exception("Bad input: " + line + " length <> 25");
                    Pattern pattern = Pattern.compile("[X]+([\\\\s]*)[X]*");
                    Matcher matcher = pattern.matcher(line);
                    if (!matcher.matches())
                        throw new Exception("Input: " + line + " doesnt match regex");
                    int sp_cnt = matcher.group(1).length();
                    if (sp_cnt < min)
                        min = sp_cnt;
                    sum = sum + sp_cnt;
                    }
                System.out.println(sum - (min * count));
                line = in.readLine();
                }
            }
        catch (Exception e){
            e.printStackTrace();
            }
        }
    }

Edited by wimcle - 2/10/11 at 10:51am
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
post #249 of 306
Quote:
Originally Posted by wimcle View Post
Could have chopped out a few lines, but wanted to play with regex

How do you do the show/hide ?

Code:
good stuff
It is using the {spoiler="Your spoiler Message"]Whatever you want hidden{/spoiler] replacing the { with [ of course
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #250 of 306
I promise, I will find time to do another challenge.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
This thread is locked  
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)