-
-
Save alistairtweed/09d448b1c5caeb7dd859399f55c4f152 to your computer and use it in GitHub Desktop.
Advent of Code
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
data = File.readlines('./input/2.txt').map(&:strip).map do |line| | |
range, character, string = line.split /:? / | |
[range.split('-').map(&:to_i), character, string] | |
end | |
# Part 1 | |
valid = data.select do |range, character, string| | |
string.count(character).between?(*range) | |
end | |
puts valid.length | |
# Part 2 | |
valid = data.select do |indices, character, string| | |
candidates = indices.map { |i| string[i-1] } | |
candidates.include?(character) && candidates.uniq.length > 1 | |
end | |
puts valid.length |
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 = File.readlines('./input/3.txt').map(&:strip) | |
num_columns = map[0].length | |
# Part 1 | |
column = 0 | |
row = 0 | |
trees = 0 | |
while row < map.length - 1 do | |
column += 3 | |
row += 1 | |
if column >= num_columns | |
column = column - num_columns | |
end | |
trees += map[row][column] == '#' ? 1 : 0 | |
end | |
puts trees | |
# Part 2 | |
slopes = [ | |
[1, 1], | |
[3, 1], | |
[5, 1], | |
[7, 1], | |
[1, 2] | |
] | |
product = 1 | |
slopes.each do |x, y| | |
column = 0 | |
row = 0 | |
trees = 0 | |
while row < map.length - 1 do | |
column += x | |
row += y | |
if column >= num_columns | |
column = column - num_columns | |
end | |
trees += map[row][column] == '#' ? 1 : 0 | |
end | |
product *= trees | |
end | |
puts product |
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
passports = File.read('./input/4.txt').strip.split(/\n\n/) | |
.map { |passport| passport.split(/\s/).map { |row| row.split(':') }.to_h } | |
# Part 1 | |
valid = passports | |
.select { |passport| (passport.keys.sort - ['cid']) == %w[byr ecl eyr hcl hgt iyr pid] } | |
puts valid.length | |
# Part 2 | |
valid = passports | |
.select { |passport| (passport.keys.sort - ['cid']) == %w[byr ecl eyr hcl hgt iyr pid] } | |
.select { |passport| passport['byr'] =~ /\A\d{4}\z/ && passport['byr'].to_i.between?(1920, 2002) } | |
.select { |passport| passport['iyr'] =~ /\A\d{4}\z/ && passport['iyr'].to_i.between?(2010, 2020) } | |
.select { |passport| passport['eyr'] =~ /\A\d{4}\z/ && passport['eyr'].to_i.between?(2020, 2030) } | |
.select { |passport| passport['hcl'] =~ /\A#[0-9a-f]{6}\z/ } | |
.select { |passport| passport['ecl'] =~ /\A(amb|blu|brn|gry|grn|hzl|oth)\z/ } | |
.select { |passport| passport['pid'] =~ /\A\d{9}\z/ } | |
.select { |passport| passport['hgt'] =~ /\A\d+(cm|in)\z/ } | |
.select do |passport| | |
if passport['hgt'] =~ /cm/ | |
passport['hgt'][/\d+/].to_i.between?(150, 193) | |
else | |
passport['hgt'][/\d+/].to_i.between?(59, 76) | |
end | |
end | |
puts valid.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment