-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.js
More file actions
27 lines (21 loc) · 726 Bytes
/
day4.js
File metadata and controls
27 lines (21 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import fs from 'fs'
const rangePairs = fs
.readFileSync('../inputs/day4.txt')
.toString('utf-8')
.split('\n')
.filter((l) => l)
const parseRangePair = (rangePair) => {
const match = rangePair
.match(/(\d+)-(\d+),(\d+)-(\d+)/)
.slice(1)
.map(Number)
return match
}
const checkContaining = ([start1, end1, start2, end2]) =>
(start1 <= start2 && end1 >= end2) || (start2 <= start1 && end2 >= end1)
const checkOverlaping = ([start1, end1, start2, end2]) =>
Math.max(start1, start2) <= Math.min(end1, end2)
const part1 = rangePairs.map(parseRangePair).filter(checkContaining).length
const part2 = rangePairs.map(parseRangePair).filter(checkOverlaping).length
console.log(part1)
console.log(part2)