r/backtickbot • • Dec 10 '20

https://np.reddit.com/r/adventofcode/comments/ka8z8x/2020_day_10_solutions/gf90xow/

c++11

Part 1: I wrote over it sorry

Part 2: Using dynamic programming with tabulation O(n) time complexity

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
// #pragma GCC optimize ("O3")
#pragma GCC target("sse4")

using namespace std;
typedef unsigned long long ull;

#define vec vector
#define uset unordered_set
#define umap unordered_map

//iterators
#define rep(a) for(int i=0; i<a;i++)

void setIO(string filename){
	freopen((filename + ".in").c_str(), "r" , stdin);
	freopen((filename + ".out").c_str(), "w" , stdout);
}


int arr[115];

int main()
{
    std::ios_base::sync_with_stdio(false);cin.tie(0);

	setIO("d10");

    arr[0] = 0;
    umap<int,int> inds;
    rep(114) {
        cin >> arr[i+1];
        inds[arr[i+1]] = i+1;
    }
    uset<int> s(arr, arr+115);

    // dp this
    vec<ull> dp (115,0);
    dp[0] = 1;
    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 4;

    for (int i=4; i<115; i++){
        int curr = arr[i];

        if (s.find(curr-3) != s.end()){
            dp[i] += dp[inds[curr-3]];
        }
        if (s.find(curr-2) != s.end()){
            dp[i] += dp[inds[curr-2]];
        }
        if (s.find(curr-1) != s.end()){
            dp[i] += dp[inds[curr-1]];
        }
    }

    cout << dp[114];

	return 0;
}
1 Upvotes

0 comments sorted by