-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.koto
40 lines (33 loc) · 1.02 KB
/
05.koto
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
# https://adventofcode.com/2020/day/5
lower_half = |r| r.start()..r.start() + r.size() / 2
upper_half = |r| r.start() + r.size() / 2..r.end()
partition = |input, lower, max|
input
.fold 0..max, |range, c|
if c == lower
lower_half range
else
upper_half range
.start()
seat_id = |seat_code|
row = partition seat_code.take(7), "F", 128
col = partition seat_code.skip(7), "L", 8
row * 8 + col
find_missing_seat = |sorted_seat_ids|
for a, b in sorted_seat_ids.zip sorted_seat_ids.skip 1
if b - a == 2
return b - 1
@main = ||
seat_ids = (io.read_to_string io.extend_path koto.script_dir, "input", "05")
.lines()
.each |seat_code| seat_id seat_code
.to_list()
.sort()
print "Part one: ${seat_ids.last()}" # 976
print "Part two: ${find_missing_seat seat_ids}" # 685
@tests =
@test part_one: ||
assert_eq (seat_id "FBFBBFFRLR"), 357
assert_eq (seat_id "BFFFBBFRRR"), 567
assert_eq (seat_id "FFFBBBFRRR"), 119
assert_eq (seat_id "BBFFBBFRLL"), 820