From 40547cf5f8cacbdbf882f10dad8cce83a218220a Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 25 Dec 2019 17:18:44 +0100 Subject: [PATCH] Brute-force solution to day 22 part 1. --- 2019/src/day22.cpp | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/2019/src/day22.cpp b/2019/src/day22.cpp index 72c71c5..d58b110 100644 --- a/2019/src/day22.cpp +++ b/2019/src/day22.cpp @@ -1,31 +1,48 @@ #include +#include #include "days.hpp" #include "utils.hpp" void aoc2019::day22_part1(std::istream &input, std::ostream &output) { - std::int64_t increment = 1; - std::int64_t offset = 0; + constexpr int DECK_SIZE = 10007; - constexpr std::int64_t DECK_SIZE = 10007; + std::vector deck(DECK_SIZE); + std::iota(deck.begin(), deck.end(), 0); + + auto copy = deck; std::string buffer; while (std::getline(input, buffer)) { std::string_view line = buffer; if (!line.find("deal into new stack")) { - increment *= -1; + std::reverse(deck.begin(), deck.end()); } else if (!line.find("deal with increment ")) { - std::int64_t new_increment; + int new_increment; from_chars(line.substr(20), new_increment); - increment *= new_increment; - increment %= DECK_SIZE; + int pos = 0; + for (auto value : deck) { + copy[pos] = value; + pos = (pos + new_increment) % DECK_SIZE; + } + std::swap(deck, copy); } else { - std::int64_t new_offset; - from_chars(line, new_offset); - offset += increment * new_offset; - offset %= DECK_SIZE; + // cut + int new_offset; + from_chars(line.substr(4), new_offset); + if (new_offset < 0) { + new_offset = DECK_SIZE + new_offset; + } + + auto it = std::copy(deck.begin() + new_offset, deck.end(), copy.begin()); + std::copy(deck.begin(), deck.begin() + new_offset, it); + + std::swap(deck, copy); } } - output << "Not implemented\n"; + + auto location = std::find(deck.begin(), deck.end(), 2019); + + output << std::distance(deck.begin(), location) << std::endl; } void aoc2019::day22_part2(std::istream &input, std::ostream &output) {