-
Notifications
You must be signed in to change notification settings - Fork 3
/
day05.rb
executable file
·79 lines (64 loc) · 1.14 KB
/
day05.rb
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
#!/usr/bin/env ruby
require 'pp'
def parse(filename)
jumps = []
File.open(filename).each do |line|
jumps.push(line.strip().to_i)
end
jumps
end
def part1(jumps)
max = jumps.length
current_idx = 0
last_idx = 0
steps = 0
loop do
last_idx = current_idx
if current_idx >= max
break
end
current_idx += jumps[current_idx]
jumps[last_idx] += 1
steps += 1
end
steps
end
def part2(jumps)
max = jumps.length
current_idx = 0
last_idx = 0
steps = 0
loop do
last_idx = current_idx
if current_idx >= max
break
end
current_idx += jumps[current_idx]
if jumps[last_idx] >= 3
jumps[last_idx] -= 1
else
jumps[last_idx] += 1
end
steps += 1
end
steps
end
puts "Hello"
jumps = parse("../input_small.txt")
steps = part1(jumps)
puts "Part1 Small: Should equal 5"
puts steps
jumps = parse("../input_small.txt")
steps = part2(jumps)
puts "Part2 Small: Should equal 10"
puts steps
###
puts "\n\n"
jumps = parse("../input.txt")
steps = part1(jumps)
puts "Part1 Large"
puts steps
jumps = parse("../input.txt")
steps = part2(jumps)
puts "Part2 Large"
puts steps