aboutsummaryrefslogtreecommitdiff
path: root/src/day10pt1.rs
diff options
context:
space:
mode:
authorAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-15 00:57:58 +0100
committerAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-15 00:57:58 +0100
commitb987ec6481ea40e257dc81a8b3ebfc5f0483afe5 (patch)
tree230b8fdd76ae89dc8ab9142d68a5bb7047d9b143 /src/day10pt1.rs
parent261fccb18f48f5a058e8d5487cab252b4727ba36 (diff)
day10 & day11
Diffstat (limited to 'src/day10pt1.rs')
-rw-r--r--src/day10pt1.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/day10pt1.rs b/src/day10pt1.rs
new file mode 100644
index 0000000..52db522
--- /dev/null
+++ b/src/day10pt1.rs
@@ -0,0 +1,81 @@
+fn mogu_levo(c: char) -> bool {
+ return c == 'S' || c == 'L' || c == 'F' || c == '-';
+}
+
+fn mogu_desno(c: char) -> bool {
+ return c == 'S' || c == '7' || c == 'J' || c == '-';
+}
+
+fn mogu_gore(c: char) -> bool {
+ return c == 'S' || c == '7' || c == 'F' || c == '|';
+}
+
+fn mogu_dole(c: char) -> bool {
+ return c == 'S' || c == 'L' || c == 'J' || c == '|';
+}
+
+fn bfs(input: &Vec<&str>, was: &mut Vec<Vec<bool>>, x: i32, y: i32, s: u32) -> u32 {
+ if s > 2 && input[y as usize].chars().nth(x as usize).unwrap() == 'S' {
+ return s;
+ }
+
+ if was[y as usize][x as usize] {
+ return 0;
+ }
+
+ was[y as usize][x as usize] = true;
+
+ for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
+ let nx = x + dx;
+ let ny = y + dy;
+ if nx >= 0 && nx < input[y as usize].len() as i32 && ny >= 0 && ny < input.len() as i32 {
+ let t = input[y as usize].chars().nth(x as usize).unwrap();
+ let n = input[ny as usize].chars().nth(nx as usize).unwrap();
+
+ let ind: bool;
+ if dx == 1 {
+ ind = mogu_levo(t) && mogu_desno(n);
+ } else if dx == -1 {
+ ind = mogu_desno(t) && mogu_levo(n);
+ } else if dy == 1 {
+ ind = mogu_gore(t) && mogu_dole(n);
+ } else {
+ ind = mogu_dole(t) && mogu_gore(n);
+ }
+
+ if !ind {
+ continue;
+ }
+
+ let ret = bfs(input, was, nx, ny, s + 1);
+ if ret != 0 {
+ return ret;
+ }
+ }
+ }
+ return 0;
+}
+
+pub fn main() {
+ let txt = std::fs::read_to_string("./input/day10.txt").unwrap();
+ let mut input: Vec<_> = vec![];
+ for line in txt.lines() {
+ input.push(line);
+ }
+
+ let mut x = 0;
+ let mut y = 0;
+
+ for i in 0..input.len() {
+ for j in 0..input[i].len() {
+ if input[i].chars().nth(j).unwrap() == 'S' {
+ x = j as i32;
+ y = i as i32;
+ }
+ }
+ }
+
+ let mut was = vec![vec![false; input[0].len()]; input.len()];
+
+ println!("{}", (bfs(&input, &mut was, x, y, 0) + 1) / 2);
+}