Guest User

Untitled

a guest
Apr 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. package com.jacknie.doodle.kakao;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Queue;
  7. import java.util.StringTokenizer;
  8. import java.util.concurrent.LinkedBlockingQueue;
  9. import java.util.stream.Collectors;
  10.  
  11. public class KakaoTest4 {
  12.  
  13. public static void main(String[] args) {
  14. System.out.println(getBusTime(1, 1, 5, new String[] {"08:00", "08:01", "08:02", "08:03"}));
  15. System.out.println(getBusTime(2, 10, 2, new String[] {"09:10", "09:09", "08:00"}));
  16. System.out.println(getBusTime(2, 1, 2, new String[] {"09:00", "09:00", "09:00", "09:00"}));
  17. System.out.println(getBusTime(1, 1, 5, new String[] {"00:01", "00:01", "00:01", "00:01", "00:01"}));
  18. System.out.println(getBusTime(1, 1, 1, new String[] {"23:59"}));
  19. System.out.println(getBusTime(10, 60, 45, new String[] {"23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"}));
  20. }
  21.  
  22. static Time getBusTime(int n, int t, int m, String[] timetable) {
  23.  
  24. List<Crew> todayCrewList = Arrays.stream(timetable)
  25. .map(Time::parse)
  26. .sorted()
  27. .map(Crew::new).collect(Collectors.toList());
  28.  
  29. ShuttleBus shuttleBus = new ShuttleBus(n, t, m);
  30. Queue<Crew> waitingLine = new LinkedBlockingQueue<>();
  31.  
  32. while (shuttleBus.isEnableService()) {
  33. Time busTime = shuttleBus.getBusTime();
  34. todayCrewList.stream()
  35. .filter(crew -> !crew.isLeft() && busTime.compareTo(crew.getWaitTime()) >= 0)
  36. .forEach(waitingLine::add);
  37. shuttleBus.pickUp(waitingLine);
  38. }
  39.  
  40. if (shuttleBus.isFull()) {
  41. Crew lastCrew = todayCrewList.get(todayCrewList.size() - 1);
  42. Time lastCrewWaitTime = lastCrew.getWaitTime();
  43. lastCrewWaitTime.minusMinutes(1);
  44. return lastCrewWaitTime;
  45. }
  46. else {
  47. Time busTime = shuttleBus.getBusTime();
  48. busTime.minusMinutes(t);
  49. return busTime;
  50. }
  51. }
  52.  
  53. static class ShuttleBus {
  54.  
  55. private final int n;
  56. private final int t;
  57. private final int m;
  58. private final Time closeTime = Time.parse("23:59");
  59. private Time busTime = Time.parse("09:00");
  60. private int service;
  61. private List<Crew> crewList = new ArrayList<>();
  62.  
  63. public ShuttleBus(int n, int t, int m) {
  64. this.n = n;
  65. this.t = t;
  66. this.m = m;
  67. this.service = 0;
  68. }
  69.  
  70. public void pickUp(Queue<Crew> waitingLine) {
  71. crewList.clear();
  72. for (int i = 0; i < m; i++) {
  73. Crew crew = waitingLine.poll();
  74. if (crew != null) {
  75. crew.takeBus();
  76. crewList.add(crew);
  77. }
  78. }
  79. busTime.plusMinutes(t);
  80. service++;
  81. }
  82.  
  83. public boolean isEnableService() {
  84. return n > service && busTime.compareTo(closeTime) <= 0;
  85. }
  86.  
  87. public boolean isFull() {
  88. return crewList.size() >= m;
  89. }
  90.  
  91. public Time getBusTime() {
  92. return Time.of(busTime);
  93. }
  94.  
  95. @Override
  96. public String toString() {
  97. return "ShuttleBus [n=" + n + ", t=" + t + ", m=" + m + ", closeTime=" + closeTime + ", busTime=" + busTime
  98. + ", service=" + service + ", crewList=" + crewList + "]";
  99. }
  100. }
  101.  
  102. static class Crew {
  103.  
  104. private final Time waitTime;
  105. private boolean left;
  106.  
  107. public Crew(Time waitTime) {
  108. this.waitTime = waitTime;
  109. }
  110.  
  111. public Time getWaitTime() {
  112. return Time.of(waitTime);
  113. }
  114.  
  115. public void takeBus() {
  116. this.left = true;
  117. }
  118.  
  119. public boolean isLeft() {
  120. return left;
  121. }
  122.  
  123. @Override
  124. public String toString() {
  125. return waitTime.toString();
  126. }
  127.  
  128. }
  129.  
  130. static class Time implements Comparable<Time> {
  131.  
  132. private int hours;
  133. private int minutes;
  134.  
  135. public static final Time of(Time other) {
  136. return new Time(other.hours, other.minutes);
  137. }
  138.  
  139. public static final Time parse(String expression) {
  140. StringTokenizer tokenizer = new StringTokenizer(expression, ":");
  141. String hours = tokenizer.nextToken();
  142. String minutes = tokenizer.nextToken();
  143. return new Time(Integer.parseInt(hours), Integer.parseInt(minutes));
  144. }
  145.  
  146. private Time(int hours, int minutes) {
  147. this.hours = hours;
  148. this.minutes = minutes;
  149. }
  150.  
  151. public void plusMinutes(int t) {
  152. int mins = t + this.minutes;
  153. this.hours += mins / 60;
  154. this.minutes = mins % 60;
  155. if (this.hours >= 24) {
  156. throw new IllegalStateException("하루가 지났습니다.");
  157. }
  158. }
  159.  
  160. public void minusMinutes(int t) {
  161. this.minutes -= t;
  162. if (this.minutes < 0) {
  163. int mins = this.minutes * -1;
  164. int remainder = mins % 60;
  165. this.hours -= mins / 60 + (remainder > 0 ? 1 : 0);
  166. this.minutes = remainder > 0 ? 60 - remainder : remainder;
  167. }
  168. if (this.hours < 0) {
  169. throw new IllegalStateException("하루 전입니다.");
  170. }
  171. }
  172.  
  173. @Override
  174. public String toString() {
  175. return String.format("%02d:%02d", hours, minutes);
  176. }
  177.  
  178. @Override
  179. public int compareTo(Time other) {
  180. if (other == null) {
  181. throw new NullPointerException("other 객체가 null 입니다.");
  182. }
  183. if (this.hours < other.hours) {
  184. return -1;
  185. }
  186. if (this.hours > other.hours) {
  187. return 1;
  188. }
  189. if (this.minutes < other.minutes) {
  190. return -1;
  191. }
  192. if (this.minutes > other.minutes) {
  193. return 1;
  194. }
  195. return 0;
  196. }
  197.  
  198. }
  199. }
Add Comment
Please, Sign In to add comment