Created
December 23, 2016 10:27
-
-
Save eoinkelly/3ea8a7220c38b9786e964ceae6ebc30f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Part2 | |
# 1 | |
# 2 3 4 | |
# 5 6 7 8 9 | |
# A B C | |
# D | |
class State | |
def go_u; self; end | |
def go_d; self; end | |
def go_l; self; end | |
def go_r; self; end | |
def to_s | |
"State is: #{self.class.to_s.downcase}" | |
end | |
end | |
class One < State # done | |
def go_d; Three.new; end | |
end | |
class Two < State # done | |
def go_d; Six.new; end | |
def go_r; Three.new; end | |
end | |
class Three < State # done | |
def go_u; One.new; end | |
def go_d; Seven.new; end | |
def go_l; Two.new; end | |
def go_r; Four.new; end | |
end | |
class Four < State # done | |
def go_d; Eight.new; end | |
def go_l; Three.new; end | |
end | |
class Five < State # done | |
def go_r; Six.new; end | |
end | |
class Six < State # done | |
def go_u; Two.new; end | |
def go_d; A.new; end | |
def go_l; Five.new; end | |
def go_r; Seven.new; end | |
end | |
class Seven < State # done | |
def go_u; Three.new; end | |
def go_d; B.new; end | |
def go_l; Six.new; end | |
def go_r; Eight.new; end | |
end | |
class Eight < State | |
def go_u; Four.new; end | |
def go_l; Seven.new; end | |
def go_r; Nine.new; end | |
def go_d; C.new; end | |
end | |
class Nine < State | |
def go_l; Eight.new; end | |
end | |
class A < State | |
def go_u; Six.new; end | |
def go_r; B.new; end | |
end | |
class B < State | |
def go_u; Seven.new; end | |
def go_d; D.new; end | |
def go_l; A.new; end | |
def go_r; C.new; end | |
end | |
class C < State | |
def go_u; Eight.new; end | |
def go_l; B.new; end | |
end | |
class D < State | |
def go_u; B.new; end | |
end | |
end | |
module Part1 | |
# 1 2 3 | |
# 4 5 6 | |
# 7 8 9 | |
class State | |
def go_u; self; end | |
def go_d; self; end | |
def go_l; self; end | |
def go_r; self; end | |
def to_s | |
"State is: #{self.class.to_s.downcase}" | |
end | |
end | |
class One < State | |
def go_r; Two.new; end | |
def go_d; Four.new; end | |
end | |
class Two < State | |
def go_d; Five.new; end | |
def go_l; One.new; end | |
def go_r; Three.new; end | |
end | |
class Three < State | |
def go_d; Six.new; end | |
def go_l; Two.new; end | |
end | |
class Four < State | |
def go_u; One.new; end | |
def go_d; Seven.new; end | |
def go_r; Five.new; end | |
end | |
class Five < State | |
def go_u; Two.new; end | |
def go_d; Eight.new; end | |
def go_l; Four.new; end | |
def go_r; Six.new; end | |
end | |
class Six < State | |
def go_u; Three.new; end | |
def go_d; Nine.new; end | |
def go_l; Five.new; end | |
end | |
class Seven < State | |
def go_u; Four.new; end | |
def go_r; Eight.new; end | |
end | |
class Eight < State | |
def go_u; Five.new; end | |
def go_l; Seven.new; end | |
def go_r; Nine.new; end | |
end | |
class Nine < State | |
def go_u; Six.new; end | |
def go_l; Eight.new; end | |
end | |
end | |
LINES = ARGF.readlines.map(&:strip) | |
def run(initial_state:) | |
LINES | |
.reduce(initial_state) { |initial_line_state, line| | |
final_line_state = line.chars.reduce(initial_line_state) { |curr_state, instruction| | |
curr_state.public_send("go_#{instruction.downcase}") | |
} | |
puts "Final line state: #{final_line_state}" | |
final_line_state | |
} | |
end | |
run(initial_state: Part1::Five.new) | |
run(initial_state: Part2::Five.new) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment