master
Avril 4 years ago
parent 5997e898b1
commit b107f62c09
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,3 +1,17 @@
COMMON_OPT_FLAGS?= -O3 -march=native -pipe -flto \
-march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
-fno-stack-check -fno-strict-aliasing
C_OPT_FLAGS?=
CXX_OPT_FLAGS?= -felide-constructors
LD_OPT_FLAGS?=-O3 -flto
COMMON_FLAGS=-Wall -pedantic $(COMMON_OPT_FLAGS)
CFLAGS?=$(COMMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS)
CXXFLAGS?=$(COMMON_FLAGS) --std=gnu++20 $(CXX_OPT_FLAGS)
LDFLAGS?=$(LD_OPT_FLAGS)
DAYS= $(wildcard day*) DAYS= $(wildcard day*)

@ -0,0 +1,30 @@
COMMON_OPT_FLAGS?= -O3 -march=native -pipe -flto \
-march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
-fno-stack-check -fno-strict-aliasing
C_OPT_FLAGS?=
CXX_OPT_FLAGS?= -felide-constructors
LD_OPT_FLAGS?=-O3 -flto
COMMON_FLAGS=-Wall -pedantic $(COMMON_OPT_FLAGS)
CFLAGS?=$(COMMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS)
CXXFLAGS?=$(COMMON_FLAGS) --std=gnu++20 $(CXX_OPT_FLAGS)
LDFLAGS?=$(LD_OPT_FLAGS)
CXXFLAGS+= -fno-exceptions
.PHONY: all
all: part1 part2
part1: day3.cpp
$(CXX) $< $(CXXFLAGS) -o $@ $(LDFLAGS)
strip $@
part2: day3.cpp
$(CXX) $< -DPART2 $(CXXFLAGS) -o $@ $(LDFLAGS)
strip $@
clean:
rm -f part{1,2}

@ -0,0 +1,79 @@
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <tuple>
using mod_table = std::vector<std::vector<bool> >;
mod_table read_input(const char* _input)
{
std::ifstream input(_input);
if(!input) {
std::cerr << "Failed to open file: " << _input << std::endl;
std::terminate();
}
mod_table output;
std::string str;
while(input)
{
if(!std::getline(input, str)) break;
std::vector<bool> ln(str.size());
int i=0;
for(const auto& chr : str)
{
switch(chr)
{
case '#': ln[i] = true;
default: i++; break;
}
}
output.push_back(std::move(ln));
str.clear();
}
return output;
}
template<typename V>
inline std::size_t wrapping_index(const V& vec, std::size_t i)
{
return (i % vec.size());
}
std::size_t count_slope(const mod_table& table, std::size_t stepx, std::size_t stepy)
{
std::size_t x=0;
std::size_t r=0;
for(std::size_t y=0;y<table.size();y+=stepy, x+=stepx)
{
r += table[y][wrapping_index(table[y], x)] ? 1 : 0;
}
return r;
}
int main()
{
const mod_table table = read_input("input");
#ifdef PART2
const auto paths = {
std::tuple<std::size_t, std::size_t>(1, 1),
std::tuple<std::size_t, std::size_t>(3, 1),
std::tuple<std::size_t, std::size_t>(5, 1),
std::tuple<std::size_t, std::size_t>(7, 1),
std::tuple<std::size_t, std::size_t>(1, 2),
};
std::size_t r=1;
for(const auto& [x, y] : paths)
{
r*= count_slope(table, x, y);
}
#else
auto r = count_slope(table, 3, 1);
#endif
std::cout << r << std::endl;
return 0;
}
Loading…
Cancel
Save