From 2a88deecc7247c6029b162e7b01471304a9a564d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 5 Dec 2024 21:11:38 -0800 Subject: [PATCH] [2024][ruby][6.1] --- 2024/ruby/day_06.rb | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2024/ruby/day_06.rb diff --git a/2024/ruby/day_06.rb b/2024/ruby/day_06.rb new file mode 100644 index 0000000..c533280 --- /dev/null +++ b/2024/ruby/day_06.rb @@ -0,0 +1,46 @@ +input = DATA.readlines(chomp: true) + .flat_map.with_index {|line, j| + line.chars.map.with_index {|x, i| + [[j,i], x] + } + }.to_h + +turns = { + [-1,0] => [0,1], + [0,1] => [1,0], + [1,0] => [0,-1], + [0,-1] => [-1,0], +} + +start = input.find { _2 == ?^ }.first +dir = [-1,0] +path = [start] +loop do + peek = path.last.zip(dir).map {|i,di| i + di } + case input.fetch(peek, nil) + when ?. + path << peek + when ?^ + path << peek + when ?# + dir = turns.fetch(dir) + when nil + break + else + fail + end +end + +pp path.to_set.length + +__END__ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...