Skip to content

Commit

Permalink
update 20210903
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Dvorak committed Sep 3, 2021
1 parent a182a45 commit 50153a2
Show file tree
Hide file tree
Showing 7 changed files with 723 additions and 611 deletions.
Binary file modified AOC2018/main
Binary file not shown.
1,113 changes: 565 additions & 548 deletions AOC2018/main.rs

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions gzView.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@ class String
def display_length; gsub(/\e.*?m/,"").length; end
def ljust_d(len); self + " " * [len - display_length, 0].max; end
def bytes_to_glyphs
r = ""
prev_high = false
self.each_byte do |b|
if b <= 127
r += "\e[27m" if prev_high
prev_high = false
c = b
else
r += "\e[7m" if !prev_high
prev_high = true
c = 255 - b
end
r += case c when 0x00 .. 0x1F then (0x2400 + c).chr(Encoding::UTF_8)
when 0x20 then "\u2423"
when 0x21 .. 0x7E then c.chr(Encoding::UTF_8)
when 0x7F then "\u2421"
end
end
r + (prev_high ? "\e[27m" : "")
self.force_encoding(Encoding::UTF_8)
.chars.map do |c|
case
when c == "\0" then "\e[7m \e[27m"
when c.valid_encoding? && c.ord > 0x20 then c
else
bits = c.bytes[0]
dots = bits[7] << 0 | bits[3] << 3 |
bits[6] << 1 | bits[2] << 4 |
bits[5] << 2 | bits[1] << 5 |
bits[4] << 6 | bits[0] << 7
(0x2800 + dots).chr(Encoding::UTF_8)
end
end.join
end
end

Expand Down Expand Up @@ -365,17 +360,22 @@ def show_parse_block bit_reader, out_buf, stats, quiet:, extrapolate:
end

puts "#" * 80 unless quiet
fbits = lambda do |str|
str.gsub(/\d ?/).with_index do |d, i|
"#{"\e[0m" if i % 8 == 0}#{"\e[30;1m" if i % 8 == 4}#{d}"
end + "\e[0m"
end
loop do
at = out_buf.size
key, code = bit_reader.read_huffman litlen_codes
stats[:block_counts][code] += 1
if code < 256
out_buf << code.chr
puts "@#{at} #{key} - #{code} - #{NEW_STR if stats[:block_counts][code] == 1}"\
puts "@#{at} #{fbits[key]} - #{code} - #{NEW_STR if stats[:block_counts][code] == 1}"\
"literal #{code.chr.bytes_to_glyphs}".ljust_d(50) + code.chr.bytes_to_glyphs unless quiet
stats[:lit_blocks] += 1
elsif code == 256
puts "@#{at} #{key} - #{code} - end of block" unless quiet
puts "@#{at} #{fbits[key]} - #{code} - end of block" unless quiet
puts "#" * 80
if bfinal && extrapolate
bit_reader.start_random!
Expand All @@ -402,7 +402,7 @@ def show_parse_block bit_reader, out_buf, stats, quiet:, extrapolate:
buf_end = out_buf.length - offset
buf_after = [buf_end + 5, out_buf.length].min
unless quiet
puts ("@#{at} #{key} #{extra.join} #{okey} #{oextra.join} - repeat" +
puts ("@#{at} #{fbits["#{key} #{extra.join} #{okey} #{oextra.join}"]} - repeat" +
" #{NEW_STR if stats[:block_counts][code] == 1}#{length}" +
" #{NEW_STR if stats[:offset_counts][ocode] == 1}#{offset}").ljust_d(45) +
"\e[31m#{out_buf[buf_before ... buf_start].bytes_to_glyphs}\e[0m" +
Expand Down
66 changes: 43 additions & 23 deletions ident_subseq_dyn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def subseq_enum str
t_prev = nil
enum.each do |c|
if !t_prev || Time.now - t_prev > 0.1
puts "#{highlight_diff(str, c.join, "")}\e[1A"
puts "#{highlight_diff(str, c.join, "", @letter_only)}\e[1A"
t_prev = Time.now
end
y.yield c.join
Expand All @@ -87,10 +87,12 @@ def subseq_enum str
end
end

attr_reader :strs
attr_reader :strs, :orig_strs

def initialize(letter_only: false, tiebreak: :first, slow_mode: :normal, sort: nil,
count_uniq: false, redundant_mode: false, min_chars: 0,
recursive_mode: false, reverse_regex: false, verbose: false)
@orig_strs = []
@strs = []
@enums = []
@tiebreak = tiebreak
Expand All @@ -114,15 +116,13 @@ def dup_empty
min_chars: @min_chars, recursive_mode: @recursive_mode)
end

def push str
str = str.downcase if @letter_only
def push orig_str
str = orig_str
str = str.downcase.gsub(/[^a-z]/, "") if @letter_only
return false if @strs.include? str
@orig_strs << orig_str
@strs << str
if @letter_only
@enums << subseq_enum(str.gsub(/[^a-z]/, "")).with_index
else
@enums << subseq_enum(str).with_index
end
@enums << subseq_enum(str).with_index
true
end

Expand Down Expand Up @@ -253,18 +253,23 @@ def initialize calc, opts
end

def strs; @top.strs; end
def orig_strs; @top.orig_strs; end
def push str; @top.push(str) && (@res = nil; true); end

def results
return @res if @res
strs = @slow_mode == :normal ? @top.results : @top.strs
chlen_goal = strs.map(&:length).max
@verbose_out = []
loop do
calc = @top.dup_empty.push_all(strs)
(puts "", strs; sleep 0.1) if @animation_mode
case @slow_mode
when :super_slow
changes = strs.zip(calc.results_with_index).filter{|x, (y, _)| x != y}
changes = strs.each.with_index
.filter{|x, _| x.length >= chlen_goal}
.map{|x, ix| [x, calc.result_with_index(ix)]}
.filter{|x, (y, _)| x != y}
chlen_max = changes.map{|x, _| x.length}.max
changes.filter!{|x, _| x.length == chlen_max}
chpos_min = changes.map{|_, (_, ix)| ix}.min
Expand All @@ -283,8 +288,13 @@ def results
count_hsh.fetch(k, []).length
end
end
return @res if @res == strs
strs = @res
if @res != strs
strs = @res
elsif @slow_mode == :super_slow && chlen_goal > 1
chlen_goal -= 1
else
return @res
end
end
end

Expand All @@ -293,14 +303,24 @@ def verbose_out; @verbose_out.dup; end

################################################################################

def find_subseq haystack, needle
last_ix = -1
needle.chars.map{|c| last_ix = haystack.index(c, last_ix + 1)}
def find_subseq haystack, needle, fold_case
if fold_case
last_ix = -1
needle.chars.map do |c|
last_ix = [
haystack.index(c.downcase, last_ix + 1),
haystack.index(c.upcase, last_ix + 1)
].compact.min
end
else
last_ix = -1
needle.chars.map{|c| last_ix = haystack.index(c, last_ix + 1)}
end
end

def highlight_diff in_str, new_str, old_str
new_ixes = find_subseq in_str, new_str if new_str.is_a? String
old_ixes = find_subseq in_str, old_str if old_str.is_a? String
def highlight_diff in_str, new_str, old_str, fold_case
new_ixes = find_subseq in_str, new_str, fold_case if new_str.is_a? String
old_ixes = find_subseq in_str, old_str, fold_case if old_str.is_a? String

in_str.chars.map.with_index do |c, ix|
color = case [new_ixes.include?(ix), old_ixes.include?(ix)]
Expand Down Expand Up @@ -378,17 +398,17 @@ def highlight_diff in_str, new_str, old_str
time_delta = Time.now - time_start
time_delta_sum += time_delta

results_changed = calc.strs.zip(old_results, results).filter{|_, old_str, new_str| old_str != new_str}.sort
results_changed = calc.orig_strs.zip(calc.strs, old_results, results).filter{|_, _, old_str, new_str| old_str != new_str}.sort
res_delta_sum += results_changed.count
results_changed.each do |in_str, old_str, new_str|
results_changed.each do |orig_str, in_str, old_str, new_str|
if old_str == new_str
nil
elsif tiebreak == :all && old_str && new_str[0].length == old_str[0].length
puts "- %p = %p | %s" % [old_str - new_str, new_str, in_str]
puts "- %p = %p | %s" % [old_str - new_str, new_str, orig_str]
elsif tiebreak == :all
puts "%p => %p | %s" % [old_str, new_str, in_str]
puts "%p => %p | %s" % [old_str, new_str, orig_str]
else
puts "%p => %p | %s" % [old_str || "", new_str, highlight_diff(in_str, new_str, old_str || "")]
puts "%p => %p | %s" % [old_str || "", new_str, highlight_diff(orig_str, new_str, old_str || "", letter_only)]
end
end

Expand Down
42 changes: 37 additions & 5 deletions ruby-snippets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,9 @@ def upscale(str, n=2)
end

require "io/console"
def progress_bar progress, width = IO.console.winsize[1] - 1
def progress_bar progress, text = "", width = IO.console.winsize[1] - 1
on_cells = ((width - 2) * progress.clamp(0 .. 1)).round
off_cells = width - 2 - on_cells
"[" + "█" * on_cells + " " * off_cells + "]"
(text.ljust(width - 2) + "]").insert(on_cells, "\e[27m").insert(0, "[\e[7m")
end

def relax_rescale(grid, f: 0.1, n: :n4, s: [])
Expand All @@ -214,6 +213,17 @@ def relax_rescale(grid, f: 0.1, n: :n4, s: [])
precision = 16 if precision > 16
(puts "warning: precision = #{precision}"; precision = 1) if precision < 1

fmt_secs = lambda do |s|
return "" unless s.finite?
return "%.2fs" % [s] if s < 60
m, s = s.divmod 60
return "%dm %0.2fs" % [m, s] if m < 60
h, m = m.divmod 60
return "%dh %dm %0.2fs" % [h, m, s] if h < 24
d, h = h.divmod 24
return "%dd %dh %dm %0.2fs" % [d, h, m, s]
end

fmt = lambda do |i, j, val|
logval = Math.log10(val.abs) / -10 if val
c = ->x{(255 * x).round.clamp(0, 255)}
Expand Down Expand Up @@ -252,6 +262,9 @@ def relax_rescale(grid, f: 0.1, n: :n4, s: [])

prev_grid = nil
prev_frame_t = nil
prev_max_delta = nil
time_start = Time.now
smooth_time_est = Float::NAN
loop.with_index do |_, t|
energy = 0
grid = (0 ... grid.size).map do |i|
Expand Down Expand Up @@ -286,6 +299,21 @@ def relax_rescale(grid, f: 0.1, n: :n4, s: [])
last_frame = t > 1 && scale < 1e-10 || (max_delta &.<= 2 * Float::EPSILON)

if(t == 0 || Time.now - prev_frame_t > 0.1 || last_frame)
time_est = if prev_max_delta && (max_delta < prev_max_delta || max_delta > 1e-15)
delta_goal = max_delta < prev_max_delta ? Float::EPSILON : 1
(Time.now - prev_frame_t) *
(Math.log(max_delta) - Math.log(delta_goal)) /
(Math.log(prev_max_delta) - Math.log(max_delta))
else Float::NAN
end

smooth_time_est = case
when !smooth_time_est.finite? then time_est
when !time_est.finite? then smooth_time_est
else 0.9 * smooth_time_est + 0.1 * time_est
end


cout = [""]
grid.each.with_index{|row, i| cout << row.map.with_index{|val, j| fmt[i, j, val]}.join(" ").rstrip}
cout << ""
Expand All @@ -294,7 +322,10 @@ def relax_rescale(grid, f: 0.1, n: :n4, s: [])
cout << "delta_1 = %.16f" % [(1 - scale) / strength]
# cout << "suppression factors = %p" % [suppression_factors.map{|f| "%.1e" % f}]
# \e[A moves cursor up; \e[?25l hides it; \e[?25h shows it again
cout << progress_bar(Math.log(max_delta || 1) / Math.log(Float::EPSILON))
cout << progress_bar(
Math.log(max_delta || 1) / Math.log(Float::EPSILON),
"elapsed: #{fmt_secs[Time.now - time_start]} | remaining: #{fmt_secs[smooth_time_est]}"
)
cout << "\e[#{cout.size}A\e[?25l" unless last_frame
print cout.join("\n")
return nil if last_frame && scale < 1e-10
Expand All @@ -303,6 +334,7 @@ def relax_rescale(grid, f: 0.1, n: :n4, s: [])
delta_1: (1 - scale) / strength
} if last_frame
prev_frame_t = Time.now
prev_max_delta = max_delta
end
prev_grid = grid.dup
end
Expand Down Expand Up @@ -343,7 +375,7 @@ def foo(x, limit = nil, n: :n4, f: 0.1, grid: nil, lowcolor: false, hicolor: fal
g, r, b = (limit.nil? ? accepted_modes : accepted_modes.max(3)).map(&:last)
plan = (0 ... xs.size).map do |i|
(0 ... xs[i].size).map do |j|
if xs[i][j] != ' '
if g[i][j]
[i, j] + [r, g, b].map do |c|
case
when c.nil? then 128
Expand Down
Binary file modified tmp.wvrz
Binary file not shown.
Loading

0 comments on commit 50153a2

Please sign in to comment.