Added day 3.
This commit is contained in:
parent
216eff5113
commit
4020feb344
|
@ -3,7 +3,8 @@ import std.stdio;
|
|||
import day1.part1;
|
||||
import day1.part2;
|
||||
import day2.part1and2;
|
||||
import day3.part1;
|
||||
|
||||
void main() {
|
||||
dive2();
|
||||
binaryDiagnostic2();
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,88 @@
|
|||
module day3.part1;
|
||||
|
||||
import std.file;
|
||||
import std.stdio;
|
||||
import std.bitmanip;
|
||||
|
||||
void writeBinary(uint n) {
|
||||
for (int i = 31; i >= 0; i--) {
|
||||
uint b = (n & (1 << i)) > 0 ? 1 : 0;
|
||||
writef("%d ", b);
|
||||
}
|
||||
writeln();
|
||||
}
|
||||
|
||||
void binaryDiagnostic() {
|
||||
auto f = File("source/day3/input.txt", "r");
|
||||
int[ubyte] frequencies;
|
||||
int count = 0;
|
||||
foreach (line; f.byLine) {
|
||||
count++;
|
||||
ubyte idx = 0;
|
||||
foreach (c; line) {
|
||||
if (idx !in frequencies) frequencies[idx] = 0;
|
||||
if (c == '1') frequencies[idx]++;
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
ulong bitCount = frequencies.length;
|
||||
uint gamma = 0;
|
||||
uint epsilon = 0;
|
||||
foreach (idx, freq; frequencies) {
|
||||
if (freq > count / 2) {
|
||||
gamma |= (1 << (bitCount - 1 - idx));
|
||||
} else {
|
||||
epsilon |= (1 << (bitCount - 1 - idx));
|
||||
}
|
||||
}
|
||||
writefln("Gamma: %d, Epsilon: %d, Power: %d", gamma, epsilon, gamma * epsilon);
|
||||
}
|
||||
|
||||
bool is1Common(uint[] values, int idx, int bitCount, bool type) pure {
|
||||
int count = 0;
|
||||
foreach (v; values) {
|
||||
if ((v & (1 << (bitCount - 1 - idx))) > 0) count++;
|
||||
}
|
||||
if (type) {
|
||||
return count >= (values.length - count);
|
||||
} else {
|
||||
return count < (values.length - count);
|
||||
}
|
||||
}
|
||||
|
||||
void filter(ref uint[] values, int idx, int bitCount, bool type) pure {
|
||||
bool common = is1Common(values, idx, bitCount, type);
|
||||
uint[] newValues = [];
|
||||
foreach (v; values) {
|
||||
bool b = (v & (1 << (bitCount - 1 - idx))) > 0;
|
||||
if (b == common) newValues ~= v;
|
||||
}
|
||||
values = newValues;
|
||||
}
|
||||
|
||||
uint findValue(uint[] values, int bitCount, bool type) {
|
||||
int idx = 0;
|
||||
while (values.length > 1) {
|
||||
filter(values, idx++, bitCount, type);
|
||||
}
|
||||
return values[0];
|
||||
}
|
||||
|
||||
void binaryDiagnostic2() {
|
||||
auto f = File("source/day3/input.txt", "r");
|
||||
uint[] values = [];
|
||||
int bitCount = 0;
|
||||
foreach (line; f.byLine) {
|
||||
bitCount = cast(int) line.length;
|
||||
int idx = 0;
|
||||
uint value = 0;
|
||||
foreach (c; line) {
|
||||
if (c == '1') value |= (1 << line.length - 1 - idx);
|
||||
idx++;
|
||||
}
|
||||
values ~= value;
|
||||
}
|
||||
uint o2 = findValue(values, bitCount, true);
|
||||
uint co2 = findValue(values, bitCount, false);
|
||||
writefln("Oxygen: %d, CO2: %d, LS Rating: %d", o2, co2, o2 * co2);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
Loading…
Reference in New Issue