|
| 1 | +package io.github.imsejin.study.programmers; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150370">개인정보 수집 유효기간</a> |
| 8 | + */ |
| 9 | +public class L150370 { |
| 10 | + |
| 11 | + static int[] solve0(String today, String[] terms, String[] privacies) { |
| 12 | + LocalDate todayDate = parseDate(today); |
| 13 | + |
| 14 | + Map<String, Integer> termMap = new HashMap<>(); |
| 15 | + for (String term : terms) { |
| 16 | + StringTokenizer tokenizer = new StringTokenizer(term); |
| 17 | + String type = tokenizer.nextToken(); |
| 18 | + int month = Integer.parseInt(tokenizer.nextToken()); |
| 19 | + |
| 20 | + termMap.put(type, month); |
| 21 | + } |
| 22 | + |
| 23 | + List<Integer> privaciesToDelete = new ArrayList<>(); |
| 24 | + for (int i = 0; i < privacies.length; i++) { |
| 25 | + String privacy = privacies[i]; |
| 26 | + |
| 27 | + StringTokenizer tokenizer = new StringTokenizer(privacy); |
| 28 | + LocalDate joinDate = parseDate(tokenizer.nextToken()); |
| 29 | + String type = tokenizer.nextToken(); |
| 30 | + |
| 31 | + Integer month = termMap.get(type); |
| 32 | + LocalDate deleteDate = joinDate.plusMonths(month); |
| 33 | + |
| 34 | + if (deleteDate.compareTo(todayDate) <= 0) { |
| 35 | + privaciesToDelete.add(i + 1); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return privaciesToDelete.stream().mapToInt(it -> it).toArray(); |
| 40 | + } |
| 41 | + |
| 42 | + static int[] solve1(String today, String[] terms, String[] privacies) { |
| 43 | + DateString todayString = new DateString(today); |
| 44 | + |
| 45 | + Map<String, Integer> termMap = new HashMap<>(); |
| 46 | + for (String term : terms) { |
| 47 | + StringTokenizer tokenizer = new StringTokenizer(term); |
| 48 | + String type = tokenizer.nextToken(); |
| 49 | + int month = Integer.parseInt(tokenizer.nextToken()); |
| 50 | + |
| 51 | + termMap.put(type, month); |
| 52 | + } |
| 53 | + |
| 54 | + List<Integer> privaciesToDelete = new ArrayList<>(); |
| 55 | + for (int i = 0; i < privacies.length; i++) { |
| 56 | + String privacy = privacies[i]; |
| 57 | + |
| 58 | + StringTokenizer tokenizer = new StringTokenizer(privacy); |
| 59 | + DateString joinDate = new DateString(tokenizer.nextToken()); |
| 60 | + String type = tokenizer.nextToken(); |
| 61 | + |
| 62 | + Integer month = termMap.get(type); |
| 63 | + DateString deleteDate = joinDate.plusMonth(month); |
| 64 | + |
| 65 | + if (deleteDate.compareTo(todayString) <= 0) { |
| 66 | + privaciesToDelete.add(i + 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return privaciesToDelete.stream().mapToInt(it -> it).toArray(); |
| 71 | + } |
| 72 | + |
| 73 | + // ------------------------------------------------------------------------------------------------- |
| 74 | + |
| 75 | + private static LocalDate parseDate(String date) { |
| 76 | + String[] fragments = date.split("\\."); |
| 77 | + int year = Integer.parseInt(fragments[0]); |
| 78 | + int month = Integer.parseInt(fragments[1]); |
| 79 | + int day = Integer.parseInt(fragments[2]); |
| 80 | + |
| 81 | + return LocalDate.of(year, month, day); |
| 82 | + } |
| 83 | + |
| 84 | + private static class DateString implements Comparable<DateString> { |
| 85 | + private final int year; |
| 86 | + |
| 87 | + private final int month; |
| 88 | + |
| 89 | + private final int day; |
| 90 | + |
| 91 | + public DateString(String date) { |
| 92 | + String[] fragments = date.split("\\."); |
| 93 | + this.year = Integer.parseInt(fragments[0]); |
| 94 | + this.month = Integer.parseInt(fragments[1]); |
| 95 | + this.day = Integer.parseInt(fragments[2]); |
| 96 | + } |
| 97 | + |
| 98 | + private DateString(int year, int month, int day) { |
| 99 | + if (year < 0) throw new IllegalArgumentException("Invalid year: " + year); |
| 100 | + if (month < 1 || month > 12) throw new IllegalArgumentException("Invalid month: " + month); |
| 101 | + if (day < 1 || day > 28) throw new IllegalArgumentException("Invalid day: " + day); |
| 102 | + |
| 103 | + this.year = year; |
| 104 | + this.month = month; |
| 105 | + this.day = day; |
| 106 | + } |
| 107 | + |
| 108 | + public DateString plusMonth(int monthToAdd) { |
| 109 | + int month = (this.month + monthToAdd) % 12; |
| 110 | + if (month == 0) month = 12; |
| 111 | + |
| 112 | + int year = this.year; |
| 113 | + if (this.month + monthToAdd > 12) { |
| 114 | + year += (this.month + monthToAdd) / 12; |
| 115 | + } |
| 116 | + |
| 117 | + return new DateString(year, month, this.day); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String toString() { |
| 122 | + return String.format("%04d.%02d.%02d", year, month, day); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int compareTo(DateString o) { |
| 127 | + return this.toString().compareTo(o.toString()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments