1
0
Fork 0

Passes test data set

This commit is contained in:
Andrew Tomaka 2024-12-24 00:26:34 -05:00
parent 0f744688b1
commit 4b1589bb99
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE

View file

@ -15,7 +15,7 @@ input = (ARGV.first.nil? ? DATA : ARGF)
@dir_arrows = @arrow_dirs.invert
number_movements = {
numeric_movements = {
"0" => ["^", ">"],
"A" => ["<", "^"],
"1" => ["^", ">"],
@ -29,7 +29,7 @@ number_movements = {
"9" => ["<", "v"],
}
arrow_movements = {
directional_movements = {
"^" => [">", "v"],
"A" => ["v", "<"],
"<" => [">"],
@ -37,15 +37,15 @@ arrow_movements = {
">" => ["^", "<"],
}
number_grid = [
numeric_keypad = [
["7", "8", "9"],
["4", "5", "6"],
["1", "2", "3"],
["#", "0", "A"],
[nil, "0", "A"],
]
arrow_grid = [
["#", "^", "A"],
directional_keypad = [
[nil, "^", "A"],
["<", "v", ">"],
]
@ -67,13 +67,6 @@ def discover(char, graph)
end
end
def dijkstra_with_lookup(graph, movements, start_char, target_char)
start = discover(start_char, graph)
target = discover(target_char, graph)
dijkstra(graph, movements, start, target)
end
def dijkstra_paths(prev, target, current_path = [])
return [current_path.reverse] if prev[target].nil?
@ -82,6 +75,13 @@ def dijkstra_paths(prev, target, current_path = [])
end
end
def dijkstra_with_lookup(graph, movements, start_char, target_char)
start = discover(start_char, graph)
target = discover(target_char, graph)
dijkstra(graph, movements, start, target)
end
def dijkstra(graph, movements, start, target)
queue = PriorityQueue.new
dest = Hash.new
@ -111,69 +111,215 @@ def dijkstra(graph, movements, start, target)
paths = dijkstra_paths(prev, target)
return dest, paths.first
return dest, paths
end
# calculate most efficient number pad paths
# actually AN efficient path; ties not included :\
number_cache = number_grid
.flatten.reject { _1 == "#" || _2 == "#" }
.permutation(2).to_a
.map { [[_1, _2], dijkstra_with_lookup(number_grid, number_movements, _1, _2)] }
.map do |(start, target), (dest, prev)|
presses = ([discover(start, number_grid)] + prev)
.each_cons(2).to_a
.map { @dir_arrows[[_2.first - _1.first, _2.last - _1.last]] }
[[start, target], presses]
end
.to_h
# numeric_map = numeric_keypad
# .flatten.reject { _1.nil? }
# .permutation(2).to_a
# .map { [[_1, _2], dijkstra_with_lookup(numeric_keypad, numeric_movements, _1, _2)] }
# .map do |(start, target), (dest, paths)|
# presses = paths.map do |path|
# ([discover(start, numeric_keypad)] + path)
# .each_cons(2).to_a
# .map { @dir_arrows[[_2.first - _1.first, _2.last - _1.last]] }.join
# end
# [[start, target], presses]
# end
# .to_h
numeric_map = {
["7", "8"]=>[">"],
["7", "9"]=>[">>"],
["7", "4"]=>["v"],
["7", "5"]=>[">v", "v>"],
["7", "6"]=>[">>v", "v>>"],
["7", "1"]=>["vv"],
["7", "2"]=>[">vv", "vv>"],
["7", "3"]=>[">>vv", "vv>>"],
["7", "0"]=>[">vvv", "v>vv", "vv>v"],
["7", "A"]=>[">>vvv"],
["8", "7"]=>["<"],
["8", "9"]=>[">"],
["8", "4"]=>["<v", "v<"],
["8", "5"]=>["v"],
["8", "6"]=>[">v", "v>"],
["8", "1"]=>["<vv", "vv<"],
["8", "2"]=>["vv"],
["8", "3"]=>[">vv", "vv>"],
["8", "0"]=>["vvv"],
["8", "A"]=>[">vvv", "vvv>"],
["9", "7"]=>["<<"],
["9", "8"]=>["<"],
["9", "4"]=>["<<v"],
["9", "5"]=>["<v", "v<"],
["9", "6"]=>["v"],
["9", "1"]=>["<<vv", "vv<<"],
["9", "2"]=>["<vv", "vv<"],
["9", "3"]=>["vv"],
["9", "0"]=>["<vvv", "vvv<"],
["9", "A"]=>["vvv"],
["4", "7"]=>["^"],
["4", "8"]=>["^>", ">^"],
["4", "9"]=>["^>>", ">>^"],
["4", "5"]=>[">"],
["4", "6"]=>[">>"],
["4", "1"]=>["v"],
["4", "2"]=>[">v", "v>"],
["4", "3"]=>[">>v", "v>>"],
["4", "0"]=>[">vv"],
["4", "A"]=>[">>vv"],
["5", "7"]=>["<^", "^<"],
["5", "8"]=>["^"],
["5", "9"]=>["^>", ">^"],
["5", "4"]=>["<"],
["5", "6"]=>[">"],
["5", "1"]=>["<v", "v<"],
["5", "2"]=>["v"],
["5", "3"]=>[">v", "v>"],
["5", "0"]=>["vv"],
["5", "A"]=>[">vv", "vv>"],
["6", "7"]=>["<<^", "^<<"],
["6", "8"]=>["<^", "^<"],
["6", "9"]=>["^"],
["6", "4"]=>["<<"],
["6", "5"]=>["<"],
["6", "1"]=>["<<v", "v<<"],
["6", "2"]=>["<v", "v<"],
["6", "3"]=>["v"],
["6", "0"]=>["<vv", "vv<"],
["6", "A"]=>["vv"],
["1", "7"]=>["^^"],
["1", "8"]=>["^^>", ">^^"],
["1", "9"]=>["^^>>", ">>^^"],
["1", "4"]=>["^"],
["1", "5"]=>["^>", ">^"],
["1", "6"]=>["^>>", ">>^"],
["1", "2"]=>[">"],
["1", "3"]=>[">>"],
["1", "0"]=>[">v"],
["1", "A"]=>[">>v", ">v>"],
["2", "7"]=>["<^^", "^^<"],
["2", "8"]=>["^^"],
["2", "9"]=>["^^>", ">^^"],
["2", "4"]=>["<^", "^<"],
["2", "5"]=>["^"],
["2", "6"]=>["^>", ">^"],
["2", "1"]=>["<"],
["2", "3"]=>[">"],
["2", "0"]=>["v"],
["2", "A"]=>[">v", "v>"],
["3", "7"]=>["<<^^", "^^<<"],
["3", "8"]=>["<^^", "^^<"],
["3", "9"]=>["^^"],
["3", "4"]=>["<<^", "^<<"],
["3", "5"]=>["<^", "^<"],
["3", "6"]=>["^"],
["3", "1"]=>["<<"],
["3", "2"]=>["<"],
["3", "0"]=>["<v", "v<"],
["3", "A"]=>["v"],
["0", "7"]=>["^^^<"],
["0", "8"]=>["^^^"],
["0", "9"]=>["^^^>", ">^^^"],
["0", "4"]=>["^<^", "^^<"],
["0", "5"]=>["^^"],
["0", "6"]=>["^^>", ">^^"],
["0", "1"]=>["^<"],
["0", "2"]=>["^"],
["0", "3"]=>["^>", ">^"],
["0", "A"]=>[">"],
["A", "7"]=>["^^^<<"],
["A", "8"]=>["<^^^", "^^^<"],
["A", "9"]=>["^^^"],
["A", "4"]=>["^^<<"],
["A", "5"]=>["<^^", "^^<"],
["A", "6"]=>["^^"],
["A", "1"]=>["^<<"],
["A", "2"]=>["<^", "^<"],
["A", "3"]=>["^"],
["A", "0"]=>["<"]
}
# cache efficient direction movements
arrow_cache = arrow_grid
.flatten.reject { _1 == "#" || _2 == "#" }
.permutation(2).to_a
.map { [[_1, _2], dijkstra_with_lookup(arrow_grid, arrow_movements, _1, _2)] }
.map do |(start, target), (dest, prev)|
presses = ([discover(start, arrow_grid)] + prev)
.each_cons(2).to_a
.map { @dir_arrows[[_2.first - _1.first, _2.last - _1.last]] }
[[start, target], presses]
end
.to_h
arrow_cache[["<", "<"]] = ["A"]
arrow_cache[["^", "^"]] = ["A"]
arrow_cache[[">", ">"]] = ["A"]
arrow_cache[["v", "v"]] = ["A"]
arrow_cache[["A", "A"]] = ["A"]
# directional_map = directional_keypad
# .flatten.reject { _1.nil? }
# .permutation(2).to_a
# .map { [[_1, _2], dijkstra_with_lookup(directional_keypad, directional_movements, _1, _2)] }
# .map do |(start, target), (dest, paths)|
# presses = paths.map do |path|
# ([discover(start, directional_keypad)] + path)
# .each_cons(2).to_a
# .map { @dir_arrows[[_2.first - _1.first, _2.last - _1.last]] }.join
# end
# [[start, target], presses]
# end
# .to_h
robot_2 = input
directional_map = {
["^", "A"]=>[">"],
["^", "<"]=>["v<"],
["^", "v"]=>["v"],
["^", ">"]=>[">v", "v>"],
["A", "^"]=>["<"],
["A", "<"]=>["v<<", "<v<"],
["A", "v"]=>["v<", "<v"],
["A", ">"]=>["v"],
["<", "^"]=>[">^"],
["<", "A"]=>[">>^"],
["<", "v"]=>[">"],
["<", ">"]=>[">>"],
["v", "^"]=>["^"],
["v", "A"]=>["^>", ">^"],
["v", "<"]=>["<"],
["v", ">"]=>[">"],
[">", "^"]=>["^<", "<^"],
[">", "A"]=>["^"],
[">", "<"]=>["<<"],
[">", "v"]=>["<"]
}
robot_1 = input
robot_2 = robot_1
.map { ["A"] + _1 }
.map { _1.each_cons(2).to_a }
.map { _1.map { |move| number_cache[move].join }.join("A") + "A" }
debugger
.map { _1.map { |move| numeric_map[move].first }.join("A") }
.map { _1 + "A" }
robot_3 = robot_2
.map { _1.chars }
.map { ["A"] + _1 }
.map { _1.each_cons(2).to_a }
.map { _1.map { |move| arrow_cache[move].join }.join("A") + "A" }
.map do
_1.map do |move|
directional_map[move].nil? ? "" : directional_map[move].first
end.join("A")
end
.map { _1 + "A" }
me = robot_3
.map { _1.chars }
.map { ["A"] + _1 }
.map { _1.each_cons(2).to_a }
.map { _1.map { |move| arrow_cache[move].join }.join("A") + "A" }
pp "<vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A"
pp me
.map do
_1.map do |move|
directional_map[move].nil? ? "" : directional_map[move].first
end.join("A")
end
.map { _1 + "A" }
me.each_with_index do |entry, i|
p "#{entry.length} * #{input[i].join.to_i} = #{entry.length * input[i].join.to_i}"
end
p me.map.with_index { |entry, i| entry.length * input[i].join.to_i }.sum
#debugger
puts me.first
puts robot_3.first
puts robot_2.first
puts robot_1.first.join
__END__
029A