diff options
author | Nat Lasseter <user@4574.co.uk> | 2022-12-23 01:53:54 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2022-12-23 01:53:54 +0000 |
commit | 6e39227aa5ae68ff69a79b63f5c28b2e2544bb74 (patch) | |
tree | e38bbf4cfce2fc2b2306db985ff65283d8b3adba /day22/part1.rb | |
parent | 6734e8dee0ff45229100bfbed9935ce080639981 (diff) |
Diffstat (limited to 'day22/part1.rb')
-rw-r--r-- | day22/part1.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/day22/part1.rb b/day22/part1.rb new file mode 100644 index 0000000..2aab75b --- /dev/null +++ b/day22/part1.rb @@ -0,0 +1,84 @@ +def consume(path) + case path[0] + when ?L + [?L, path[1..-1]] + when ?R + [?R, path[1..-1]] + when /\d/ + act = path.chars.take_while { |ch| ch =~ /\d/ }.join + l = act.length + [act.to_i, path[l..-1]] + end +end + +input = $stdin.readlines.map(&:rstrip) +map = input[0..-3] +path = input[-1] + +r = 0 +c = input[0].index(?.) +d = ?R + +until path.empty? + action, path = consume(path) + case action + when ?L + d = case d + when ?U; ?L + when ?R; ?U + when ?D; ?R + when ?L; ?D + end + when ?R + d = case d + when ?U; ?R + when ?R; ?D + when ?D; ?L + when ?L; ?U + end + else + action.times do + case d + when ?U + nr = r - 1 + if nr < 0 || map[nr][c] == ?\s || map[nr][c].nil? + nr = map.length - 1 + nr -= 1 until map[nr][c] == ?. || map[nr][c] == ?# + end + break if map[nr][c] == ?# + r = nr + when ?R + nc = c + 1 + if nc >= map[r].length || map[r][nc] == ?\s || map[r][nc].nil? + nc = 0 + nc += 1 until map[r][nc] == ?. || map[r][nc] == ?# + end + break if map[r][nc] == ?# + c = nc + when ?D + nr = r + 1 + if nr >= map.length || map[nr][c] == ?\s || map[nr][c].nil? + nr = 0 + nr += 1 until map[nr][c] == ?. || map[nr][c] == ?# + end + break if map[nr][c] == ?# + r = nr + when ?L + nc = c - 1 + if nc < 0 || map[r][nc] == ?\s || map[r][nc].nil? + nc = map[r].length - 1 + nc -= 1 until map[r][nc] == ?. || map[r][nc] == ?# + end + break if map[r][nc] == ?# + c = nc + end + end + end +end + +puts 1000 * (r+1) + 4 * (c + 1) + case d + when ?R; 0 + when ?D; 1 + when ?L; 2 + when ?U; 3 + end |