-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.koto
56 lines (47 loc) · 1.42 KB
/
01.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
# https://adventofcode.com/2020/day/1
parse_expenses = |input|
input
.lines()
.each |line| line.to_number()
.to_tuple()
product_of_two_expenses_that_sum_to_2020 = |expenses|
for x in 0..expenses.size()
expense_x = expenses[x]
for y in x + 1..expenses.size()
expense_y = expenses[y]
if expense_x + expense_y == 2020
return expense_x * expense_y
product_of_three_expenses_that_sum_to_2020 = |expenses|
for x in 0..expenses.size()
expense_x = expenses[x]
for y in x + 1..expenses.size()
expense_y = expenses[y]
for z in y + 1..expenses.size()
expense_z = expenses[z]
if expense_x + expense_y + expense_z == 2020
return expense_x * expense_y * expense_z
@main = ||
input = io.extend_path koto.script_dir, "input", "01"
>> io.read_to_string
>> parse_expenses
part_one = product_of_two_expenses_that_sum_to_2020 input
print "Part one: $part_one" # 974304
part_two = product_of_three_expenses_that_sum_to_2020 input
print "Part two: $part_two" # 236430480
@tests =
@pre_test: ||
self.input = "\
1721
979
366
299
675
1456"
@test part_one: ||
expenses = parse_expenses self.input
result = product_of_two_expenses_that_sum_to_2020 expenses
assert_eq result, 514579
@test part_two: ||
expenses = parse_expenses self.input
result = product_of_three_expenses_that_sum_to_2020 expenses
assert_eq result, 241861950