-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4-2.p
104 lines (89 loc) · 3.44 KB
/
day4-2.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
--- Day 4: Repose Record ---
--- Part Two ---
Strategy 2: Of all guards, which guard is most frequently asleep on the same minute?
In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
*/
DEFINE VARIABLE c AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDate AS CHARACTER NO-UNDO.
DEFINE VARIABLE cLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTime AS CHARACTER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iGuard AS INTEGER NO-UNDO.
DEFINE VARIABLE iMinute AS INTEGER NO-UNDO.
DEFINE VARIABLE iSleep AS INTEGER NO-UNDO.
DEFINE VARIABLE iSleepMin AS INTEGER EXTENT 60 NO-UNDO.
DEFINE VARIABLE iStartMin AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE ttEvent NO-UNDO
FIELD dt AS DATETIME
FIELD iMinute AS INTEGER
FIELD cEvent AS CHARACTER
FIELD iGuard AS INTEGER
INDEX ix IS PRIMARY UNIQUE dt.
DEFINE TEMP-TABLE ttGuard NO-UNDO
FIELD iGuard AS INTEGER
FIELD iShifts AS INTEGER
FIELD iSleepTime AS INTEGER
FIELD iMinute AS INTEGER EXTENT 60
FIELD iSleepMin AS INTEGER
FIELD iSleepMinFreq AS INTEGER
INDEX ix IS PRIMARY UNIQUE iGuard
INDEX ist iSleepTime DESCENDING
INDEX ism iSleepMinFreq DESCENDING.
ETIME(YES).
INPUT FROM C:\User\JCCARDOT\Perso\Travail\aoc\aoc2018\day4.txt.
REPEAT:
IMPORT UNFORMATTED cLine.
c = LEFT-TRIM(ENTRY(1, cLine, "]"), "[").
ASSIGN
cDate = ENTRY(1, c, " ")
cTime = ENTRY(2, c, " ").
CREATE ttEvent.
ASSIGN
ttEvent.dt = DATETIME(INTEGER(ENTRY(2,cDate,"-")),INTEGER(ENTRY(3,cDate,"-")),INTEGER(ENTRY(1,cDate,"-")),
INTEGER(ENTRY(1,cTime,":")),INTEGER(ENTRY(2,cTime,":")))
ttEvent.iMinute = INTEGER(ENTRY(2,cTime,":"))
ttEvent.cEvent = TRIM(ENTRY(2, cLine, "]"))
.
END.
INPUT CLOSE.
FOR EACH ttEvent BY ttEvent.dt:
IF ttEvent.cEvent BEGINS "Guard" THEN DO:
iGuard = INTEGER(SUBSTRING(ENTRY(2, ttEvent.cEvent, " "), 2)).
FIND ttGuard WHERE ttGuard.iGuard = iGuard NO-ERROR.
IF NOT AVAILABLE ttGuard THEN DO:
CREATE ttGuard.
ASSIGN ttGuard.iGuard = iGuard.
END.
ttGuard.iShifts = ttGuard.iShifts + 1.
END.
ELSE IF ttEvent.cEvent = "falls asleep" THEN
iStartMin = ttEvent.iMinute.
ttEvent.iGuard = iGuard.
IF ttEvent.cEvent = "wakes up" THEN DO:
ttGuard.iSleepTime = ttGuard.iSleepTime + ttEvent.iMinute - iStartMin.
DO i = iStartMin TO ttEvent.iMinute - 1:
ttGuard.iMinute[i + 1] = ttGuard.iMinute[i + 1] + 1.
END.
END.
END.
FOR EACH ttGuard:
DO i = 0 TO 59:
IF ttGuard.iMinute[i + 1] > iSleep THEN DO:
iSleep = ttGuard.iMinute[i + 1].
iMinute = i.
END.
END.
ASSIGN
ttGuard.iSleepMin = iMinute
ttGuard.iSleepMinFreq = iSleep.
END.
FOR EACH ttGuard BY ttGuard.iSleepMinFreq DESCENDING:
LEAVE.
END.
MESSAGE ETIME SKIP
ttGuard.iGuard "*" ttGuard.iSleepMin "=" ttGuard.iGuard * ttGuard.iSleepMin
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* 105 */
/* 2917 * 35 = 102095 */