Skip to content

Commit b97288c

Browse files
committed
WIP
1 parent 3cd827c commit b97288c

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

2020/16/solve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ const solve2 = () => {
1818
};
1919

2020
solve1();
21-
// solve2();
21+
solve2();

2020/16/test.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
const { util1, util2, isWithinRange, isWithinSomeOfTheRanges } = require('./utils');
1+
const {
2+
util1,
3+
util2,
4+
isWithinRange,
5+
isWithinSomeOfTheRanges,
6+
getValidTickets,
7+
} = require('./utils');
28

39
describe('it should solve part 1 and part 2', () => {
410
const input1 = [
@@ -16,6 +22,22 @@ describe('it should solve part 1 and part 2', () => {
1622
'38,6,12',
1723
];
1824

25+
const input2 = [
26+
'class: 0-1 or 4-19',
27+
'row: 0-5 or 8-19',
28+
'seat: 0-13 or 16-19',
29+
'',
30+
'your ticket:',
31+
'11,12,13',
32+
'',
33+
'nearby tickets:',
34+
'3,9,18',
35+
'40,4,50',
36+
'55,2,20',
37+
'15,1,5',
38+
'5,14,9',
39+
];
40+
1941
test('it should solve example 1', () => {
2042
expect(isWithinRange(7, [1, 3, 5, 7])).toEqual(true);
2143
expect(isWithinRange(3, [1, 3, 5, 7])).toEqual(true);
@@ -42,11 +64,28 @@ describe('it should solve part 1 and part 2', () => {
4264
[13, 40, 45, 50],
4365
]),
4466
).toEqual(false);
45-
67+
4668
expect(util1(input1)).toEqual(71);
4769
});
4870

49-
/* test('it should solve example 2', () => {
50-
expect(util2(input1)).toEqual(0);
51-
}); */
71+
test('it should solve example 2', () => {
72+
util2(input2);
73+
const rules = [
74+
[0, 1, 4, 19],
75+
[0, 5, 8, 19],
76+
[0, 13, 16, 19],
77+
];
78+
const tickets = [
79+
[3, 9, 18],
80+
[40, 4, 50],
81+
[55, 2, 20],
82+
[15, 1, 5],
83+
[5, 14, 9],
84+
];
85+
expect(getValidTickets(rules, tickets)).toEqual([
86+
[3, 9, 18],
87+
[15, 1, 5],
88+
[5, 14, 9],
89+
]);
90+
});
5291
});

2020/16/utils.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ const parseRules = (input) =>
1010
})
1111
.filter(Boolean);
1212

13+
const getRuleNames = (input) =>
14+
input
15+
.map((row) => {
16+
const [, name] =
17+
row.match(/(\w+\s?\w+?): (\d+)-(\d+) or (\d+)-(\d+)/) || [];
18+
if (name !== undefined) {
19+
return name;
20+
}
21+
return undefined;
22+
})
23+
.filter(Boolean);
24+
1325
const parseTickets = (input) =>
1426
input
1527
.map((row) => {
@@ -52,9 +64,48 @@ const util1 = (input) => {
5264
return sum;
5365
};
5466

67+
const getValidTickets = (rules, tickets) =>
68+
tickets
69+
.map((ticket) => {
70+
if (ticket.some((value) => !isWithinSomeOfTheRanges(value, rules))) {
71+
return false;
72+
}
73+
return ticket;
74+
})
75+
.filter(Boolean);
76+
77+
const findMatchingRules = (rules, tickets, ruleNames) => {
78+
const matches = {};
79+
tickets.forEach((ticket) => {
80+
matches[ticket] = {};
81+
rules.forEach((rule, ruleIndex) => {
82+
ticket.forEach((value, valueIndex) => {
83+
if (isWithinRange(value, rule)) {
84+
matches[ticket][ruleNames[valueIndex]] = [
85+
...(matches[ticket][ruleNames[valueIndex]] || []),
86+
ruleIndex,
87+
];
88+
}
89+
});
90+
});
91+
});
92+
return matches;
93+
};
94+
5595
const util2 = (input) => {
56-
console.log(input);
57-
return input;
96+
const rules = parseRules(input);
97+
const [, ...nearbyTickets] = parseTickets(input);
98+
const validTickets = getValidTickets(rules, nearbyTickets);
99+
const ruleNames = getRuleNames(input);
100+
const matchingRules = findMatchingRules(rules, validTickets, ruleNames);
101+
Object.values(matchingRules).forEach((matchingRule) => console.log(matchingRule));
102+
return validTickets;
58103
};
59104

60-
module.exports = { util1, util2, isWithinRange, isWithinSomeOfTheRanges };
105+
module.exports = {
106+
util1,
107+
util2,
108+
isWithinRange,
109+
isWithinSomeOfTheRanges,
110+
getValidTickets,
111+
};

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