From 9b6e79bc3b725f137c5df46d03816343f41bb128 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Mon, 11 Nov 2024 20:30:23 +0100 Subject: aoc 2015, day03 --- 2015/src/day03.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 2015/src/day03.cpp (limited to '2015') diff --git a/2015/src/day03.cpp b/2015/src/day03.cpp new file mode 100644 index 0000000..e81da90 --- /dev/null +++ b/2015/src/day03.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include + +using namespace std; + +auto +read_file(string_view filename) +{ + fstream input{ filename }; + return string{ istreambuf_iterator{ input }, {} }; +} + +void +part1(string_view directions) +{ + set> positions{}; + int xpos = 0; + int ypos = 0; + + positions.emplace(xpos, ypos); + for ( const auto direction: directions ) { + switch ( direction ) { + case '^': + --ypos; + break; + case 'v': + ++ypos; + break; + case '<': + --xpos; + break; + case '>': + ++xpos; + break; + default: + break; + } + positions.emplace(xpos, ypos); + } + cout << positions.size() << endl; +} + +void +part2(string_view directions) +{ + set> positions{}; + + for ( string_view::size_type start = 0; start != 2; ++start ) { + int xpos = 0; + int ypos = 0; + + positions.emplace(xpos, ypos); + + for ( string_view::size_type idx = start; idx < directions.length(); idx += 2 ) { + const auto direction = directions[idx]; + + switch ( direction ) { + case '^': + --ypos; + break; + case 'v': + ++ypos; + break; + case '<': + --xpos; + break; + case '>': + ++xpos; + break; + default: + break; + } + positions.emplace(xpos, ypos); + } + } + cout << positions.size() << endl; +} + +int +main() +{ + auto directions = read_file("data/day03.txt"); + // part1(">"); + // part1("^>v<"); + // part1("^v^v^v^v^v"); + part1(directions); + // part2("^v"); + // part2("^>v<"); + // part2("^v^v^v^v^v"); + part2(directions); +} -- cgit v1.3