Unable to load image

Advent of Code day 6

was preparing my butthole after the last one but ended up being the fastest one i've done :marseyexcited:

with open("day6input.txt") as f:
    input = f.read()

# part 1

for i in range(len(input)):
    marker = set(input[i:i+4])
    if len(marker) == 4:
        answer = i + 4
        break

print(answer)

# part 2 

for i in range(len(input)):
    marker = set(input[i:i+14])
    if len(marker) == 14:
        answer = i + 14
        break

print(answer)
28
Jump in the discussion.

No email address required.

My brute force solution

#include <iostream>

#define PREAMBLE_WINDOW_SIZE 4
#define MESSAGE_WINDOW_SIZE 14

inline bool contains_duplicates(const std::string& window)
{
  // O(n²) meh
  for(size_t i = 0; i < window.length(); i++)
    for(size_t j = 0; j < window.length(); j++)
      if(i != j and window[i] == window[j])
        return 1;
  return 0;
}

int main()
{
  std::string line;
  std::getline(std::cin, line);
  size_t i = 0;

  for(; i < line.length() - PREAMBLE_WINDOW_SIZE; i++)
  {
    if(not contains_duplicates(line.substr(i, PREAMBLE_WINDOW_SIZE)))
    {
      // PART A
      //std::cout << (i + PREAMBLE_WINDOW_SIZE) << std::endl;
      // return 0;
      break;
    }
  }

  for(; i < line.length() - MESSAGE_WINDOW_SIZE; i++)
    if(not contains_duplicates(line.substr(i, MESSAGE_WINDOW_SIZE)))
    {
      // PART B
      std::cout << (i + MESSAGE_WINDOW_SIZE) << std::endl;
      return 0;
    }

  return -1;
}

Discuss

Jump in the discussion.

No email address required.

based and sexy Indian dude pilled

trans lives matter

Jump in the discussion.

No email address required.

iostream :!marseyyikes:

Jump in the discussion.

No email address required.

Hi @JollyMoon,

Your comment has been automatically removed because you forgot to include trans lives matter.

Don't worry, we're here to help! We won't let you post or comment anything that doesn't express your love and acceptance towards the trans community. Feel free to resubmit your comment with trans lives matter included.

This is an automated message; if you need help, you can message us here.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.