-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4_part2.jakt
114 lines (94 loc) · 3.52 KB
/
day4_part2.jakt
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
105
106
107
108
109
110
111
112
113
114
/// Expect:
/// - output: "3852\n"
import reader
import utility
function parse_event(event: String) throws -> ([i32], String) {
let date_event = event.split(']')
let date = date_event[0].substring(start: 1, length: date_event[0].length()-1)
let event = date_event[1].substring(start: 1, length: date_event[1].length()-1)
let date_time = date.split(' ')
let date_parts = date_time[0].split('-')
let time_parts = date_time[1].split(':')
return ([
date_parts[0].to_int()!
date_parts[1].to_int()!
date_parts[2].to_int()!
time_parts[0].to_int()!
time_parts[1].to_int()!
], event)
}
function build_guard_sleeping(events: [([i32], String)]) throws -> ([i32:[i32:i32]], [i32:i32]) {
mut guards_sleeping_minutes: [i32:[i32:i32]] = [:]
mut guards_sleeping_total: [i32:i32] = [:]
mut current_guard = 0i32
mut current_sleep_start = -1i32
for event in events.iterator() {
match event.1.substring(start: 0, length: 5) {
"Guard" => {
let guards = utility::format_match(line: event.1, format: "Guard #%")
if guards.has_value() {
current_guard = guards![0]
}
}
"falls" => {
current_sleep_start = event.0[4]
}
"wakes" => {
if not guards_sleeping_minutes.contains(current_guard) {
guards_sleeping_minutes[current_guard] = [:]
guards_sleeping_total[current_guard] = 0
}
guards_sleeping_total[current_guard] += event.0[4] - current_sleep_start
for i in current_sleep_start..event.0[4] {
if not guards_sleeping_minutes[current_guard].contains(i) {
guards_sleeping_minutes[current_guard][i] = 0
}
guards_sleeping_minutes[current_guard][i]++
}
current_sleep_start = -1i32
}
else => {}
}
}
return (guards_sleeping_minutes, guards_sleeping_total)
}
function largest(anon collection: [i32:i32]) throws -> i32 {
mut current_largest = -1i32
mut current_largest_key = -1i32
for key in collection.keys().iterator() {
if collection[key] > current_largest {
current_largest = collection[key]
current_largest_key = key
}
}
return current_largest_key
}
function main() {
mut events: [([i32], String)] = []
for event in reader::lines_string(day: 4).iterator() {
events.push(parse_event(event))
}
utility::sort(values: events, sorter: function(anon item_a: &([i32], String), anon item_b: &([i32], String)) -> i8 {
for i in 0..5 {
if item_a.0[i] > item_b.0[i] {
return 1
} else if item_a.0[i] < item_b.0[i] {
return -1
}
}
return 0
})
mut (guards_sleeping_minutes, guards_sleeping_total) = build_guard_sleeping(events)
mut minute_guard = -1i32
mut minute_count = -1i32
mut minute_minute = -1i32
for guard_ in guards_sleeping_minutes.keys().iterator() {
let guard_minute = largest(guards_sleeping_minutes[guard_])
if guards_sleeping_minutes[guard_][guard_minute] > minute_count {
minute_guard = guard_
minute_count = guards_sleeping_minutes[guard_][guard_minute]
minute_minute = guard_minute
}
}
println("{}", minute_guard * minute_minute)
}