Compare commits
No commits in common. "efa81418eb6c1856cb00cedee0294089ca42fcb2" and "5f2cb7c7ee458899a9bad95ab0b04b532eb7dce9" have entirely different histories.
efa81418eb
...
5f2cb7c7ee
11
day10.chpl
11
day10.chpl
@ -13,12 +13,11 @@ iter ops() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deltas = ops(),
|
const deltas = ops();
|
||||||
cycles = deltas.size,
|
const states = + scan deltas;
|
||||||
states: [1..cycles] int = + scan deltas,
|
const indices = 20..220 by 40;
|
||||||
interesting = 20..220 by 40;
|
writeln(+ reduce (states[indices-1] * indices));
|
||||||
writeln(+ reduce (states[interesting] * interesting));
|
|
||||||
|
|
||||||
const pixels = [(x, pc) in zip(states[1..240], 0..)]
|
const pixels = [(x, pc) in zip(states[0..<240], 0..)]
|
||||||
if abs((pc % 40) - x) <= 1 then "#" else " ";
|
if abs((pc % 40) - x) <= 1 then "#" else " ";
|
||||||
writeln(reshape(pixels, {1..6, 1..40}));
|
writeln(reshape(pixels, {1..6, 1..40}));
|
||||||
|
83
day12.cr
83
day12.cr
@ -1,83 +0,0 @@
|
|||||||
require "advent"
|
|
||||||
INPUT = input(2022, 12).lines.map(&.chars)
|
|
||||||
EDGES = {} of Tuple(Int32,Int32) => Array(Tuple(Int32, Int32))
|
|
||||||
|
|
||||||
def add_at(pos, c, x, y)
|
|
||||||
return unless y >= 0 && y < INPUT.size
|
|
||||||
return unless x >= 0 && x < INPUT[0].size
|
|
||||||
|
|
||||||
o = INPUT[y][x]
|
|
||||||
o = 'a' if o == 'S'
|
|
||||||
o = 'z' if o == 'E'
|
|
||||||
|
|
||||||
return if (c.ord-o.ord) > 1
|
|
||||||
EDGES[pos] << ({x,y});
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_nearby(x, y)
|
|
||||||
c = INPUT[y][x]
|
|
||||||
c = 'a' if c == 'S'
|
|
||||||
c = 'z' if c == 'E'
|
|
||||||
|
|
||||||
if !EDGES[{x,y}]?
|
|
||||||
EDGES[{x,y}] = [] of Tuple(Int32,Int32)
|
|
||||||
end
|
|
||||||
add_at({x,y}, c, x+1, y)
|
|
||||||
add_at({x,y}, c, x-1, y)
|
|
||||||
add_at({x,y}, c, x, y+1)
|
|
||||||
add_at({x,y}, c, x, y-1)
|
|
||||||
end
|
|
||||||
|
|
||||||
from = {0,0}
|
|
||||||
to = {100,100}
|
|
||||||
INPUT.each_with_index do |row, y|
|
|
||||||
row.each_with_index do |c, x|
|
|
||||||
pos = {x, y}
|
|
||||||
add_nearby(x, y)
|
|
||||||
from = pos if c == 'E'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
costs = {from => 0}
|
|
||||||
mins = {} of Tuple(Int32, Int32) => Int32
|
|
||||||
visited = Set(Tuple(Int32, Int32)).new
|
|
||||||
|
|
||||||
while !costs.empty?
|
|
||||||
k, v = costs.min_by do |k,v|
|
|
||||||
v
|
|
||||||
end
|
|
||||||
|
|
||||||
if k == to
|
|
||||||
puts "Found! #{v}"
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
costs.delete k
|
|
||||||
mins[k] = v
|
|
||||||
visited << k
|
|
||||||
INPUT[k[1]][k[0]] = INPUT[k[1]][k[0]].upcase
|
|
||||||
puts k
|
|
||||||
|
|
||||||
EDGES[k].each do |edge|
|
|
||||||
next if visited.includes? edge
|
|
||||||
if old = costs[edge]?
|
|
||||||
costs[edge] = v+1 if old > v+1
|
|
||||||
else
|
|
||||||
costs[edge] = v+1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
lengths = [] of Int32
|
|
||||||
puts mins
|
|
||||||
INPUT.each_with_index do |line, y|
|
|
||||||
line.each_with_index do |c, x|
|
|
||||||
next unless c == 'A'
|
|
||||||
if amin = mins[{x,y}]?
|
|
||||||
lengths << amin
|
|
||||||
end
|
|
||||||
print c
|
|
||||||
end
|
|
||||||
puts
|
|
||||||
end
|
|
||||||
puts lengths.min
|
|
Loading…
Reference in New Issue
Block a user