Skip to content

Commit 8306f60

Browse files
committed
Day 11
1 parent b7accbd commit 8306f60

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

11_hex_grid.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

0 commit comments

Comments
 (0)