~saurabhs/aoc21

352390f07ec269e4e6069cb770201bd48c207701 — Saurabh S 9 months ago 2bfb35b mister
Add day6
2 files changed, 65 insertions(+), 0 deletions(-)

A day6/input
A day6/main.cpp
A day6/input => day6/input +2 -0
@@ 0,0 1,2 @@
3,3,5,1,1,3,4,2,3,4,3,1,1,3,3,1,5,4,4,1,4,1,1,1,3,3,2,3,3,4,2,5,1,4,1,2,2,4,2,5,1,2,2,1,1,1,1,4,5,4,3,1,4,4,4,5,1,1,4,3,4,2,1,1,1,1,5,2,1,4,2,4,2,5,5,5,3,3,5,4,5,1,1,5,5,5,2,1,3,1,1,2,2,2,2,1,1,2,1,5,1,2,1,2,5,5,2,1,1,4,2,1,4,2,1,1,1,4,2,5,1,5,1,1,3,1,4,3,1,3,2,1,3,1,4,1,2,1,5,1,2,1,4,4,1,3,1,1,1,1,1,5,2,1,5,5,5,3,3,1,2,4,3,2,2,2,2,2,4,3,4,4,4,1,2,2,3,1,1,4,1,1,1,2,1,4,2,1,2,1,1,2,1,5,1,1,3,1,4,3,2,1,1,1,5,4,1,2,5,2,2,1,1,1,1,2,3,3,2,5,1,2,1,2,3,4,3,2,1,1,2,4,3,3,1,1,2,5,1,3,3,4,2,3,1,2,1,4,3,2,2,1,1,2,1,4,2,4,1,4,1,4,4,1,4,4,5,4,1,1,1,3,1,1,1,4,3,5,1,1,1,3,4,1,1,4,3,1,4,1,1,5,1,2,2,5,5,2,1,5


A day6/main.cpp => day6/main.cpp +63 -0
@@ 0,0 1,63 @@
#include <fstream>
#include <iostream>
#include <sstream>

typedef unsigned long long int ULLI;

ULLI cnt_process_lifetime(std::string input, int days) {
  std::string buffer;
  std::stringstream ss(input);
  ULLI count[9] = {0};
  while (getline(ss, buffer, ',')) {
    int num = std::stoi(buffer);
    count[num]++;
  }

  int weeks = days / 7;
  while(weeks-- > 0) {
    ULLI add_zero = 0, add_one = 0;
    if(count[7]) {
      add_zero = count[7];
      count[7] = 0;
    }
    if(count[8]) {
      add_one = count[8];
      count[8] = 0;
    }

    for(int i = 6; i >= 0; i--) {
      if(count[i]) {
        count[i + 2] += count[i];
      } 
    }

    count[0] += add_zero;
    count[1] += add_one;
  }

  ULLI result = 0;
  for(const auto& c : count) result += c;

  int days_left = days % 7;
  if(days_left == 0) return result;

  for(int i = 0; i < days_left; i++) result += count[i];  
  return result;
}

ULLI prob1(int days) {
  std::ifstream fs;
  fs.open("input");
  if (!fs.is_open()) {
    return -1;
  }
  std::string input;
  getline(fs, input);
  fs.close();
  return cnt_process_lifetime(input, days);
}

int main() { 
  std::cout << "prob1 " << prob1(80) << "\n"; 
  std::cout << "prob2 " << prob1(256) << "\n"; 
}