File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ def dist ( x , y )
2
+ # If move NE then SE, you end up at (1, -1).
3
+ # This requires 2 moves to get to, since there is no +1, -1 move.
4
+ # Same if we are at (-1, 1) since there is no -1, +1 move.
5
+ # Therefore:
6
+ # If both coords are same sign, distance is the maximum magnitude.
7
+ # If they are of different sign, distance is sum of magnitudes.
8
+ #
9
+ # NOTE that for my input, it was sufficient to always use max only!
10
+ # So, I never had a situation where I was [+, -] or [-, +].
11
+ x . positive? == y . positive? ? [ x . abs , y . abs ] . max : x . abs + y . abs
12
+ end
13
+
14
+
15
+ x = 0
16
+ y = 0
17
+ maxdist = 0
18
+
19
+ ( !ARGV . empty? && ARGV . first . include? ( ?,) ? ARGV . first : ARGF . read ) . split ( ?,) { |i |
20
+ case i . strip
21
+ when 'ne'
22
+ y -= 1
23
+ when 'sw'
24
+ y += 1
25
+ when 'nw'
26
+ x -= 1
27
+ when 'se'
28
+ x += 1
29
+ when 'n'
30
+ y -= 1
31
+ x -= 1
32
+ when 's'
33
+ y += 1
34
+ x += 1
35
+ else
36
+ raise "unknown #{ i } "
37
+ end
38
+ maxdist = [ maxdist , dist ( x , y ) ] . max
39
+ }
40
+
41
+ puts dist ( x , y )
42
+
43
+ puts maxdist
You can’t perform that action at this time.
0 commit comments