Skip to content

Instantly share code, notes, and snippets.

View gurgeous's full-sized avatar

Adam Doppelt gurgeous

  • Seattle, WA
View GitHub Profile
def solve(key)
(0..).inject(1) do |memo, n|
return n if memo == key
(memo * 7) % 20201227
end
end
def transform(subject, n)
(1..n).inject(1) { |memo, _| (memo * subject) % 20201227 }
end
DIRS = {
'e' => [ 1, 0 ],
'w' => [ -1, 0 ],
'sw' => [ -1, -1 ],
'se' => [ 0, -1 ],
'nw' => [ 0, 1 ],
'ne' => [ 1, 1 ],
}.freeze
#
# append to data
data += (data.max + 1..1000000).to_a
max = data.max
#
# build cups/lookup from data
#
Node = Struct.new(:value, :next)
players = data.split("\n\n").map do |s|
s.lines[1..].join.ints
end
# part 1
loop do
top = players.map(&:shift)
(top[0] > top[1]) ? players[0] += top : players[1] += top.reverse
if loser = players.find_index(&:empty?)
p players[1 - loser].reverse.map.with_index { |c, ii| c * (ii + 1) }.sum
# parse
foods = data.lines.map do |s|
s =~ /(.*) \(contains (.*)\)/
[ $1.split, $2.split(', ') ]
end
ingredients = foods.map(&:first).flatten.uniq.sort
allergens = foods.map(&:second).flatten.uniq.sort
#
# part 1
@gurgeous
gurgeous / 20b.rb
Last active December 20, 2020 06:55
#
# use this to print at the end of part 1
#
# (0...input.dim).each do |r|
# lines = []
# (0...input.dim).each do |c|
# tile = board[[ r, c ]]
# tile[1..-2].each.with_index do |line, index|
# (lines[index] ||= '') << line[1..-2].join
# end
input = Struct.new(:tiles, :dim).new({})
data.split("\n\n").map do |s|
lines = s.lines.map(&:chomp)
input.tiles[lines.first[/\d+/].to_i] = lines[1..].map(&:chars)
end
input.dim = input.tiles.length.sqrt.to_i
def coords(input, pos)
[ pos / input.dim, pos % input.dim ]
end
94: 118 64 | 22 34
21: 16 64 | 49 34
70: 58 34 | 106 64
100: 58 64 | 56 34
24: 29 64 | 128 34
63: 107 64 | 106 34
10: 64 64 | 34 64
1: 40 34 | 58 64
119: 56 34 | 103 64
131: 56 64 | 9 34
#
# parsing
#
top, messages = data.split("\n\n")
rules = []
top.lines.each do |s|
n, rule = s.split(': ')
rules[n.to_i] = case rule
when /^(\d+ ?)*$/ then [ :seq, rule.ints ]
space = Hash.new('.')
data.lines.map(&:chomp).each.with_index do |row, x|
row.each_char.with_index { |c, y| space[[ x, y, 0, 0 ]] = c }
end
6.times do
copy = space.dup
# bounds
coords = space.keys