-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.koto
62 lines (54 loc) · 1.34 KB
/
03.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
# https://adventofcode.com/2020/day/3
parse_input = |input|
input.lines()
.each |line|
line
.each |c| c == "#"
.to_tuple()
.to_tuple()
count_trees_on_grid = |grid, (slope_x, slope_y)|
pos_x, pos_y = 0, 0
tree_count = 0
height = grid.size()
width = grid[0].size()
until pos_y >= height
if grid[pos_y][pos_x % width]
tree_count += 1
pos_x += slope_x
pos_y += slope_y
tree_count
check_slopes_and_multiply_tree_counts = |grid|
slopes = (1, 1), (3, 1), (5, 1), (7, 1), (1, 2)
slopes
.each |slope| count_trees_on_grid grid, slope
.product()
@main = ||
input = io.extend_path koto.script_dir, "input", "03"
>> io.read_to_string
>> parse_input
print "Part one: ${count_trees_on_grid input, (3, 1)}" # 250
print "Part two: ${check_slopes_and_multiply_tree_counts input}" # 1592662500
@tests =
@pre_test: ||
self.input = parse_input "\
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
"
@test parse_input: ||
assert self.input[0][2]
assert self.input[1][4]
assert not self.input[0][1]
assert not self.input[4][2]
@test part_one: ||
assert_eq (count_trees_on_grid self.input, (3, 1)), 7
@test part_two: ||
assert_eq (check_slopes_and_multiply_tree_counts self.input), 336