Day 9: Mirage Maintenance
For part one, the problem seemed straight forward enough but I found the implementation a bit difficult and found it quite annoying, this may also be because I have been coding everyday on top of work and music, and am tired in general. That said I may have normalised my baseline because I feel like I've written some not-so-simple code and solved the problem in a reasonable amount of time.
There is room for improvement in my dealing with recursive flow and while loops, and how to debug them, as I often have issues with them and keep crashing the browser with infinite loops. I should probably just switch to running the code in Node.js but it's nice to be here within the blog already.
I will have to come back to part two if there is time, because I didn't get to it this weekend.
Came back to part two a week later it's done!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  const inputToUse = realInput;
  const inputLines = inputToUse.split(/\n/);
  const histories = [];
  inputLines.forEach(function(line){
    const trimmedLine = line.trim();
    const history = trimmedLine.split(' ');
    histories.push(history);
  });
  console.log('histories',histories);
  const allPredictions = [];
  const allHistPredictions = [];
  histories.forEach(function(history,i){
    history.forEach(function(entry,j){
      const numEntry = parseFloat(entry);
      histories[i][j] = numEntry;
    });
    const differenceBreakdowns = [history];
    var buildingBreakdowns = true;
    var depth = 0;
    while( buildingBreakdowns ) {
      const toBreakdown = differenceBreakdowns[depth];
      const count = toBreakdown.length;
      for( var i=0; i<count-1; i++ ) {
        const thisEntry = differenceBreakdowns[depth][i];
        // console.log('thisEntry',thisEntry);
        const nextEntry = differenceBreakdowns[depth][i+1];
        // console.log('nextEntry',nextEntry); 
        const difference = nextEntry - thisEntry;
        // console.log('difference',difference);
        if( !differenceBreakdowns[depth+1] ) {
          differenceBreakdowns[depth+1] = [];
        }
        differenceBreakdowns[depth+1].push(difference);
      }
      const lastBreakdown = differenceBreakdowns[depth+1];
      // const lastBreakdown = [0,0,0,0,0];
      // console.log('lastBreakdown',lastBreakdown);
      const brokeDownToZero = lastBreakdown.every(value => value === 0);
      if( brokeDownToZero ) {
        // console.log('broke down to zero');
        buildingBreakdowns = false;
      } else {
        depth = depth + 1;
      }
    }
    // console.log('difference breakdowns',differenceBreakdowns);
    // ! make predictions
    const breakdownCount = differenceBreakdowns.length;
    console.log('breakdownCount',breakdownCount);
    for( var i=breakdownCount-1; i>-1; i-- ) {
      var nextBreakdownLastEntry = 0;
      var nextBreakdownFirstEntry = 0;
      const breakdown = differenceBreakdowns[i];
      // if( i == breakdownCount-1 ) {
      //   prediction = 0;
      // } else {
      const thisBreakdownLength = breakdown.length;
      const lastEntry = breakdown[thisBreakdownLength-1];
      const firstEntry = breakdown[0];
      // console.log('lastEntry',lastEntry);
      
      if( differenceBreakdowns[i+1] ) {
        const nextBreakdown = differenceBreakdowns[i+1];
        const nextBreakdownLength = nextBreakdown.length;
        nextBreakdownLastEntry = nextBreakdown[nextBreakdownLength-1];
        nextBreakdownFirstEntry = nextBreakdown[0];
      }
      // console.log('nextBreakdownLastEntry',nextBreakdownLastEntry);
      const prediction = lastEntry + nextBreakdownLastEntry;
      const histPrediction = firstEntry - nextBreakdownFirstEntry;
      // }
      differenceBreakdowns[i].push(prediction);
      differenceBreakdowns[i].unshift(histPrediction);
      // console.log('differenceBreakdowns',differenceBreakdowns);      
    }
    console.log('after prediction, differenceBreakdowns', differenceBreakdowns);
    const histWithPrediction = differenceBreakdowns[0];
    const histWPredLength = histWithPrediction.length;
    const finalPrediction = histWithPrediction[histWPredLength-1];
    const finalHistPrediction = histWithPrediction[0];
    
    allPredictions.push(finalPrediction);
    allHistPredictions.push(finalHistPrediction);
  });
  console.log('allPredictions',allPredictions);
  console.log('allHistPredictions',allHistPredictions);
  var predictionTotal = 0;
  allPredictions.forEach(function(prediction) {
    predictionTotal = predictionTotal + prediction;
  });
  console.log('predictionTotal',predictionTotal);
  var histPredictionTotal = 0;
  allHistPredictions.forEach(function(prediction) {
    histPredictionTotal = histPredictionTotal + prediction;
  });
  console.log('histPredictionTotal',histPredictionTotal);