summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-15/main.c
blob: c7e11576348a44a726a583066ff45b62a68051d9 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

#if 0
  #define N 1
#else
  #define N 5
#endif


// part 2 doesnt work with input for some reason bruh
#if 1
  #define FILE_P "sample.txt"
  #define T_CAP 10 * N
  #define T_CAP_S 10
#else
  #define FILE_P "input.txt"
  #define T_CAP 100 * N
  #define T_CAP_S 100
#endif

int table[T_CAP][T_CAP];

void ParseInput(char *filepath)
{
  char ch;
  FILE *fp;
  fp = fopen(filepath, "r");

  if(fp == NULL)
  {
    fprintf(stderr, "ERROR: something with file idk fuck you");
    exit(EXIT_FAILURE);
  }

  int i = 0;
  int j = 0;
  while((ch = fgetc(fp)) != EOF)
  {
    if(ch == '\n') { i += 1; j = 0; continue; }

    table[i][j] = (ch - '0');
    j += 1;
  }
  
  fclose(fp);
}

void FindRisk()
{
  // dont add to total risk
  table[0][0] = 0;
  
  for(int i=1; i<T_CAP; i++) table[i][0] += table[i-1][0];
  for(int j=1; j<T_CAP; j++) table[0][j] += table[0][j-1];

  for(int i=1; i<T_CAP; i++)
  {
    for(int j=1; j<T_CAP; j++)
    {
      table[i][j] += min(table[i][j-1], table[i-1][j]);
    }
  }

}

void Part1()
{
  ParseInput(FILE_P);
  FindRisk();
  printf("PART1: %d\n", table[T_CAP-1][T_CAP-1]); 
}

void ReconstructMap()
{
  for(int j=0; j<T_CAP_S; j++)
    for(int n=T_CAP_S; n<T_CAP; n+=T_CAP_S)
      for(int i=0; i<T_CAP_S; i++)
      {
	if(table[i+n-T_CAP_S][j] != 9)
	  table[i+n][j] = table[i+n-T_CAP_S][j] + 1;
	else
	  table[i+n][j] = 1;
      }

  for(int i=0; i<T_CAP; i++)
    for(int n=T_CAP_S; n<T_CAP; n+=T_CAP_S)
      for(int j=0; j<T_CAP_S; j++)
      {
	if(table[i][j+n-T_CAP_S] != 9)
	  table[i][j+n] = table[i][j+n-T_CAP_S] + 1;
	else
	  table[i][j+n] = 1;
      }
}

void Part2()
{
  ParseInput(FILE_P);
  ReconstructMap();
  FindRisk();
  printf("PART2: %d\n", table[T_CAP-1][T_CAP-1]);
}

void PrintTable()
{
  for(int i=0; i<T_CAP; i++)
  {
    for(int j=0; j<T_CAP; j++)
    {
      printf("%d", table[i][j]);
    }
    printf("\n");
  }
}

int main(void)
{
  memset(table, 0, sizeof(table));
  //  Part1();
  Part2();
 
  return 0;
}