Last active
December 24, 2019 16:29
-
-
Save budnik/7e9caa003681fc67008c66e81e011ee4 to your computer and use it in GitHub Desktop.
advent2019
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
def move(d, l, c) | |
case d | |
when ?R then [c.first+l, c.last] | |
when ?L then [c.first-l, c.last] | |
when ?U then [c.first, c.last-l] | |
when ?D then [c.first, c.last+l] | |
end | |
end | |
def r(a,b) | |
Range.new(*[a,b].sort) | |
end | |
# s1='R8,U5,L5,D3' | |
# s2='U7,R6,D4,L4' | |
summ1= []; | |
summ2= []; | |
s1.split(',').inject([0,0]) { |(cx,cy), i| d,n= i[0], i[1..]; (summ1<<[[cx,cy], move(d,n.to_i, [cx,cy])]).last.last} | |
s2.split(',').inject([0,0]) { |(cx,cy), i| d,n= i[0], i[1..]; (summ2<<[[cx,cy], move(d,n.to_i, [cx,cy])]).last.last} | |
s=[];summ2.each{|(ax,ay),(bx,by)| summ1.each{|(aax,aay),(bbx,bby)| pp [ax,ay,bx,by,aax,aay,bbx,bby].inspect; s<<[ax,ay,bx,by,aax,aay,bbx,bby] if (aax==bbx && ay == by && r(ax,bx)===aax && r(aay,bby)===ay)||(aay==bby && ax == bx && r(ay,by)===aay && r(aax,bbx)===ax) }} | |
xss=s.map{|ss| [ss.each_slice(2).group_by(&:first).select{|k,v| v.size == 2}.keys.first, ss.each_slice(2).group_by(&:last).select{|k,v| v.size == 2}.keys.first] } | |
cc=0;c=0;rr=Hash.new; s2.split(',').inject([0,0]) { |(cx,cy), i| d,n= i[0], i[1..]; n.to_i.times{ c+=1; cx,cy= move(d,1,[cx,cy]); rr[(xss&[[cx,cy]]).first]||=c }; [cx,cy]} | |
cc=0;c=0;r=Hash.new; s1.split(',').inject([0,0]) { |(cx,cy), i| d,n= i[0], i[1..]; n.to_i.times{ c+=1; cx,cy= move(d,1,[cx,cy]); r[(xss&[[cx,cy]]).first]||=c }; [cx,cy]} | |
pp (r.each_pair.map{|k,v| [k, rr[k]+v] }.sort_by(&:last))[1][1] |
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
a = 10007.times.to_a | |
input = 'input'.lines.map(&:chomp).compact | |
input.each do |l| | |
case l | |
when /^deal with increment (.*)$/ then a=loop.with_index.inject(Array.new(a.size)){|n,(_,i)| break n if a.empty? ; n[Regexp.last_match(1).to_i*i%n.size] = a.shift; n } | |
when /^cut (.*)$/ then a=a.rotate(Regexp.last_match(1).to_i) | |
when /deal into new stack/ then a=a.reverse | |
when /done/ then pp(a.find_index(2019)) | |
else | |
binding.irb | |
end | |
end |
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
def neigbours(i,j) | |
((0..4).to_a&[i-1, i+1]).product([j]) + [i].product((0..4).to_a&[j-1, j+1]) | |
end | |
def ng(c,n) | |
case c | |
when ?# then n==1 ? ?# : ?. | |
when ?. then (1..2)===n ? ?# : c | |
end | |
end | |
I=input.each_line.map(&:chomp).map(&:each_char).map(&:to_a) | |
s=Set.new; loop.inject(I.dup){ |ii| iii=ii.dup; s<<ii.hash; ii.map!.with_index{|l, i| l.map.with_index{|c,j| n = neigbours(i,j).map{|(a,b)| iii[a][b]}.count(?#); ng(c,n) }}; break ii if s.include?(ii.hash); ii } | |
.flatten.map.with_index{|c,i| c == ?# ? 2**i : 0 }.sum |
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
def neigbours(d, i,j) | |
raise if i == 2 && j == 2 | |
n = (((0..4).to_a&[i-1, i+1]).product([j]) + [i].product((0..4).to_a&[j-1, j+1]) - [[2,2]]).map(&[d].method(:+)) | |
n += [[d+1, 1, 2]] if i == 0 | |
n += [[d+1, 2, 1]] if j == 0 | |
n += [[d+1, 3, 2]] if i == 4 | |
n += [[d+1, 2, 3]] if j == 4 | |
n += 5.times.to_a.map{|p| [d-1, 0, p] } if i == 1 && j == 2 | |
n += 5.times.to_a.map{|p| [d-1, p, 0] } if i == 2 && j == 1 | |
n += 5.times.to_a.map{|p| [d-1, 4, p] } if i == 3 && j == 2 | |
n += 5.times.to_a.map{|p| [d-1, p, 4] } if i == 2 && j == 3 | |
n | |
end | |
def ng(c,n) | |
case c | |
when ?# then n==1 ? ?# : ?. | |
else | |
(1..2)===n ? ?# : c | |
end | |
end | |
OI = '####. | |
.##.. | |
##.#. | |
###.. | |
##..# | |
' | |
II = '....# | |
#..#. | |
#.?## | |
..#.. | |
#....' | |
result = Hash.new { Array.new(5, Array.new(5, '')) } | |
result[0] = OI.each_line.map(&:chomp).map(&:each_char).map(&:to_a) | |
final = 200.times.inject(result.dup) do |r, i| | |
old = r.dup | |
(r.keys.min - 1).upto(r.keys.max+1).map do |d| | |
maze = 5.times.map do |i| | |
5.times.map do |j| | |
next if i == 2 && j == 2 | |
c = old.dig(d,i,j) | |
n = neigbours(d,i,j).count{|(a,b,c)| old.dig(a,b,c) == ?# } | |
ng(c,n) | |
end | |
end | |
next unless maze.flatten.include?(?#) | |
[d, maze] | |
end.compact.to_h | |
end | |
pp final.values.flatten.count ?# |
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
v = 1,2,3 | |
input = 5 | |
i=0 | |
loop do | |
op1, op2, p1, p2, p3 = (1_000_000 + v[i]).digits | |
break if op1 == op2 && op1 == 9 | |
case op1 | |
when 3 | |
v[v[i+1]] = input | |
i+= 2 | |
when 4 | |
puts(p1.zero? ? v[v[i+1]]: v[i+1]) | |
i+= 2 | |
when 1 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) + (p2.zero? ? v[v[i+2]]: v[i+2]) | |
i+=4 | |
when 2 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) * (p2.zero? ? v[v[i+2]]: v[i+2]) | |
i+=4 | |
when 5 | |
pv = (p1.zero? ? v[v[i+1]]: v[i+1]) | |
!pv.zero? ? i = (p2.zero? ? v[v[i+2]]: v[i+2]) : i+=3 | |
when 6 | |
pv = (p1.zero? ? v[v[i+1]]: v[i+1]) | |
pv.zero? ? i = (p2.zero? ? v[v[i+2]]: v[i+2]) : i+=3 | |
when 7 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) < (p2.zero? ? v[v[i+2]]: v[i+2]) ? 1 : 0 | |
i+=4 | |
when 8 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) == (p2.zero? ? v[v[i+2]]: v[i+2]) ? 1 : 0 | |
i+=4 | |
end | |
end |
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
V = 3,8,1001,8,10,8,105,1,0,0,21,34,43,60,81,94,175,256,337,418,99999,3,9,101,2,9,9,102,4,9,9,4,9,99,3,9,102,2,9,9,4,9,99,3,9,102,4,9,9,1001,9,4,9,102,3,9,9,4,9,99,3,9,102,4,9,9,1001,9,2,9,1002,9,3,9,101,4,9,9,4,9,99,3,9,1001,9,4,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,99 | |
def calc(*input) | |
v = V.dup | |
inputs = input.reverse_each | |
i=0 | |
loop do | |
op1, op2, p1, p2, p3 = (1_000_000 + v[i]).digits | |
break if op1 == op2 && op1 == 9 | |
case op1 | |
when 3 | |
v[v[i+1]] = inputs.next | |
i+= 2 | |
when 4 | |
return(p1.zero? ? v[v[i+1]]: v[i+1]) | |
i+= 2 | |
when 1 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) + (p2.zero? ? v[v[i+2]]: v[i+2]) | |
i+=4 | |
when 2 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) * (p2.zero? ? v[v[i+2]]: v[i+2]) | |
i+=4 | |
when 5 | |
pv = (p1.zero? ? v[v[i+1]]: v[i+1]) | |
!pv.zero? ? i = (p2.zero? ? v[v[i+2]]: v[i+2]) : i+=3 | |
when 6 | |
pv = (p1.zero? ? v[v[i+1]]: v[i+1]) | |
pv.zero? ? i = (p2.zero? ? v[v[i+2]]: v[i+2]) : i+=3 | |
when 7 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) < (p2.zero? ? v[v[i+2]]: v[i+2]) ? 1 : 0 | |
i+=4 | |
when 8 | |
v[v[i+3]] = (p1.zero? ? v[v[i+1]]: v[i+1]) == (p2.zero? ? v[v[i+2]]: v[i+2]) ? 1 : 0 | |
i+=4 | |
end | |
end | |
end | |
pp 5.times.to_a.permutation.map{|ps| [ps, ps.inject(0, &method(:calc))]}.sort_by(&:last).last |
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
#d1t0 | |
a[1..2]=12,2;(0..).each{|i| i=4*i; return aa[0] if aa[i] == 99; op = aa[i] == 1 ? :+ : :* ; aa[aa[i+3]] = aa[aa[i+1]].send(op, aa[aa[i+2]])} | |
#d1t1 | |
(0..99).each{|n| (0..99).each{|v| zzz=input.dup; zzz[1..2] = n,v ;return [n,v] if d1t0(zzz)==result} | |
#d4t0 | |
N.upto(M).select{|p| c =p.to_s.chars; (c == c.sort) && p.to_s[/00|11|22|33|44|55|66|77|88|99/]}.count | |
#d4t1 | |
N.upto(M).select{|p| c =p.to_s.chars; (c == c.sort) && p.to_s[/(([^0]|^)00([^0]|$))|(([^1]|^)11([^1]|$))|(([^2]|^)22([^2]|$))|(([^3]|^)33([^3]|$))|(([^4]|^)44([^4]|$))|(([^5]|^)55([^5]|$))|(([^6]|^)66([^6]|$))|(([^7]|^)77([^7]|$))|(([^8]|^)88([^8]|$))|(([^9]|^)99([^9]|$))/] }.count | |
#d12t1 | |
ss=Matrix[[7,10,17],[-2,7,0],[12,5,12],[5,-8,6]]; | |
[vvv=1000.times.inject(Matrix.build(4,3){0}){|vv| ss+=vv; vv+Matrix[*ss.to_a.transpose.map{|a| a.map{|n|a.sort.reverse.index(n)-a.sort.index(n)} }.transpose]},ss+vvv].map{|m| m.to_a.map{|mm| mm.map(&:abs).reduce(:+)}}.transpose.map{|a,b|a*b}.sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment