File tree 2 files changed +152
-0
lines changed
2 files changed +152
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ class Rule
4
+ attr_reader :from , :to
5
+
6
+ def initialize ( line )
7
+ @from , @to = line . split ( '|' ) . map ( &:to_i )
8
+ end
9
+
10
+ def inspect
11
+ to_s
12
+ end
13
+
14
+ def to_s
15
+ "<#{ self . class } : #{ @from } -> #{ @to } >"
16
+ end
17
+ end
18
+
19
+ class Update
20
+ attr_reader :pages
21
+
22
+ def initialize ( line )
23
+ @pages = line . split ( ',' ) . map ( &:to_i )
24
+ end
25
+
26
+ def satisfy? ( rule )
27
+ first = @pages . find_index rule . from
28
+ last = @pages . find_index rule . to
29
+
30
+ return true if first . nil? or last . nil?
31
+
32
+ first < last
33
+ end
34
+
35
+ def score
36
+ @pages [ @pages . size / 2 ]
37
+ end
38
+
39
+ def inspect
40
+ to_s
41
+ end
42
+
43
+ def to_s
44
+ "<#{ self . class } : #{ @pages } >"
45
+ end
46
+ end
47
+
48
+ class Puzzle
49
+ def initialize ( input )
50
+ rules , updates = input . partition { |l | l . include? '|' }
51
+
52
+ @rules = rules . map { |r | Rule . new r }
53
+ @updates = updates . reject { |u | u == '' } . map { |u | Update . new u }
54
+ end
55
+
56
+ def score
57
+ @updates . map do |u |
58
+ if @rules . all? { |r | u . satisfy? r }
59
+ u . score
60
+ else
61
+ 0
62
+ end
63
+ end . sum
64
+ end
65
+ end
66
+
67
+ input = File . read ( '05.input' ) . lines . map ( &:strip )
68
+ puzzle = Puzzle . new ( input )
69
+ puts puzzle . score
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ class Rule
4
+ attr_reader :from , :to
5
+
6
+ def initialize ( line )
7
+ @from , @to = line . split ( '|' ) . map ( &:to_i )
8
+ end
9
+
10
+ def inspect
11
+ to_s
12
+ end
13
+
14
+ def to_s
15
+ "<#{ self . class } : #{ @from } -> #{ @to } >"
16
+ end
17
+ end
18
+
19
+ class Update
20
+ attr_reader :pages
21
+
22
+ def initialize ( line , rules )
23
+ @pages = line . split ( ',' ) . map ( &:to_i )
24
+ @rules = rules
25
+ end
26
+
27
+ def satisfy? ( rule )
28
+ first = @pages . find_index rule . from
29
+ last = @pages . find_index rule . to
30
+
31
+ return true if first . nil? or last . nil?
32
+
33
+ first < last
34
+ end
35
+
36
+ def sort!
37
+ @pages . sort! do |a , b |
38
+ if @rules . any? { |r | r . from == a and r . to == b }
39
+ -1
40
+ elsif @rules . any? { |r | r . from == b and r . to == a }
41
+ 1
42
+ else
43
+ 0
44
+ end
45
+ end
46
+ end
47
+
48
+ def score
49
+ @pages [ @pages . size / 2 ]
50
+ end
51
+
52
+ def inspect
53
+ to_s
54
+ end
55
+
56
+ def to_s
57
+ "<#{ self . class } : #{ @pages } >"
58
+ end
59
+ end
60
+
61
+ class Puzzle
62
+ def initialize ( input )
63
+ rules , updates = input . partition { |l | l . include? '|' }
64
+
65
+ @rules = rules . map { |r | Rule . new r }
66
+ @updates = updates . reject { |u | u == '' } . map { |u | Update . new ( u , @rules ) }
67
+ end
68
+
69
+ def score
70
+ @updates . map do |u |
71
+ if not @rules . all? { |r | u . satisfy? r }
72
+ u . sort!
73
+ u . score
74
+ else
75
+ 0
76
+ end
77
+ end . sum
78
+ end
79
+ end
80
+
81
+ input = File . read ( '05.input' ) . lines . map ( &:strip )
82
+ puzzle = Puzzle . new ( input )
83
+ puts puzzle . score
You can’t perform that action at this time.
0 commit comments