From 5f92ea475dadc173b2172834227f9392fd7d5a7c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 10 Jan 2025 19:34:39 +0100 Subject: aoc 2016, day 1 --- 2016/data/.gitkeep | 0 2016/src/day01.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 7 ++- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 2016/data/.gitkeep create mode 100644 2016/src/day01.cpp diff --git a/2016/data/.gitkeep b/2016/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/2016/src/day01.cpp b/2016/src/day01.cpp new file mode 100644 index 0000000..f5d74a5 --- /dev/null +++ b/2016/src/day01.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +vector +split(string_view line, string_view delimiter) +{ + size_t pos_start = 0; + size_t pos_end = 0; + + vector res; + + while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) { + auto token = line.substr(pos_start, pos_end - pos_start); + pos_start = pos_end + delimiter.length(); + + res.emplace_back(token); + } + + if ( pos_start != line.size() ) { + res.emplace_back(line.substr(pos_start)); + } + return res; +} + +vector> +read_file(string_view filename) +{ + fstream input{ filename }; + string content{ istreambuf_iterator{ input }, istreambuf_iterator{} }; + + vector> result; + for ( const auto& part: split(content, ", ") ) { + result.emplace_back(part[0], stol(part.substr(1))); + } + return result; +} + +void +part1(const vector>& input) +{ + array, 4> dirs = { + make_tuple(1L, 0L), + make_tuple(0L, 1L), + make_tuple(-1L, 0L), + make_tuple(0L, -1L) + }; + + size_t dir = 0; + long pos_x = 0; + long pos_y = 0; + + for ( const auto& [change, length]: input ) { + if ( change == 'L' ) { + dir = (dir + 1) % 4; + } + else { + dir = (dir + 3) % 4; + } + + auto [delta_x, delta_y] = dirs.at(dir); + + pos_x += length * delta_x; + pos_y += length * delta_y; + } + + cout << abs(pos_x) + abs(pos_y) << endl; +} + +void +part2(const vector>& input) +{ + array, 4> dirs = { + make_tuple(1L, 0L), + make_tuple(0L, 1L), + make_tuple(-1L, 0L), + make_tuple(0L, -1L) + }; + + size_t dir = 0; + long pos_x = 0; + long pos_y = 0; + + set> visited; + + for ( auto [change, length]: input ) { + if ( change == 'L' ) { + dir = (dir + 1) % 4; + } + else { + dir = (dir + 3) % 4; + } + + auto [delta_x, delta_y] = dirs.at(dir); + + while ( length-- > 0 ) { + pos_x += delta_x; + pos_y += delta_y; + + if ( visited.contains({ pos_x, pos_y }) ) { + cout << abs(pos_x) + abs(pos_y) << endl; + return; + } + + visited.emplace(pos_x, pos_y); + } + } +} + +int +main() +{ + auto input = read_file("data/day01.txt"); + part1(input); + part2(input); +} diff --git a/makefile b/makefile index 928c1bb..5ce2c9a 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,7 @@ include config.mk all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \ + $(patsubst 2016/src/%.cpp,2016/bin/%,$(wildcard 2016/src/*.cpp)) \ $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \ $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \ $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp)) \ @@ -16,6 +17,10 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \ 2015/bin/day04: 2015/src/day04.cpp | 2015/bin c++ $(CPPFLAGS) $^ -lmd -o $@ +# 2016 +2016/bin/%: 2016/src/%.cpp | 2016/bin + c++ $(CPPFLAGS) $^ -o $@ + # 2020 2020/bin/%: 2020/src/%.cpp | 2020/bin c++ $(CPPFLAGS) $^ -o $@ @@ -39,6 +44,6 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \ c++ $(CPPFLAGS) $^ -L/usr/local/lib -lz3 -o $@ clean: - rm -rf 2015/bin 2020/bin 2022/bin 2023/bin 2024/bin + rm -rf 2015/bin 2016/bin 2020/bin 2022/bin 2023/bin 2024/bin .PHONY: all clean -- cgit v1.3