Advertisement
alefhidalgo

TheOtherClock

Jun 20th, 2011
1,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.util.Scanner;
  2. /**
  3.  * Tuenti Programming Contest
  4.  * Challenge 13: The other clock
  5.  * @author alefhidalgo [at] gmail [dot] com
  6.  */
  7. public class TheOtherClock {
  8.    
  9.     public static final int SECOND_ZERO_LEDS = 36; /*Total leds ON at t=0*/
  10.     private int[] ledDisplay;
  11.     private int[] lastLedDisplay;  
  12.     {   /* initialization block */ 
  13.         ledDisplay = new int[6];
  14.         lastLedDisplay = new int[6];
  15.     }
  16.    
  17.     /**
  18.      * getLedsVariations: Get led variations between possible digits transition
  19.      * @param lastDigit
  20.      * @param newDigit
  21.      * @return
  22.      */
  23.     private int getLedsVariations(int lastDigit, int newDigit) {
  24.         //all possible transitions in a led display
  25.         if(lastDigit == 0 && newDigit == 1) return 0;
  26.         if(lastDigit == 1 && newDigit == 2) return 4;
  27.         if(lastDigit == 2 && newDigit == 3) return 1;
  28.         if(lastDigit == 3 && newDigit == 4) return 1;
  29.         if(lastDigit == 4 && newDigit == 5) return 2;
  30.         if(lastDigit == 5 && newDigit == 6) return 1;
  31.         if(lastDigit == 6 && newDigit == 7) return 1;
  32.         if(lastDigit == 7 && newDigit == 8) return 4;
  33.         if(lastDigit == 8 && newDigit == 9) return 0;
  34.         if(lastDigit == 9 && newDigit == 0) return 1;
  35.         if(lastDigit == 5 && newDigit == 0) return 2;
  36.         return 0; /*no variations*/
  37.     }
  38.    
  39.     /**
  40.      * Get Total led variations between last and new state
  41.      * @return
  42.      */
  43.     private int getTotalVariations(){
  44.         int sumVariations = 0;
  45.         for (int i = 0; i < ledDisplay.length; i++) {          
  46.             sumVariations += getLedsVariations (lastLedDisplay[i], ledDisplay[i]);         
  47.         }
  48.         return sumVariations;
  49.     }
  50.        
  51.     /**
  52.      * Set time in the led Display
  53.      * @param h
  54.      * @param m
  55.      * @param s
  56.      */
  57.     private void setTime(int h, int m, int s) {
  58.         String sHours = String.valueOf(h);
  59.         String sMin = String.valueOf(m);
  60.         String sSec = String.valueOf(s);
  61.         if (sHours.length() > 1) {
  62.             ledDisplay[4] = Integer.parseInt(sHours.substring(1, 2));
  63.             ledDisplay[5] = Integer.parseInt(sHours.substring(0, 1));
  64.         } else {
  65.             ledDisplay[4] = Integer.parseInt(sHours.substring(0, 1));
  66.             ledDisplay[5] = 0;
  67.         }
  68.  
  69.         if (sMin.length() > 1) {
  70.             ledDisplay[2] = Integer.parseInt(sMin.substring(1, 2));
  71.             ledDisplay[3] = Integer.parseInt(sMin.substring(0, 1));
  72.         } else {
  73.             ledDisplay[2] = Integer.parseInt(sMin.substring(0, 1));
  74.             ledDisplay[3] = 0;
  75.         }
  76.  
  77.         if (sSec.length() > 1) {
  78.             ledDisplay[0] = Integer.parseInt(sSec.substring(1, 2));
  79.             ledDisplay[1] = Integer.parseInt(sSec.substring(0, 1));
  80.         } else {
  81.             ledDisplay[0] = Integer.parseInt(sSec.substring(0, 1));
  82.             ledDisplay[1] = 0;
  83.         }
  84.        
  85.     }
  86.    
  87.     /**
  88.      * getTotalLedsOn
  89.      *  Get total leds ON in a 7 display clock from t=0 to t=seconds
  90.      * @param seconds
  91.      * @return
  92.      */
  93.     public int getTotalLedsOn(int seconds) {
  94.         int sumLedsOn = TheOtherClock.SECOND_ZERO_LEDS;    
  95.         for (int i = 0; i <= seconds; i++) {
  96.             int seg = i % 60;
  97.             int min = (i / 60) % 60;
  98.             int hours = (i / 3600) % 12;
  99.             setTime(hours, min, seg);                              
  100.             if(i>0) {
  101.                 //get only variations between off/on leds
  102.                 sumLedsOn += getTotalVariations();
  103.             }
  104.             //save the last display state in order to detect the variations
  105.             System.arraycopy(ledDisplay, 0, lastLedDisplay, 0, ledDisplay.length);
  106.         }
  107.         return sumLedsOn;
  108.     }
  109.    
  110.     /* For Debug purpose */
  111. //  private void printLedDisplay() {
  112. //      System.out.println(ledDisplay[5] + "" + ledDisplay[4] + ":" + ledDisplay[3] + "" + ledDisplay[2] + ":" + ledDisplay[1] + "" + ledDisplay[0]);
  113. //  }
  114. // 
  115.     public static void main(String args[]) {
  116.         TheOtherClock theClock = new TheOtherClock();      
  117.         Scanner in = new Scanner(System.in);
  118.         while (in.hasNextInt()) {
  119.             System.out.println(theClock.getTotalLedsOn(in.nextInt()));
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement