-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3question2.ts
More file actions
162 lines (123 loc) · 3.97 KB
/
day3question2.ts
File metadata and controls
162 lines (123 loc) · 3.97 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { readFileSync } from "fs";
class TestableInput {
value: string;
meetsCriteria: boolean = true;
constructor(value: string) {
this.value = value;
}
test(bitIndex: number, expectedBitValue: number) {
if (Number(this.value[bitIndex]) !== expectedBitValue) {
this.meetsCriteria = false;
}
}
}
class BitCounter {
private zeroCount: number = 0;
private oneCount: number = 0;
private favoredBitValue: 0 | 1;
constructor(favoredBitValue: 0 | 1) {
this.favoredBitValue = favoredBitValue;
}
public record(bitValue: number) {
if (bitValue === 0) {
this.zeroCount++;
} else if (bitValue === 1) {
this.oneCount++;
}
}
public getMostCommonBit(): number {
if (this.zeroCount === this.oneCount) {
return this.favoredBitValue;
}
return this.zeroCount > this.oneCount ? 0 : 1;
}
public getLeastCommonBit(): number {
if (this.zeroCount === this.oneCount) {
return this.favoredBitValue;
}
return this.zeroCount < this.oneCount ? 0 : 1;
}
}
function loadBitCounter(
testableInputs: TestableInput[],
indexOfBitToCount: number,
favoredBitValue: 0 | 1
) {
const bitCounter = new BitCounter(favoredBitValue);
for (let input of testableInputs) {
bitCounter.record(Number(input.value[indexOfBitToCount]));
}
return bitCounter;
}
function findOxygenGeneratorRating(
rawInputs: string[],
bitLengthOfInput: number
) {
let testableInputs = rawInputs.map((i) => new TestableInput(i));
let currentTestingBitIndex = 0;
while (
testableInputs.length !== 1 &&
currentTestingBitIndex < bitLengthOfInput
) {
let expectedBitValue = loadBitCounter(
testableInputs,
currentTestingBitIndex,
1
).getMostCommonBit();
testableInputs.forEach((testableInput) =>
testableInput.test(currentTestingBitIndex, expectedBitValue)
);
// Inefficient; shouldn't have to iterate over this array twice.
// Could move the test into the filter callback, but that feels weird
// Probably should take a functional approach
// Another option would be to make TestableInput fluent
testableInputs = testableInputs.filter((i) => i.meetsCriteria);
currentTestingBitIndex++;
}
if (testableInputs.length !== 1) {
throw new Error("Was not able to discover a single rating value");
}
return testableInputs[0];
}
function findCo2ScrubberRating(rawInputs: string[], bitLengthOfInput: number) {
let testableInputs = rawInputs.map((i) => new TestableInput(i));
let currentTestingBitIndex = 0;
while (
testableInputs.length !== 1 &&
currentTestingBitIndex < bitLengthOfInput
) {
let expectedBitValue = loadBitCounter(
testableInputs,
currentTestingBitIndex,
0
).getLeastCommonBit();
testableInputs.forEach((testableInput) =>
testableInput.test(currentTestingBitIndex, expectedBitValue)
);
testableInputs = testableInputs.filter((i) => i.meetsCriteria);
currentTestingBitIndex++;
}
if (testableInputs.length !== 1) {
throw new Error("Was not able to discover a single rating value");
}
return testableInputs[0];
}
let rawInputs: string[];
const rawData = readFileSync("./day3inputs.txt", "utf8");
rawInputs = rawData.split("\r\n");
const bitLengthOfInput = rawInputs[0].length;
const binaryOxygenGeneratorRating = findOxygenGeneratorRating(
rawInputs,
bitLengthOfInput
);
const binaryCo2ScrubberRating = findCo2ScrubberRating(
rawInputs,
bitLengthOfInput
);
const oxygenGeneratorRating = parseInt(binaryOxygenGeneratorRating.value, 2);
const co2ScrubberRating = parseInt(binaryCo2ScrubberRating.value, 2);
console.log(binaryOxygenGeneratorRating.value);
console.log(oxygenGeneratorRating);
console.log(binaryCo2ScrubberRating.value);
console.log(co2ScrubberRating);
console.log(oxygenGeneratorRating * co2ScrubberRating);