Une drôle d'horloge

Cette semaine, c'est Lothaire qui vous propose un #KataOfTheWeek : The Berlin clock

Briefing du Kata : L’idée est la suivante: chaque lampe allumée indique qu’une certaine durée de temps s’est écoulée.
Plus précisément:

  • La lumière de la première ligne s'allume puis s'éteint à chaque seconde.
  • Chaque lumière de la deuxième ligne (4 au total) représente 5 heures.
  • Chaque lumière de la troisième ligne (4 au total) représente 1 heure.
  • Chaque lumière de la quatrième ligne (11 au total) représente 5 minutes. (les lumières rouges indiquent les quarts d’heure)
  • Chaque lumière de la dernière ligne (4 au total) représente 1 minute.

Les lumières s'allument de gauche à droite.
Les lumières des heures sont rouges, celles des minutes sont jaunes(sauf celles qui indiquent les quarts d'heure qui sont rouges) et la lumière des secondes est jaune (elle est allumée pour les secondes paires et éteinte pour les secondes impaires).

Ici je vous propose de représenter les couleurs par des lettres:

  • Y = jaune
  • R = rouge
  • O = éteint

Le but est de créer une fonction qui prend en paramètre l'heure et qui renvoie une string représentant l'heure affichée sur l'horloge de Berlin.

Exemple: L'heure 13:42:09 correspond sur l'horloge de Berlin à :

         -------
         |  O  |
-------------------------
|  R  |  R  |  O  |  O  |
-------------------------
|  R  |  R  |  R  |  O  |
-------------------------
| Y|Y|R|Y|Y|R|Y|Y|O|O|O |
-------------------------
|  Y  |  Y  |  O  |  O  |
-------------------------

Saurez-vous résoudre le problème ?

Bon courage !


Et voici une solution proposée par l'auteur en Java:

public class BerlinClock {

    public String getBerlinClockAsString(LocalTime time) {
        return computeBerlinClockFromLocalTime(time).toString();
    }

    public BerlinClockTime computeBerlinClockFromLocalTime(LocalTime time) {
        BerlinClockTime berlinClockTime = new BerlinClockTime();
        berlinClockTime.setSeconds(getSeconds(time.getSecond()));
        berlinClockTime.setTopHours(getTopHours(time.getHour()));
        berlinClockTime.setBottomHours(getBottomHours(time.getHour()));
        berlinClockTime.setTopMinutes(getTopMinutes(time.getMinute()));
        berlinClockTime.setBottomMinutes(getBottomMinutes(time.getMinute()));

        return berlinClockTime;
    }

    private String getSeconds(Integer seconds) {
        return seconds%2 == 0 ? Color.YELLOW.getCode() : Color.OFF.getCode();
    }

    private String getTopHours(Integer hours) {
        Integer nbOn = hours/5;

        IntFunction<Color> func = x -> x <= nbOn ? Color.RED : Color.OFF;
        return getRow(func, 4);
    }

    private String getBottomHours(Integer hours) {
        Integer nbOn = hours%5;

        IntFunction<Color> func = x -> x <= nbOn ? Color.RED : Color.OFF;
        return getRow(func, 4);
    }

    private String getTopMinutes(Integer minutes) {
        Integer nbOn = minutes/5;

        IntFunction<Color> func = x -> {
            if (x <= nbOn && x%3 == 0) {
                return Color.RED;
            } else if (x <= nbOn) {
                return Color.YELLOW;
            } else {
                return Color.OFF;
            }
        };
        return getRow(func, 11);
    }

    private String getBottomMinutes(Integer minutes) {
        Integer nbOn = minutes%5;

        IntFunction<Color> func = x -> x <= nbOn ? Color.YELLOW : Color.OFF;
        return getRow(func, 4);
    }


    private String getRow(IntFunction<Color> func, int totalLength) {
        return IntStream
                .range(1, totalLength+1)
                .mapToObj(func)
                .map(Color::getCode)
                .collect(Collectors.joining());
    }

}


public enum Color {
    RED("R"),
    YELLOW("Y"),
    OFF("O");

    private String code;

    Color (String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

public class BerlinClockTime {

    private String seconds;
    private String topHours;
    private String bottomHours;
    private String topMinutes;
    private String bottomMinutes;

    @Override
    public String toString() {
        String firstDelimiter = "         -------\n";
        String delimiter = "-------------------------\n";

        return  firstDelimiter +
                row1element(seconds) +
                delimiter +
                row4elements(topHours) +
                delimiter +
                row4elements(bottomHours) +
                delimiter +
                row11elements(topMinutes) +
                delimiter +
                row4elements(bottomMinutes) +
                delimiter;
    }

    private String row1element(String element) {
        return "         |  " + element + "  |\n";
    }

    private String row4elements(String element) {
        return Arrays.stream(element.split(""))
                .collect(Collectors.joining("  |  ", "|  ", "  |\n"));
    }

    private String row11elements(String element) {
        return Arrays.stream(element.split(""))
                .collect(Collectors.joining("|", "| ", " |\n"));
    }


    public String getSeconds() {
        return seconds;
    }

    public String getTopHours() {
        return topHours;
    }

    public String getBottomHours() {
        return bottomHours;
    }

    public String getTopMinutes() {
        return topMinutes;
    }

    public String getBottomMinutes() {
        return bottomMinutes;
    }

    public void setSeconds(String seconds) {
        this.seconds = seconds;
    }

    public void setTopHours(String topHours) {
        this.topHours = topHours;
    }

    public void setBottomHours(String bottomHours) {
        this.bottomHours = bottomHours;
    }

    public void setTopMinutes(String topMinutes) {
        this.topMinutes = topMinutes;
    }

    public void setBottomMinutes(String bottomMinutes) {
        this.bottomMinutes = bottomMinutes;
    }
}

Votre équipe TakiVeille

TakiVeille

TakiVeille