Skip to content

Instantly share code, notes, and snippets.

@saegeullee
Last active November 16, 2024 04:15
Show Gist options
  • Select an option

  • Save saegeullee/ac2f83326b4977770eddb60d4fbb720e to your computer and use it in GitHub Desktop.

Select an option

Save saegeullee/ac2f83326b4977770eddb60d4fbb720e to your computer and use it in GitHub Desktop.
Advent of Code 4번 문제
type Attribute = 'byr' | 'iyr' | 'eyr' | 'hgt' | 'hcl' | 'ecl' | 'pid' | 'cid';
type Passport = {
byr: string;
iyr: string;
eyr: string;
hgt: string;
hcl: string;
ecl: string;
pid: string;
cid?: string;
};
function part1(input:string):number {
const lines:string[] = input.split('\n');
lines.shift();
const passports:Partial<Passport>[] = [];
let current:Partial<Passport> = {};
for(const line of lines) {
if(line !== "") {
const attrs:string[] = line.split(' ')
for(const attr of attrs) {
const attrkey:Attribute = attr.split(":")[0] as Attribute;
current[attrkey] = attr.split(":")[1];
}
} else {
passports.push(current);
current = {};
}
}
const attributes:Attribute[] = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
let cnt:number = 0;
for(const passport of passports) {
let isValid:boolean = true;
for(const attr of attributes) {
if(!passport[attr]) {
isValid = false;
break;
}
}
if(isValid) cnt++;
}
return cnt;
}
function yearStrValidateFunc(yearStr:string, least:number, most:number): boolean {
if(yearStr.length !== 4) return false;
const byr:number = parseInt(yearStr, 10);
if(isNaN(byr)) return false;
if(byr >= least && byr <= most) return true;
return false;
}
function byrValidateFunc(arg:string):boolean {
return yearStrValidateFunc(arg, 1920, 2002);
}
function iyrValidateFunc(arg:string):boolean {
return yearStrValidateFunc(arg, 2010, 2020);
}
function eyrValidateFunc(arg:string):boolean {
return yearStrValidateFunc(arg, 2020, 2030);
}
function hgtValidateFunc(arg:string):boolean {
if(arg.endsWith('cm')) {
const matched: RegExpMatchArray | null = arg.match(/\d+/);
if(!matched) return false;
const hgt = parseInt(matched[0], 10);
if(hgt >= 150 && hgt <= 193) return true;
return false;
} else if(arg.endsWith('in')) {
const matched: RegExpMatchArray | null = arg.match(/\d+/);
if(!matched) return false;
const hgt = parseInt(matched[0], 10);
if(hgt >= 59 && hgt <= 76) return true;
return false;
}
return false
}
function hclValidateFunc(arg:string):boolean {
const regex = /^#[0-9a-f]{6}$/;
if (regex.test(arg)) return true;
return false
}
function eclValidateFunc(arg:string):boolean {
return ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(arg);
}
function pidValidateFunc(arg:string):boolean {
const regex = /^\d{9}$/;
if (regex.test(arg)) return true;
return false;
}
type ValidationFunctions = {
[K in Attribute]: (value: string) => boolean;
};
function part2(input:string):number {
const lines:string[] = input.split('\n');
lines.shift();
const passports:Partial<Passport>[] = [];
let current:Partial<Passport> = {};
for(const line of lines) {
if(line !== "") {
const attrs:string[] = line.split(' ')
for(const attr of attrs) {
const attrkey:Attribute = attr.split(":")[0] as Attribute;
current[attrkey] = attr.split(":")[1];
}
} else {
passports.push(current);
current = {};
}
}
const attributes:Attribute[] = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
const validPassports:Passport[] = [];
for(const passport of passports) {
let isValid:boolean = true;
for(const attr of attributes) {
if(!passport[attr]) {
isValid = false;
break;
}
}
if(isValid) validPassports.push(passport as Passport);
}
const validateFunctions:ValidationFunctions = {
'byr': byrValidateFunc,
'iyr': iyrValidateFunc,
'eyr': eyrValidateFunc,
'hgt': hgtValidateFunc,
'hcl': hclValidateFunc,
'ecl': eclValidateFunc,
'pid': pidValidateFunc,
"cid": (arg:string) => true
}
let cnt:number = 0;
for(const passport of validPassports) {
let isValid:boolean = true;
for(const [key, val] of Object.entries(passport)) {
const validateFunction = validateFunctions[key as Attribute];
if(!validateFunction(val)) {
isValid = false;
break;
}
}
if(isValid) cnt++
}
return cnt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment