Skip to content

Instantly share code, notes, and snippets.

View gurgeous's full-sized avatar

Adam Doppelt gurgeous

  • Seattle, WA
View GitHub Profile
# 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
#
# parse
#
rules, your, nearby = data.split("\n\n")
rules = rules.lines.map do |r|
r.scan(/(\d+)-(\d+)/).map { |x| Range.new(*x.map(&:to_i)) }
end
your = your.scan(/\d+/).map(&:to_i)
nearby = nearby.lines.map { |l| l.scan(/\d+/).map(&:to_i) }.select(&:present?)
@gurgeous
gurgeous / 15.rb
Created December 15, 2020 05:29
15.rb
map = Hash.new { |h, v| h[v] = [] }
val = nil
(0...2020).each do |turns|
val = if turns < data.length
data[turns]
elsif map[val].length >= 2
map[val][-1] - map[val][-2]
else
0
end
#
# part 1
#
mem = Hash.new(0)
on = off = nil
data.lines.each do |s|
case s
when /mask = (\w+)/
mask = $1
# part 1
earliest = data.lines.first.to_i
ids = data.lines[1].split(',').map(&:to_i)
m = earliest
loop do
ids.each do |id|
if id != 0 && m % id == 0
p id * (m - earliest)
exit
end