summary refs log tree commit diff
path: root/15/src/main.rs.bak
blob: 33f76e366b4af65ce599db8b624dbc725c4e4d5a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use advent_lib::prelude::*;


fn main() -> Result<()> {
  let mut args = std::env::args();
  if args.len() != 2 {
    eprintln!("Usage: advent input");
  }
  let _ = args.next();
  let filename = args.next().unwrap();

  let input = advent_lib::read_lines_file(&filename)?;

  let mut value_grid = Vec::new();
  let mut risk_grid: Vec<Vec<Option<i64>>> = Vec::new();
  for line in &input{
    let mut value_row = Vec::new();
    let mut risk_row = Vec::new();
    for c in line.chars() {
      let value = c.to_digit(10).unwrap() as i64;
      value_row.push(value);
      risk_row.push(None);
    }
    value_grid.push(value_row);
    risk_grid.push(risk_row);
  }

  let height = value_grid.len();
  let width = value_grid[0].len();

  /*
  risk_grid[0][0] = Some(0);
  find_path(0, 0, &value_grid, &mut risk_grid, false);
  let risk = risk_grid[height - 1][width - 1].unwrap();
  println!("{}", risk);
  */

  let mut big_risk_grid = Vec::new();
  for _ in 0 .. height * 5 {
    let mut big_risk_grid_row = Vec::new();
    for _ in 0 .. width * 5 {
      big_risk_grid_row.push(None);
    }
    big_risk_grid.push(big_risk_grid_row);
  }
  big_risk_grid[0][0] = Some(0);
  find_path(0, 0, &value_grid, &mut big_risk_grid, true);
  let big_risk = big_risk_grid[height * 5 - 1][width * 5 - 1].unwrap();
  println!("{}", big_risk);

  Ok(())
}


#[allow(dead_code)]
fn debug_risk_grid(risk_grid: &Vec<Vec<Option<i64>>>) {
  for row in risk_grid {
    for cell in row {
      match cell {
        None => { print!(" ."); }
        Some(n) => { print!(" {}", n); }
      }
    }
    println!("");
  }
  println!("");
}


fn find_path(x: usize, y: usize, value_grid: &Vec<Vec<i64>>,
    risk_grid: &mut Vec<Vec<Option<i64>>>, repeat: bool)
{
  let mut height = value_grid.len();
  let mut width = value_grid[0].len();
  if repeat {
    width *= 5;
    height *= 5;
  }

  let mut exploration_stack = Vec::new();
  exploration_stack.push((x, y));

  let mut counter = 0;

  loop {
    let (x, y) = match exploration_stack.pop() {
      None => { return; }
      Some(value) => { value }
    };
    let risk_so_far = risk_grid[y][x].unwrap();
    counter += 1;
    if counter % 100000 == 0 {
      print!(" [{},{} {}]", x, y, risk_so_far);
    }

    if x > 0 {
      let value_left = get_value(x - 1, y, value_grid);
      let new_risk_left = risk_so_far + value_left;

      let risk_left = risk_grid[y][x-1];
      let should_move = match risk_left {
        None => true,
        Some(prior_best_risk) => {
          new_risk_left < prior_best_risk
        }
      };
      if should_move {
        risk_grid[y][x-1] = Some(new_risk_left);

        exploration_stack.push((x - 1, y));
      }
    }

    if y > 0 {
      let value_up = get_value(x, y - 1, value_grid);
      let new_risk_up = risk_so_far + value_up;

      let risk_up = risk_grid[y-1][x];
      let should_move = match risk_up {
        None => true,
        Some(prior_best_risk) => {
          new_risk_up < prior_best_risk
        }
      };
      if should_move {
        risk_grid[y-1][x] = Some(new_risk_up);

        exploration_stack.push((x, y - 1));
      }
    }

    if x + 1 < width {
      let value_right = get_value(x + 1, y, value_grid);
      let new_risk_right = risk_so_far + value_right;

      let risk_right = risk_grid[y][x+1];
      let should_move = match risk_right {
        None => true,
        Some(prior_best_risk) => {
          new_risk_right < prior_best_risk
        }
      };
      if should_move {
        risk_grid[y][x+1] = Some(new_risk_right);

        exploration_stack.push((x + 1, y));
      }
    }

    if y + 1 < height {
      let value_down = get_value(x, y + 1, value_grid);
      let new_risk_down = risk_so_far + value_down;

      let risk_down = risk_grid[y+1][x];
      let should_move = match risk_down {
        None => true,
        Some(prior_best_risk) => {
          new_risk_down < prior_best_risk
        }
      };
      if should_move {
        risk_grid[y+1][x] = Some(new_risk_down);

        exploration_stack.push((x, y + 1));
      }
    }
  }
}


fn get_value(x: usize, y: usize, value_grid: &Vec<Vec<i64>>) -> i64 {
  let height = value_grid.len();
  let width = value_grid[0].len();

  let tile_x = x / width;
  let tile_y = y / height;
  let increment: i64 = (tile_x + tile_y) as i64;
  let raw_value = value_grid[y % height][x % width];
  (raw_value + increment - 1) % 9 + 1
}