Created
December 13, 2016 11:24
-
-
Save smatyas/db9859bc38da0a34e843a344f2a66008 to your computer and use it in GitHub Desktop.
aoc 2016/13/1
This file contains hidden or 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
| $goal_x = 31 | |
| $goal_y = 39 | |
| $fav_num = 1350 | |
| # $goal_x = 7 | |
| # $goal_y = 4 | |
| # $fav_num = 10 | |
| $path = [] | |
| $good_paths = [] | |
| $min_path_length = 200 | |
| def is_wall?(x, y) | |
| num = x*x + 3*x + 2*x*y + y + y*y | |
| num += $fav_num | |
| num.to_s(2).count('1').odd? | |
| end | |
| def pmap(x, y) | |
| puts $path.inspect | |
| puts ' 0123456789' | |
| (0..y).each do |my| | |
| (0..x).each do |mx| | |
| print my.to_s.ljust(3, ' ') if mx == 0 | |
| if mx == $goal_x && my == $goal_y | |
| print is_wall?(mx, my) ? 'X' : 'x' | |
| elsif $path.include?([mx,my]) | |
| print 'O' | |
| else | |
| print is_wall?(mx, my) ? '#' : '.' | |
| end | |
| end | |
| puts '' | |
| end | |
| end | |
| def step(x, y) | |
| # sleep 0.1 | |
| # puts "step called #{x} #{y}" | |
| if $path.include?([x, y]) | |
| return false | |
| end | |
| $path.push [x, y] | |
| # pmap(40, 40) | |
| if x < 0 || y < 0 || $path.length >= $min_path_length || is_wall?(x, y) | |
| $path.pop | |
| return false | |
| end | |
| if x == $goal_x && y == $goal_y | |
| puts "Found a route with length #{$path.length}!" | |
| $good_paths.push $path | |
| $min_path_length = $path.length | |
| $path.pop | |
| return false | |
| end | |
| step(x+1, y) | |
| step(x, y+1) | |
| step(x-1, y) | |
| step(x, y-1) | |
| $path.pop | |
| return false | |
| end | |
| # pmap(50,50) | |
| step(1, 1) | |
| puts "Min. steps: #{$min_path_length - 1}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment