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
# 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 |
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
# | |
# 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 |
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
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 |
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
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 |
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
# | |
# 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 ] |
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
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 |
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
# | |
# 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?) |
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
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 |
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
# | |
# part 1 | |
# | |
mem = Hash.new(0) | |
on = off = nil | |
data.lines.each do |s| | |
case s | |
when /mask = (\w+)/ | |
mask = $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
# 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 |