Skip to content

Commit 23366da

Browse files
committed
added day 2
1 parent bec4f4e commit 23366da

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

day4/program.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@ const fs = require('fs');
33
fs.readFile('./input.txt', 'utf-8', (e, data) => {
44
const items = data.split("\n");
55
const rawPassports = findPassports(items);
6-
const passports = splitPassportData(rawPassports);
6+
const passportsCheck1 = splitPassportData(rawPassports);
7+
8+
const toValidatePart2 = [];
79

8-
let validPassports = 0;
9-
passports.forEach(p => {
10+
passportsCheck1.forEach((p, i) => {
1011
if (validatePassport(p)) {
12+
toValidatePart2.push(p);
13+
}
14+
});
15+
16+
console.log(`Part1: Found ${toValidatePart2.length} valid passports`);
17+
18+
let validPassports = 0;
19+
20+
toValidatePart2.forEach((p, i) => {
21+
if (validatePassportProperties(p)){
1122
validPassports++;
1223
}
1324
});
1425

15-
console.log(`Found ${validPassports} valid passports`);
26+
console.log(`Part2: Found ${validPassports} valid passports`);
1627

1728
});
1829

@@ -39,7 +50,9 @@ function splitPassportData(raw) {
3950
const parts = row.split(' ');
4051
parts.forEach(part => {
4152
let split = part.split(':');
42-
passport[split[0]] = split[1]
53+
if (split[0] === 'byr' || split[0] === 'iyr' || split[0] === 'eyr' ) split[1] = parseInt(split[1]);
54+
55+
passport[split[0]] = split[1];
4356
})
4457
});
4558
passports.push(passport);
@@ -57,4 +70,24 @@ function validatePassport(passport) {
5770
}
5871

5972
return false;
60-
}
73+
}
74+
75+
function validatePassportProperties(passport) {
76+
if (passport.byr > 2002 || passport.byr < 1920) return false;
77+
if (passport.iyr > 2020 || passport.iyr < 2010) return false;
78+
if (passport.eyr > 2030 || passport.eyr < 2020) return false;
79+
if (!validateHeight(passport.hgt)) return false;
80+
if (!passport.hcl.match(/#[0-9a-f]{6}/)) return false;
81+
if (['amb','blu','brn','gry','grn','hzl','oth'].indexOf(passport.ecl) === -1) return false;
82+
if (passport.pid.length != 9 || !passport.pid.match(/[0-9]{9}/)) return false;
83+
return true;
84+
}
85+
86+
function validateHeight(height) {
87+
let replace = height.indexOf('in') > -1 ? 'in' : 'cm';
88+
height.replace(replace,'');
89+
height = parseInt(height);
90+
if (replace === 'in' && (height > 76 || height < 59)) return false;
91+
if (replace === 'cm' && (height > 193 || height < 150)) return false;
92+
return true;
93+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy