-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.koto
67 lines (58 loc) · 1.97 KB
/
10.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
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
# https://adventofcode.com/2020/day/10
from io import extend_path, read_to_string
from koto import script_dir
parse_input = |input|
input
.lines()
.each |line| line.to_number()
.to_list()
.sort()
joltage_differences = |adapters|
differences = [0, 0, 0, 0]
previous = 0
for adapter in adapters
difference = adapter - previous
differences[difference] += 1
previous = adapter
differences[3] += 1
differences
count_arrangements = |adapters|
# The first adapter has a single arrangement
arrangements = [1]
# Iterate over the remaining adapters
for i in 1..adapters.size()
adapter = adapters[i]
# Adapters with ratings <= 3 are also valid start points
arrangements_for_adapter = if adapter <= 3 then 1 else 0
# Add the number of arrangements from previous adapters within range
for j in i - 1..=0
if adapter - adapters[j] <= 3
arrangements_for_adapter += arrangements[j]
else
break
arrangements.push arrangements_for_adapter
# The result is the arrangement count for the last adapter
arrangements.last()
@main = ||
adapters = io.extend_path script_dir, "input", "10"
>> read_to_string
>> parse_input
differences = joltage_differences adapters
print "Part one: ${differences[1] * differences[3]}" # 2040
print "Part two: ${count_arrangements adapters}" # 28346956187648
@tests =
@pre_test: ||
self.adapters_1 =
parse_input read_to_string extend_path script_dir, "input", "10-example-1"
self.adapters_2 =
parse_input read_to_string extend_path script_dir, "input", "10-example-2"
@test part_one: ||
differences = joltage_differences self.adapters_1
assert_eq differences[1], 7
assert_eq differences[3], 5
differences = joltage_differences self.adapters_2
assert_eq differences[1], 22
assert_eq differences[3], 10
@test part_two: ||
assert_eq (count_arrangements self.adapters_1), 8
assert_eq (count_arrangements self.adapters_2), 19208