Browse Source

Add day 3

master
mid-kid 3 years ago
parent
commit
c2fa60db6f
  1. 21
      Makefile
  2. 3
      d01.cc
  3. 3
      d02.cc
  4. 89
      d03.cc
  5. 1000
      d03_input.txt
  6. 2
      d03_output.txt

21
Makefile

@ -1 +1,22 @@
CXXFLAGS := -std=c++20 -O2 -Wall -Wextra CXXFLAGS := -std=c++20 -O2 -Wall -Wextra
sources := $(wildcard *.cc)
progs := $(patsubst %.cc,%,$(sources))
.PHONY: all
all: $(progs)
.PHONY: clean
clean:
rm -f $(progs) $(progs:=_test.txt)
.PHONY: test
test: $(addprefix test-,$(progs))
.PHONY: test-%
test-%: %_test.txt
@test -f $*_output.txt && (diff -u $*_output.txt $<; rm -f $<)
@test -f $*_output.txt || cat $<
$(progs:=_test.txt): %_test.txt: %
./$< $*_input.txt > $@

3
d01.cc

@ -3,7 +3,8 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
if (argc <= 1) return 1; if (argc <= 1) return 1;
vector<unsigned> numbers; vector<unsigned> numbers;

3
d02.cc

@ -14,7 +14,8 @@ struct commands {
int pos; int pos;
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
if (argc <= 1) return 1; if (argc <= 1) return 1;
vector<struct commands> commands; vector<struct commands> commands;

89
d03.cc

@ -0,0 +1,89 @@
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
auto parse_input(char *file)
{
ifstream input(file);
if (!input.is_open()) exit(1);
vector<string> parsed;
for (string line; getline(input, line);) {
parsed.push_back(line);
}
return parsed;
}
// Count the occurences of each bit position
vector<int> count_occurences(vector<string> input)
{
vector<int> counts(input[0].length(), 0);
for (string line : input) {
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == '1') counts[i]++;
if (line[i] == '0') counts[i]--;
}
}
return counts;
}
unsigned part1(auto input)
{
vector<int> counts = count_occurences(input);
// Convert the counts array into a binary value
unsigned out = 0;
for (int i : counts) {
out <<= 1;
if (i > 0) out += 1;
}
return out * (out ^ ((1 << counts.size()) - 1));
}
unsigned part2(auto input)
{
size_t pos;
vector<string> numbers_ox = input;
pos = 0;
while (numbers_ox.size() > 1 && pos < input[0].length()) {
int count = count_occurences(numbers_ox)[pos];
char bit = (count >= 0) + '0';
for (size_t i = 0; i < numbers_ox.size(); i++) {
if (numbers_ox[i][pos] != bit) {
numbers_ox.erase(numbers_ox.begin()+i);
i--;
}
}
pos++;
}
unsigned ox = stol(numbers_ox[0], NULL, 2);
vector<string> numbers_co = input;
pos = 0;
while (numbers_co.size() > 1 && pos < input[0].length()) {
int count = count_occurences(numbers_co)[pos];
char bit = (count < 0) + '0';
for (size_t i = 0; i < numbers_co.size(); i++) {
if (numbers_co[i][pos] != bit) {
numbers_co.erase(numbers_co.begin()+i);
i--;
}
}
pos++;
}
unsigned co = stol(numbers_co[0], NULL, 2);
return ox * co;
}
int main(int argc, char *argv[])
{
if (argc <= 1) return 1;
auto input = parse_input(argv[1]);
cout << part1(input) << endl;
cout << part2(input) << endl;
}

1000
d03_input.txt

File diff suppressed because it is too large

2
d03_output.txt

@ -0,0 +1,2 @@
2595824
2135254
Loading…
Cancel
Save