- commit
- 5cea89071e5b5f2baa882c61af47b48874c1b539
- parent
- e6a6b5e9b9860736af6280316ab81ad095c51f95
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2022-12-23 09:18
2022-12-23
Diffstat
A | 2022/23/input.txt | 73 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2022/23/solution.rs | 132 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2022/23/test.txt | 7 | +++++++ |
3 files changed, 212 insertions, 0 deletions
diff --git a/2022/23/input.txt b/2022/23/input.txt
@@ -0,0 +1,73 @@ -1 1 #.##.##...####..#.##.##....###....##...#..#####..#.##.##.#...###.##..#.## -1 2 .#....#..#.##..#.#.#...#..#..####..#.#.##.#.#..#.#..#.#..##.#..........#. -1 3 ##.#####.###.#...######..###.#.#..##.#.##.#......#..##.....###.#.##.##.## -1 4 ..#######.####.#..###...####..####....#.####.#.#..#.#.###.##.###.##..#... -1 5 .#..#.##...#...####.....##.#.#..#...###.....##..#.##..#.#.#.##.##.#.#.#.. -1 6 ####.#...#.#..##..###.####.#######.....#.##.#..#.#...#.##.#....#......... -1 7 ..#.#.####...###.##.#..#.....#.##...#.#.####.....#..##....#..#####...##.. -1 8 #####..##.....#.##..#.####....#.#....#.##....#.#....##.#####.#...####..## -1 9 #.#..#####.###..####..#.##..#######....##..#.###.#.#....##.#..##.##..#..# -1 10 ..##.#.##.....###..#..#..#.####....#..####..##.###...#..#..#...##..##..## -1 11 #.###.##.#...##.##.#.##..####.##.#.#.###...##.#....#.#.#.##.##...#.##.#.# -1 12 .#####..###.#.###.###....#..#..#....#.##.#.###.#.####..###.#####.#.#####. -1 13 ...#..##.##.#.#.###.#######.##.#...##..##...#......#..####.#.#...#.#####. -1 14 .#..###..######..#.#.##.##.#.##....##.#...#...#..#.#.##.#...#.#..#...#.## -1 15 ##..#.#...#.###.#..#..#.###.###..###.#.......#####.##.####..#...#..#....# -1 16 ...#..###...#...###.#.....#.###.##.###.####.##.....##..##.#.##.#.#...#.## -1 17 ####.#......#..###..#.#...##.#.##..#...#.###...#..##.#..#.....####.##.### -1 18 ##....#..#####..#####....##.#####.##.###..#.#####..###.##..#.#..###.#.### -1 19 ##.##...##.###.#.#..#####...#..#...#....#..#.#.#.......##..#...#..######. -1 20 ......#.###.#.##..####..#..##..##.###.#..##...#...#...#...#.###.#.#.##.## -1 21 ....##..#...##.##.#.#.#..#..#.#...####.#.....#.#.#.##.#.....##.#...#.#.## -1 22 ..#..###.#..###.####..#.#....#..##.#.##..###...#.##.#.####.##.#.#.#.#...# -1 23 ...###.....####.######...####.....#.#.#.#...#..##.##...#.###..###.#.##### -1 24 #.##..#.##.#......#####..#...####.#...###...###.##...##.##...###.###.##.# -1 25 ###....#..#.###..#.####.##.#...##..#..#..#....###...#......#..#..##.#.### -1 26 .####.#.###..#########...#.##############.##...#.##.##.#..#.#.##.#.#.###. -1 27 .#.##...#...###..#..#.#.#..#.#.#.#.##........#.##.#.#.##.##.#.#.##.###### -1 28 ##.#.#.###..###.###.#.#.....#.###..####.#.#..##.#.##..#.####..#.......##. -1 29 .#.#####.#..###..##.#..#...#.#.##..#..#..#..##.####.##.#..###.#......#... -1 30 .##..##..#..#.#..###...#.#.##.###..#..#...#.##.#.##..#.#.##..#....#...#.. -1 31 ..#......###..###..##...##.###..##.#.....#.###...##.##..#...#..#.#..#.... -1 32 .#.#.#.#.#...###.#######..#..###...#.###.#.###.##....#...#.##......####.. -1 33 #...#.##.#...#.#.#.###.#..#.#..##.###.######.#.#....###.##...#....#....#. -1 34 .###....#..####.#..#....##.#..##..#..#.#####.#.#...##.##...####.###.##..# -1 35 #...#.#...#...##.#####.#.#...##..#..##.#...#...###....##.######.#..#.##.# -1 36 #...##...##..#......####.#.#.##.##.#.......####...####...#..#..#.#..#.##. -1 37 .###....#.####..##.###..#....#...##.###..#...#.##.#...#####.###..#..#..## -1 38 ##.#..##..###.#..#..####...#.##.##..#......####....#....#.###..##......## -1 39 ##.##....#####.##.###.######..#..####..##.#.####.#..####.##...##....##### -1 40 .##.#..##..#.##..###.....#..#.##..####...#.##...#.#.#.#..##.###.....##... -1 41 .##.#...#...####..##.#....##...#..###.#.#..#....##.######.#...##.###..##. -1 42 ##.##.###..#.##.#..#....#.##.#.##..#...#..##.#.####..##.##...#..##.##..## -1 43 .#..###.#.#.##..#..#.####.#.##.#..##.#....#.###.##..#.###...#..#....###.. -1 44 ####..#.#..###..#...##.##.###.##...#.##.##...##..#.##.##...#.#..#.#.#.#.. -1 45 ..###...#..#...........#.###.##..#..###..##.#.##.#...#.#..#.....##..##### -1 46 .....##....###..###.###..#..#...##..#......#.##..#.#.#..##..#.###.....#.# -1 47 ..####.#......#...#.#.#.#..#..##....##..#.#...##.##.##.###.###...###...#. -1 48 ##..#.#.#.##.###########.#.###....####.##.#.#...####..#.##.#...##...#.... -1 49 ##...#..#...####.#.####....#.########.######..###....###..##..#...#.###.. -1 50 ##.###.####..#.##.#.#####.##.###..#.#.#.##.##.##.####.##.#...#..#..##.... -1 51 .#..#.#.##..##...#....#.#....#....###..#..##..###.##.####.##.####.##.#... -1 52 ##.#.##.##...#.#..#.#...##...#...#..#......#.###......###.....#.#.#####.# -1 53 ..#.##.##.###....#.#..###..#..##.####.###.##..##.##....#...##.#..#..##.## -1 54 #####.#...#..##..##.#.#.....##..#..##.##.##.####..#####.#..###..#.##..##. -1 55 ####.##..........###.###.#####.#.#..##...##.....#..###..##..#.####..#.##. -1 56 #.....##.####...#..##.##..#..#....##..#...#..#.#.....#..#...#.###.##.#..# -1 57 #.#.##..###.#...##.#...###.#......#....##..##.##.###.#.#.#..#.##.####..## -1 58 ##..###.#.#......##.##.##.#.#####.#.#.###...###.##.#......#.##########..# -1 59 .####....#..#...###.#..##.#.####..#.#..######.#.####..##....###..##..#..# -1 60 #..##.#.#...##.#.#.########.....#.##...#....#.####.###.########.#.##.#... -1 61 #.#######..#.##.#.#.#..####..##.#..###.####.#..#########....####....###.. -1 62 ..##.#...##...#..###.#..##.#..#...##.####.######.####.#..##.#...#.#..###. -1 63 .#..##.#.##..#......##.#.#.##.#####.#.###....#...###...#....#.###.....### -1 64 #......#.#..##....##.########.#..#..##.##......#.#..######..##.##..##.### -1 65 ..##...#...#.#..#.#########..##..#####..#..#..##.#....##...#.#....##.#.#. -1 66 .##..###.####.#..#####.###..###..#......##...#.....####..###..#..##.#.##. -1 67 ......###.#..#...#..#.#.##.#.###...##.#.#....####.#..#.######.#.#...####. -1 68 ...#.##.####....##..##.#.###.###.....#..###...####..##..######.#..##..... -1 69 .....####.....###.#.#.......#####.#.#....##...#####..#..#.#..#.#.#..##.## -1 70 ##..##.#..##..#..#.#####..##.####.#...#.#..#..###..#..#....#...#.###.##.. -1 71 #.#..#.#########.####..#####..####.#.#####....#.#..##...#..####.#..###.#. -1 72 #..##.###.##..#....#...#..#.##..#..###..#####..#######.#####....#.##..... -1 73 ####.####.########.#.###....#.#..#..#.#.#.#..#....#..#.....#..##..#.##.#.
diff --git a/2022/23/solution.rs b/2022/23/solution.rs
@@ -0,0 +1,132 @@ -1 1 #[path = "../lib.rs"] mod lib; -1 2 -1 3 enum Dir { -1 4 North, -1 5 East, -1 6 South, -1 7 West, -1 8 } -1 9 -1 10 const DIRS: [Dir; 4] = [Dir::North, Dir::South, Dir::West, Dir::East]; -1 11 -1 12 -1 13 fn get_input() -> Vec<(i64, i64)> { -1 14 let mut elves = vec![]; -1 15 for (y, line) in lib::iter_input().enumerate() { -1 16 for (x, c) in line.chars().enumerate() { -1 17 if c == '#' { -1 18 elves.push((x as i64, y as i64)); -1 19 } -1 20 } -1 21 } -1 22 return elves; -1 23 } -1 24 -1 25 fn render(elves: &Vec<(i64, i64)>) { -1 26 let x1 = elves.iter().map(|(x, _)| *x).min().unwrap(); -1 27 let y1 = elves.iter().map(|(_, y)| *y).min().unwrap(); -1 28 let x2 = elves.iter().map(|(x, _)| *x).max().unwrap(); -1 29 let y2 = elves.iter().map(|(_, y)| *y).max().unwrap(); -1 30 -1 31 for y in y1..=y2 { -1 32 for x in x1..=x2 { -1 33 if elves.contains(&(x, y)) { -1 34 print!("#"); -1 35 } else { -1 36 print!("."); -1 37 } -1 38 } -1 39 print!("\n"); -1 40 } -1 41 print!("\n"); -1 42 } -1 43 -1 44 fn propose_move(elves: &Vec<(i64, i64)>, x: i64, y: i64, offset: usize) -> (i64, i64) { -1 45 if !elves.iter().any(|(x2, y2)| (*x2, *y2) != (x, y) && (*x2 - x).abs() <= 1 && (*y2 - y).abs() <= 1) { -1 46 return (x, y); -1 47 } -1 48 -1 49 for dir_i in 0..4 { -1 50 match DIRS[(dir_i + offset) % 4] { -1 51 Dir::North => { -1 52 if !elves.iter().any(|(x2, y2)| *y2 == y - 1 && (*x2 - x).abs() <= 1) { -1 53 return (x, y - 1); -1 54 } -1 55 }, -1 56 Dir::East => { -1 57 if !elves.iter().any(|(x2, y2)| *x2 == x + 1 && (*y2 - y).abs() <= 1) { -1 58 return (x + 1, y); -1 59 } -1 60 }, -1 61 Dir::South => { -1 62 if !elves.iter().any(|(x2, y2)| *y2 == y + 1 && (*x2 - x).abs() <= 1) { -1 63 return (x, y + 1); -1 64 } -1 65 }, -1 66 Dir::West => { -1 67 if !elves.iter().any(|(x2, y2)| *x2 == x - 1 && (*y2 - y).abs() <= 1) { -1 68 return (x - 1, y); -1 69 } -1 70 }, -1 71 } -1 72 } -1 73 return (x, y); -1 74 } -1 75 -1 76 fn do_round(elves: &Vec<(i64, i64)>, offset: usize) -> (Vec<(i64, i64)>, bool) { -1 77 let mut tmp = vec![]; -1 78 let mut changed = false; -1 79 -1 80 for (x, y) in elves.iter() { -1 81 let proposed = propose_move(elves, *x, *y, offset); -1 82 tmp.push(proposed); -1 83 changed |= proposed != (*x, *y); -1 84 } -1 85 -1 86 let mut result = vec![]; -1 87 -1 88 for i in 0..elves.len() { -1 89 let (x, y) = tmp[i]; -1 90 if tmp.iter().filter(|(x2, y2)| *x2 == x && *y2 == y).count() == 1 { -1 91 result.push((x, y)); -1 92 } else { -1 93 result.push(elves[i]); -1 94 } -1 95 } -1 96 -1 97 // println!("==== Round {} ====", offset); -1 98 // render(&result); -1 99 -1 100 return (result, changed); -1 101 } -1 102 -1 103 fn part1(init: &Vec<(i64, i64)>, rounds: usize) -> i64 { -1 104 let mut elves = do_round(init, 0).0; -1 105 for i in 1..rounds { -1 106 elves = do_round(&elves, i).0; -1 107 } -1 108 -1 109 let x1 = elves.iter().map(|(x, _)| x).min().unwrap(); -1 110 let y1 = elves.iter().map(|(_, y)| y).min().unwrap(); -1 111 let x2 = elves.iter().map(|(x, _)| x).max().unwrap(); -1 112 let y2 = elves.iter().map(|(_, y)| y).max().unwrap(); -1 113 -1 114 return (x2 + 1 - x1) * (y2 + 1 - y1) - elves.len() as i64; -1 115 } -1 116 -1 117 fn part2(init: &Vec<(i64, i64)>) -> usize { -1 118 let mut state = do_round(init, 0); -1 119 for i in 1.. { -1 120 state = do_round(&state.0, i); -1 121 if !state.1 { -1 122 return i + 1; -1 123 } -1 124 } -1 125 unreachable!(); -1 126 } -1 127 -1 128 fn main() { -1 129 let elves = get_input(); -1 130 println!("part1: {}", part1(&elves, 10)); -1 131 println!("part2: {}", part2(&elves)); -1 132 }
diff --git a/2022/23/test.txt b/2022/23/test.txt
@@ -0,0 +1,7 @@ -1 1 ....#.. -1 2 ..###.# -1 3 #...#.# -1 4 .#...## -1 5 #.###.. -1 6 ##.#.## -1 7 .#..#..