-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.rb
77 lines (52 loc) · 1.23 KB
/
day3.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
#!/usr/bin/env ruby
# Day 1 2016
# See http://adventofcode.com/2016/day/3
puts "Advent of Code 2016 day 3"
valid = 0
p2valid = 0
def is_valid_triangle?(sides)
a = sides[0]
b = sides[1]
c = sides[2]
if (a+b > c && a+c > b && b + c > a)
return true
end
return false
end
#Part 1
File.open('day3.data').each do |line|
continue if(line.nil?)
#split data into side numbers
md = /(\d+)\s+(\d+)\s+(\d+)/.match(line)
if(!md.nil? && md.length)
sides = [md[1].to_i, md[2].to_i, md[3].to_i]
if(is_valid_triangle?(sides))
valid += 1
end
end
end
puts "part1 valid triangles = #{valid}"
#Part 2 - columns
col1 = []
col2 = []
col3 = []
File.open('day3.data').each do |line|
continue if(line.nil?)
#split data into side numbers
md = /(\d+)\s+(\d+)\s+(\d+)/.match(line)
if(!md.nil? && md.length)
col1.push(md[1].to_i)
col2.push(md[2].to_i)
col3.push(md[3].to_i)
if(col1.length == 3)
#validate columns
p2valid += 1 if(is_valid_triangle?(col1))
p2valid += 1 if(is_valid_triangle?(col2))
p2valid += 1 if(is_valid_triangle?(col3))
col1 = []
col2 = []
col3 = []
end
end
end
puts "part2 valid triangles = #{p2valid}"