diff options
| author | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-16 06:55:42 +0100 |
|---|---|---|
| committer | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-16 06:55:42 +0100 |
| commit | 457c2af984ac34e6615a8b49b953c4499809255c (patch) | |
| tree | 1cbfbb97d9ec829b7924a20c2d3ab22f8209cb15 | |
| parent | b2c6978311010cd522d2680e7078e496fab28744 (diff) | |
day16
| -rw-r--r-- | input/day16.txt | 110 | ||||
| -rw-r--r-- | src/day12pt1.rs | 13 | ||||
| -rw-r--r-- | src/day12pt2.rs | 3 | ||||
| -rw-r--r-- | src/day16pt1.rs | 65 | ||||
| -rw-r--r-- | src/day16pt2.rs | 87 | ||||
| -rw-r--r-- | src/main.rs | 8 |
6 files changed, 282 insertions, 4 deletions
diff --git a/input/day16.txt b/input/day16.txt new file mode 100644 index 0000000..81b3afc --- /dev/null +++ b/input/day16.txt @@ -0,0 +1,110 @@ +\..\.|.........|..|................................................................./.-............./-....../. +............../..--.../...................-....|.......................\../...|..-....\...-|...-...........\.. +.......\......................................|..-\.................-..-...................................... +........\..............|......................\........./..............................|..............||...... +.........|........|......-...........|....../....-.../.....|/.........-..../.../..........................|... +..\............................./\............................................................................ +........|....................\............................./.....|\|..-....../............/..-..........\..... +...\.........................\.............|.........../..............-..../.............../-................. +.||....../..............\.|...............-......................|-..-......../.|............................. +...|.|.....-...../.............-............./............\.\/........|.............................\|.-..\.-. +............./....................|.......|.....\..\..............................-.../....|.................. +./......./.|...............\................................................||................................ +......\......|.................|........../......|\|.......-|..................|.....-.-.||../.\.............. +................../...............|.....\........-...-.\...||..\...........\............|.........|........... +...-........|\|........././/............|...........\.../\...|.......-........\....................-.......... +..../...........\.........................../......-.|...................................../..\........|...... +\...............\/......./........-..............|..............--........../....|..-.\...........-........./. +..........\........\|.........|\.........../........................../.........|............\..........|..... +...........\....|/.|..........-..............-....-|...\..........-.../............./......................... +..\..\......../...\\....................../...../.........................|.....\..............|..\...-....... +...........................-./...................................-/.\.....\...................\.../........... +..-..................|..................-...\.......|../............./|-............/........|.......|..-..... +...........-......................-.................|............../.......-./..........|....../.............. +.........|.............././...............|..../..............|.........|.....|..\......................|..... +......./-/...................\\..\......|........................\...\....../.....................|...-....... +.....|.......................|............|...|........|...........-....../..................-...-............ +.../................../......|...........................-............|.............-........./............|.. +.-.........|.............|-...../..............................\................|........................../.. +.........../.........-.......|..-.........................-...............-...-.../...................../..... +../.......-../....................-...\...\........................-.............-...\...../.|.........../.... +............-...............|........................................../...\.|....|........-.....\|........... +.....................|\............................-..-....../..-|..../..................\................\... +.\.................-......\...|............................./......./.........-............../................ +.\.........././............../|......\/..............\........................|........................|...-.. +.............../.........|.................................-.......\\.\-..........-......................./-.. +-.|......................|.............|............................/....................|...........-........ +../.....-.......|.....-.....\.../\........|.............|.........\./..//\................|.........-.....||.. +.\..\.......|......./............\..\.........../........-.../.|..............-..../....\.........../.\.\..... +.............|..........|.........|.|...\-......................-..............|.../............./.........|.. +....|................................................../...........-......-/......................-.....-..... +............................./.........-............................./..................\..........\..../..... +.....|.....................-.........../.......|..................-.......\...|\...|......\.....\...|......... +........|............../..../|.|......-................|...../..........|................|...................- +....../.....//..........\.-|......|.....|..................../..............|................................. +\.-...-....\..................\....................|............||............................../............. +............................-|...-...........-.../......-.........\..-...|.|\..........-...............\.|//./ +....../......././.|...................|\........../.\.../-...\..........\.-..|\../...........\....\..|.....\.. +...............\.../..../.................\.......-...........\..|..................\\................./.\.... +............-..........-......................|.......|..|....................-......|......\...........|..... +...............\......|.-............./............\.../.............-|........./...............\|\.....|..... +.\...............|.................|...........\......\................\............\.|...\......./........... +\...|..............-..........|...................|....-.....\......../..........................\........|..| +.........|........./.........|....-........-...................../|./.--.......\........................-..../ +..........................-.....-...............-/............................--...../........................ +.....................-.\../\..........|.....................\.......\.../..........|.............\............ +......................../..\.......\...............................................\./........................ +....|......\..|\.-........../......................\|........\...............\.................|.............. +............................./.....|..|............-...\.........-...\....|......\.............-..|........... +..................-......-.....................-...........-......\...-|-.-................\............|/.... +...|....../............../..\.......................-....\...|.....\..|..\......./.................-.....-.... +.........-........|........../..-................................./....-.................-.................... +.............................-.......\...............\.....|......-....-.../.................\................ +...../......|...................|..............-........./.............\...\....|.............\.......|....... +.......|............./........./......\..../........|...........\................................-............ +/...|././.......|-.|..........|................................/\.\.......-......-........-........\.......... +../................./|.-...\........................|........|........|...\|.\........................./.....- +................-.........../|..............|.|/...|\...........-.......................-..|../............... +./..||........|....../........................|.................................../..............\..|./-...... +/..-...../......................................\-...........-..................\|................../......... +.............|.-......................|................-../.................-............../..............-... +-.....-|-...............\..........-...........\.......\........................\.........../..../...../...... +.-..................../..........|.--.............................-..-....|.\................|....|...\......\ +......./.-........................................\....|........|...../...........|...\|..-................... +\...............|........|.......\|..-..|...-..|-........|........./.\........................................ +/.|./....|....-...../\..|...............................-.../........./.............-|.........-..../.-..\.... +../......./...../..|........|../.....\....\.............-.-.................-...\.......|.\...........\....... +...-........./.............../..................\.|................/..//..................|.......-..\......-. +./.............../.-..\..........|.............-....................-....-..|...........-..................... +...........|-..|........................................./\............./-.../.................\........\..... +................................/...........................\.....................|.|......................... +.........|............\........................................-...../..........|........../.../......\...\... +../.........|......./..................|........\.../.\.-...-......../../\......../................../........ +.-........./.........|....|.|.............................................|.\............/../................. +./.........................................\....../................../.--.....-..../.....|./.-......|.......\. +.\..................../.-........|.........\............\...........\...............|-../................\.... +........................|......./.-....-....\.|...................-......-..........|........\.........../...| +.............-......./.../......................\.......\............/......................................|. +............-..........................-..................-.......\--......-.....-.......|..\..-....|........\ +...............|.......\......|....................\.....................-.../.|......|................../.... +---..\-.../.........//....\../....|........\....|-.........-.....|..........-|..............|...../..........| +......-.............................-.........................-.........../...../.|.../................-.....| +.........-.........\..............|....\..............-......\......../......\.......-\........-.............\ +.................../......................|......|.....\..........................-..-......./....-.....-..... +...\.....................-..-....\.......././....\......./...........................\..............\\........ +.................|................./.....................\...../.\.................\.........|...|....|....... +./............-........................\..|...-............................./............./..\................ +-............-..............|.................-..............................-..............................\. +................................/.....\........|....-.....||............/.|.............-..|....|....-........ +/.........|............-./...|././..............\./-............/......../\.\..././../..\..\..............-... +..../.....................\.........|......-...........|...\/..........//\.......//....-...|/.....\..\........ +../............................/...././...-....-............./.|..\..............|...../.........-.....\....|. +.................................\..../...-.....-.....-....|..\.\...../....../.............../.\.............. +..................|..|.......-......\.../....../....................................................|.......-. +-./.....|...\.....\../.......\..........................\.....|.............|...................|.\.../.../\.. +..................-.....\.............|...|.........-..-............................../............/..|....... +.....................\..../.....|......................................\.\............|/....................\. +.....-..\.........-....\..............-..........................\.....\....................\.........\.....-. +\...\|\...../.\............................\........-.........................../.......-............\........ +.............................................-......\.............|..../.......|......../..................... +/.........-............./......|................-........-.............|...-..../.....\.\....\................ diff --git a/src/day12pt1.rs b/src/day12pt1.rs new file mode 100644 index 0000000..bdf4027 --- /dev/null +++ b/src/day12pt1.rs @@ -0,0 +1,13 @@ +pub fn main() { + let txt = std::fs::read_to_string("./input/day12.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.split(" ").collect::<Vec<_>>()) + .collect::<Vec<Vec<_>>>(); + + for line in input { + let s = line[0]; + let n = line[1].split(",").map(|s| s.parse::<u64>().unwrap()).collect::<Vec<_>>(); + println!("{} {:?}", s, n); + } +} diff --git a/src/day12pt2.rs b/src/day12pt2.rs new file mode 100644 index 0000000..bb98e7f --- /dev/null +++ b/src/day12pt2.rs @@ -0,0 +1,3 @@ +pub fn main() { + +} diff --git a/src/day16pt1.rs b/src/day16pt1.rs new file mode 100644 index 0000000..5377074 --- /dev/null +++ b/src/day16pt1.rs @@ -0,0 +1,65 @@ +use std::collections::HashMap; + +fn bfs( + input: &Vec<Vec<char>>, + was: &mut Vec<Vec<bool>>, + x: i32, + y: i32, + dx: i32, + dy: i32, + comb: &mut HashMap<(i32, i32, i32, i32), i32>, +) { + if x < 0 + || x >= input[0].len() as i32 + || y < 0 + || y >= input.len() as i32 + || comb.contains_key(&(x, y, dx, dy)) + { + return; + } + + let c = input[y as usize][x as usize]; + was[y as usize][x as usize] = true; + comb.insert((x, y, dx, dy), 1); + + if c == '/' { + bfs(input, was, x - dy, y - dx, -dy, -dx, comb); + } else if c == '\\' { + bfs(input, was, x + dy, y + dx, dy, dx, comb); + } else if c == '|' { + if dy != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x, y - 1, 0, -1, comb); + bfs(input, was, x, y + 1, 0, 1, comb); + } + } else if c == '-' { + if dx != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x - 1, y, -1, 0, comb); + bfs(input, was, x + 1, y, 1, 0, comb); + } + } else if c == '.' { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } +} + +pub fn main() { + let txt = std::fs::read_to_string("./input/day16.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<Vec<char>>>(); + + let mut was = vec![vec![false; input[0].len()]; input.len()]; + let mut comb = HashMap::new(); + + bfs(&input, &mut was, 0, 0, 1, 0, &mut comb); + + let mut sum = 0; + for i in was { + sum += i.iter().filter(|&n| *n == true).count(); + } + println!("{}", sum); +} diff --git a/src/day16pt2.rs b/src/day16pt2.rs new file mode 100644 index 0000000..9e2f094 --- /dev/null +++ b/src/day16pt2.rs @@ -0,0 +1,87 @@ +use std::collections::HashMap; + +fn bfs( + input: &Vec<Vec<char>>, + was: &mut Vec<Vec<bool>>, + x: i32, + y: i32, + dx: i32, + dy: i32, + comb: &mut HashMap<(i32, i32, i32, i32), i32>, +) { + if x < 0 + || x >= input[0].len() as i32 + || y < 0 + || y >= input.len() as i32 + || comb.contains_key(&(x, y, dx, dy)) + { + return; + } + + let c = input[y as usize][x as usize]; + was[y as usize][x as usize] = true; + comb.insert((x, y, dx, dy), 1); + + if c == '/' { + bfs(input, was, x - dy, y - dx, -dy, -dx, comb); + } else if c == '\\' { + bfs(input, was, x + dy, y + dx, dy, dx, comb); + } else if c == '|' { + if dy != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x, y - 1, 0, -1, comb); + bfs(input, was, x, y + 1, 0, 1, comb); + } + } else if c == '-' { + if dx != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x - 1, y, -1, 0, comb); + bfs(input, was, x + 1, y, 1, 0, comb); + } + } else if c == '.' { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } +} + +fn try_comb(input: &Vec<Vec<char>>, x: i32, y: i32, dx: i32, dy: i32) -> usize { + let mut was = vec![vec![false; input[0].len()]; input.len()]; + let mut comb = HashMap::new(); + + bfs(&input, &mut was, x, y, dx, dy, &mut comb); + + let mut sum = 0; + for i in was { + sum += i.iter().filter(|&n| *n == true).count(); + } + + return sum; +} + +pub fn main() { + let txt = std::fs::read_to_string("./input/day16.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<Vec<char>>>(); + + let mut m = 0; + for i in 0..input.len() { + m = usize::max(m, try_comb(&input, 0, i as i32, 1, 0)); + m = usize::max( + m, + try_comb(&input, (input[0].len() - 1) as i32, i as i32, -1, 0), + ); + } + + for i in 0..input[0].len() { + m = usize::max(m, try_comb(&input, i as i32, 0, 0, 1)); + m = usize::max( + m, + try_comb(&input, i as i32, (input.len() - 1) as i32, 0, -1), + ); + } + + println!("{}", m); +} diff --git a/src/main.rs b/src/main.rs index f3c0d0b..3c60b82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -mod day15pt1; -mod day15pt2; +mod day16pt1; +mod day16pt2; fn main() { - day15pt1::main(); - day15pt2::main(); + day16pt1::main(); + day16pt2::main(); } |
