diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/day10pt1.rs | 81 | ||||
| -rw-r--r-- | src/day10pt2.rs | 149 | ||||
| -rw-r--r-- | src/day11pt1.rs | 45 | ||||
| -rw-r--r-- | src/day11pt2.rs | 45 | ||||
| -rw-r--r-- | src/main.rs | 8 |
5 files changed, 324 insertions, 4 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); +} diff --git a/src/day10pt2.rs b/src/day10pt2.rs new file mode 100644 index 0000000..c05402f --- /dev/null +++ b/src/day10pt2.rs @@ -0,0 +1,149 @@ +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<String>, 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); + // was[ny as usize][nx as usize] = false; + + if ret != 0 { + return ret; + } + } + } + return 0; +} + +fn set_s(input: &mut Vec<String>, xs: i32, ys: i32) { + let levi = mogu_levo(input[ys as usize].chars().nth(xs as usize - 1).unwrap()); + let gornji = mogu_gore(input[ys as usize - 1].chars().nth(xs as usize).unwrap()); + let donji = mogu_dole(input[ys as usize + 1].chars().nth(xs as usize).unwrap()); + let desni = mogu_desno(input[ys as usize].chars().nth(xs as usize + 1).unwrap()); + + if donji && gornji { + input[ys as usize] = input[ys as usize].replace("S", "|"); + } else if donji && levi { + input[ys as usize] = input[ys as usize].replace("S", "7"); + } else if donji && desni { + input[ys as usize] = input[ys as usize].replace("S", "F"); + } else if gornji && levi { + input[ys as usize] = input[ys as usize].replace("S", "J"); + } else if gornji && desni { + input[ys as usize] = input[ys as usize].replace("S", "L"); + } else if levi && desni { + input[ys as usize] = input[ys as usize].replace("S", "-"); + } else { + panic!(); + } +} + +fn part2(input: &Vec<String>, was: Vec<Vec<bool>>) -> u32 { + let mut s: u32 = 0; + let mut en: bool; + let mut lc: char; + + for i in 0..input.len() { + en = false; + lc = 'S'; + + for j in 0..input[i].len() { + let c = input[i].chars().nth(j).unwrap(); + + if !was[i][j] { + s += en as u32; + continue; + } + + if c == '-' { + continue; + } else if c == '|' { + en = !en; + continue; + } + + if lc == 'S' { + lc = c; + if lc == 'J' || lc == '7' { + panic!(); + } + } else { + if (lc == 'F' && c == 'J') || (lc == 'L' && c == '7') { + en = !en; + } + lc = 'S'; + } + } + } + + return s; +} + +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.to_string()); + } + + let mut xs = 0; + let mut ys = 0; + + for i in 0..input.len() { + for j in 0..input[i].len() { + if input[i].chars().nth(j).unwrap() == 'S' { + xs = j as i32; + ys = i as i32; + } + } + } + + let mut was = vec![vec![false; input[0].len()]; input.len()]; + bfs(&input, &mut was, xs, ys, 0); + set_s(&mut input, xs, ys); + + println!("{}", part2(&input, was)); +} diff --git a/src/day11pt1.rs b/src/day11pt1.rs new file mode 100644 index 0000000..5f1eb67 --- /dev/null +++ b/src/day11pt1.rs @@ -0,0 +1,45 @@ +pub fn main() { + let txt = std::fs::read_to_string("./input/day11.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<_>>(); + + let mut coords = vec![]; + + let mut off_y = 0; + for i in 0..input.len() { + if !input[i].contains(&'#') { + off_y += 1; + continue; + } + + let mut off_x = 0; + for j in 0..input[i].len() { + if !input + .clone() + .into_iter() + .map(|s| s[j]) + .collect::<Vec<char>>() + .contains(&'#') + { + off_x += 1; + continue; + } + + if input[i][j] == '#' { + coords.push(((j + off_x) as isize, (i + off_y) as isize)); + } + } + } + + let mut sum: u64 = 0; + for coord1 in &coords { + for coord2 in &coords { + if coord1 != coord2 { + sum += ((coord1.0 - coord2.0).abs() + (coord1.1 - coord2.1).abs()) as u64; + } + } + } + println!("{}", sum / 2); +} diff --git a/src/day11pt2.rs b/src/day11pt2.rs new file mode 100644 index 0000000..87d80b7 --- /dev/null +++ b/src/day11pt2.rs @@ -0,0 +1,45 @@ +pub fn main() { + let txt = std::fs::read_to_string("./input/day11.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<_>>(); + + let mut coords = vec![]; + + let mut off_y = 0; + for i in 0..input.len() { + if !input[i].contains(&'#') { + off_y += 1000000 - 1; + continue; + } + + let mut off_x = 0; + for j in 0..input[i].len() { + if !input + .clone() + .into_iter() + .map(|s| s[j]) + .collect::<Vec<char>>() + .contains(&'#') + { + off_x += 1000000 - 1; + continue; + } + + if input[i][j] == '#' { + coords.push(((j + off_x) as isize, (i + off_y) as isize)); + } + } + } + + let mut sum: u64 = 0; + for coord1 in &coords { + for coord2 in &coords { + if coord1 != coord2 { + sum += ((coord1.0 - coord2.0).abs() + (coord1.1 - coord2.1).abs()) as u64; + } + } + } + println!("{}", sum / 2); +} diff --git a/src/main.rs b/src/main.rs index 3f23276..11d3ec1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -mod day9pt1; -mod day9pt2; +mod day11pt1; +mod day11pt2; fn main() { - day9pt1::main(); - day9pt2::main(); + day11pt1::main(); + day11pt2::main(); } |
