Created
December 22, 2016 02:22
-
-
Save tomkel5/04d3047481745604b337572ab8a3670a to your computer and use it in GitHub Desktop.
Daily Programmer Challenge #296: https://www.reddit.com/r/dailyprogrammer/comments/5j6ggm/20161219_challenge_296_easy_the_twelve_days_of/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tomkel.dailyprogrammer; | |
import com.sun.deploy.util.StringUtils; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Challenge296 { | |
private static class DayOfChristmas { | |
int date; | |
String ordinal; | |
String gift; | |
DayOfChristmas(int date, String ordinal, String gift) { | |
this.date = date; | |
this.ordinal = ordinal; | |
this.gift = gift; | |
} | |
} | |
private static class DaysOfChristmas extends ArrayList<DayOfChristmas> { | |
/** | |
* Get a single verse | |
* @param dayOfChristmas The current day | |
* @return String | |
*/ | |
private String getVerse(DayOfChristmas dayOfChristmas) { | |
List<String> lines = new ArrayList<>(dayOfChristmas.date + 2); | |
lines.add(String.format("On the %s day of Christmas", dayOfChristmas.ordinal)); | |
lines.add("my true love sent to me"); | |
lines.addAll( | |
stream().filter(d -> d.date <= dayOfChristmas.date) | |
.sorted((day1, day2) -> Integer.compare(day2.date, day1.date)) // Backwards | |
.map( | |
d -> String.format( | |
"%s%s %s", | |
(d.date == 1 && dayOfChristmas.date > 1) ? "and " : "", | |
d.date, | |
d.gift | |
) | |
) | |
.collect(Collectors.toList()) | |
); | |
return StringUtils.join(lines, "\n"); | |
} | |
/** | |
* Get the entire song | |
* @return String | |
*/ | |
private String getSong() { | |
List<String> verses = stream().sorted((i, j) -> Integer.compare(i.date, j.date)) | |
.map(this::getVerse) | |
.collect(Collectors.toList()); | |
return StringUtils.join(verses, "\n\n"); | |
} | |
} | |
public static void main(String[] args) { | |
DaysOfChristmas daysOfChristmas = new DaysOfChristmas() {{ | |
add(new DayOfChristmas(1, "first", "Partridge in a Pear Tree")); | |
add(new DayOfChristmas(2, "second", "Turtle Doves")); | |
add(new DayOfChristmas(3, "third", "French Hens")); | |
add(new DayOfChristmas(4, "fourth", "Calling Birds")); | |
add(new DayOfChristmas(5, "fifth", "Golden Rings")); | |
add(new DayOfChristmas(6, "sixth", "Geese a Laying")); | |
add(new DayOfChristmas(7, "seventh", "Swans a Swimming")); | |
add(new DayOfChristmas(8, "eighth", "Maids a Milking")); | |
add(new DayOfChristmas(9, "ninth", "Ladies Dancing")); | |
add(new DayOfChristmas(10, "tenth", "Lords a Leaping")); | |
add(new DayOfChristmas(11, "eleventh", "Pipers Piping")); | |
add(new DayOfChristmas(12, "twelfth", "Drummers Drumming")); | |
}}; | |
System.out.print(daysOfChristmas.getSong()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment