m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/04/b.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:14:20 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:14:20 +0100
commit8216a91439c30ba2c6f57a49be2daef7ec6e596c (patch)
treea05cb67d42211aacbcf2b9fada606d8213c233ed /04/b.rb
parentd4792ef0e009a6c6f58d01e8387ac3cceb8c9687 (diff)
Add day 4
Diffstat (limited to '04/b.rb')
-rw-r--r--04/b.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/04/b.rb b/04/b.rb
new file mode 100644
index 0000000..e2cd254
--- /dev/null
+++ b/04/b.rb
@@ -0,0 +1,29 @@
+file = File.read('input.txt')
+records = file.split "\n\n"
+
+fields = [ 'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid' ]
+
+regexes = [
+ /^byr:(19[2-9]\d|200[0-2])$/,
+ /^iyr:(201\d|2020)$/,
+ /^eyr:(202\d|2030)$/,
+ /^hgt:((1[5-8]\d|19[0-3])cm|(59|6\d|7[0-6])in)$/,
+ /^hcl:#[0-9a-f]{6}$/,
+ /^ecl:(amb|blu|brn|gry|grn|hzl|oth)$/,
+ /^pid:\d{9}$/
+]
+
+count = 0
+records.each do |record|
+ fields_correct = 0
+ record.split.each do |entry|
+ if regexes.any? {|r| r.match?(entry)}
+ fields_correct += 1
+ end
+ end
+
+ if fields_correct == 7
+ count += 1
+ end
+end
+puts count