Rust/2024/15: add solution
This commit is contained in:
parent
a491b68e3d
commit
f168432363
10 changed files with 267 additions and 3 deletions
|
@ -9,12 +9,12 @@
|
|||
|[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of ≥255548)|
|
||||
|[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of ≥197402)|
|
||||
|
||||
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 14/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 13/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 5/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 4/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|
||||
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 15/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 13/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 5/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 4/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
|||||||[**1**](https://adventofcode.com/2024/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2024/01.rs "Rust solution for 2024/01") [<img height=12 src=".assets/hs.svg">](Haskell/2024/01.hs "Haskell solution for 2024/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrCsOKKn-KNieKKnOKKnOKLleKIqeKJoEBcbixAIC4K4oipLyviioMo4pmtw5fiip7iirg9fOKMtS3iiKnijYYpCg== "Uiua solution for 2024/01") [<img height=12 src=".assets/nix.svg">](Nix/2024/01.nix "Nix solution for 2024/01") [<img height=12 src=".assets/rb.svg">](Ruby/2024/01.rb "Ruby solution for 2024/01")|
|
||||
|[**2**](https://adventofcode.com/2024/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2024/02.rs "Rust solution for 2024/02") [<img height=12 src=".assets/hs.svg">](Haskell/2024/02.hs "Haskell solution for 2024/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4pah4oqc4ouVKeKIqeKJoEBcbixAIC4KCkNoZWNrIOKGkCDDl-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKeKniC0K4oipLyviiaHil4fiioMoL-KGpeKJoShDaGVja-KWvSniip7iiaAu4oeh4qe74p-cwqR8Q2hlY2spCg== "Uiua solution for 2024/02") [<img height=12 src=".assets/rb.svg">](Ruby/2024/02.rb "Ruby solution for 2024/02")|[**3**](https://adventofcode.com/2024/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2024/03.rs "Rust solution for 2024/03") [<img height=12 src=".assets/hs.svg">](Haskell/2024/03.hs "Haskell solution for 2024/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03") [<img height=12 src=".assets/rb.svg">](Ruby/2024/03.rb "Ruby solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2024/04.rs "Rust solution for 2024/04") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgripLAg4oaQIOKIqeKKleKWoeKKmSzijZzima3iipviip7iioMtK-KIqeKHocKw4oqf4oq44pazCgrin5woLyviiaHil4coLyvijJUiWE1BUyIp4oqC4p-c4o2a4oeM4oqC4oqDKOKKguKksHziioLiiKniiaHilqHin5zijYkpKQovK-KZreKniCjDl-KIqSgv4oal4omh4omN4pahIk1BUyLiioLin5zijZrih4wp4qSwKTNfMwo= "Uiua solution for 2024/04") [<img height=12 src=".assets/rb.svg">](Ruby/2024/04.rb "Ruby solution for 2024/04")|[**5**](https://adventofcode.com/2024/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2024/05.rs "Rust solution for 2024/05") [<img height=12 src=".assets/hs.svg">](Haskell/2024/05.hs "Haskell solution for 2024/05") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriioPihpjihpniiqLiiprijJUiXG5cbiIuCuKKmSjiipziipzii5XiiKniiaBAXG4sQHwuKQriipwo4pah4oqc4ouVKeKIqeKJoEBcbixALC4KCk1pbiAgICDihpAg4oqi4pa9wqziirjiiIo64p-cKOKJoeKKo-KWveKIiuKKmeKKuOKJoeKKoikKUmVtb3ZlIOKGkCDiioLiioMo4oaZfOKGmCsxKeKKouKKmuKKuD0KU29ydCAgIOKGkCDiipnii4Xil4zijaIo4oqD4oqC4ouFUmVtb3Zl4oqZ4pehTWlufOKJoDDip7vil4wpW10KTWlkICAgIOKGkCDiio_ijIrDtzLiirjip7sKCuKIqSgvK-KJoeKXh01pZOKWvSkgwqwsLOKKuOKJoeKJjSDin5zijZpTb3J04oqZwqQK "Uiua solution for 2024/05") [<img height=12 src=".assets/rb.svg">](Ruby/2024/05.rb "Ruby solution for 2024/05")|[**6**](https://adventofcode.com/2024/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2024/06.rs "Rust solution for 2024/06") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgpJbml0ICDihpAgwq8xXzDiiqLiipo9QF4KU3RlcCAg4oaQIOKorCjin5wrfOKHjOKNnOKKosKvKT1AI-KXoSjirJpALuKKoSspClZhbGlkIOKGkCAvw5fDl-KIqT7CrzEu4oqZ4pazCgptYXAuW10g4oq4SW5pdAomcOKKuOKnuyDiipnii4Xil4zijaIo4oqDKGluc2VydOKKmTA64oqZ4peMfOKLhVN0ZXApfOKLheKLhVZhbGlkKQoKU2tpcEZvcndhcmQg4oaQIOKNoijin5wrfMOX4oqDKOKLhVZhbGlkfOKJoEAj4qyaQC7iiqErKSkKQ2hlY2tDeWNsZSDihpAgKAogIG1hcC5bXSDiirhJbml0CiAg4o2iKOKKgyhpbnNlcnTiipkwOuKKmeKKgnzih4zijZziiqLCr1NraXBGb3J3YXJk4peMKXzCrGhhc-KWoTriipniioIpCiAgVmFsaWTii4Xil4wKKQoK4pa9PUAu4peh4oqh4oqZ4peMwrBtYXAKJnAgLyviiaF3YWl04omhcG9vbChDaGVja0N5Y2xl4o2c4oqh4ouFQCMp4oqZwqQK "Uiua solution for 2024/06")|[**7**](https://adventofcode.com/2024/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2024/07.rs "Rust solution for 2024/07") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=IyBFeHBlcmltZW50YWwhCgomc2MKCuKKnCjiipnilqHCsOKKguKKnOKLlSniiaBAXG4swqziirjiiIoiOiAiCgpDaGVjayEg4oaQIHwyIOKNoygKICA94oqZwrDCpAp8IOKNoyjijaQuLkNoZWNrIV4w4o2c4oipKOKHjMKw4ouVKeKMneKKgl4wfOKNpC4uQ2hlY2shXjDijaQuPTDiioPil7_Dt3xDaGVjayFeMC0pIDriipnijZzih4zCsOKKgikKCuKIqSgvK8OXKeKJoSjin5w64p-c4oqDQ2hlY2sh4oiYQ2hlY2shKOKNpC4wKeKKmcKw4pahKQo= "Uiua solution for 2024/07")|[**8**](https://adventofcode.com/2024/day/8) [<img height=12 src=".assets/rs.svg">](Rust/2024/08.rs "Rust solution for 2024/08") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgpWYWxpZCDihpAgL8OXw5fiiKk-wq8xLgpXYWxrICDihpAg4oqZ4ouF4ouF4peM4o2iKOKKgyjiioLiipnil4x84ouF4p-cKyl84ouF4ouFVmFsaWQp4oavMC7iirgtCgriiaEo4pah4qeFPDLiipo9KeKKmcKk4pa94oq44omgQC7il7Tiirjima3in5wowqTCpOKWsykKCkEg4oaQIOKNmuKJoSjilqHilr064p-c4omhVmFsaWTiip_iiKkoK-KKuC0p4pehOsKw4oqfKQpCIOKGkCDijZriiaEo4pah4oqC4oipV2FsayDil6EoOuKKmeKKmeKImCnCsOKKnykKCuKIqSjip7vil7Qv4peH4oqC4o2aL-KXh-KKgikg4oqDQiBBCg== "Uiua solution for 2024/08")|
|
||||
|[**9**](https://adventofcode.com/2024/day/9) [<img height=12 src=".assets/rs.svg">](Rust/2024/09.rs "Rust solution for 2024/09")|[**10**](https://adventofcode.com/2024/day/10) [<img height=12 src=".assets/rs.svg">](Rust/2024/10.rs "Rust solution for 2024/10") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiaHii5XiirjiiaBAXG4KCk5laWdoIOKGkCDilr09KzHiirjiiqI64p-c4qyaMOKKoSviioIwW-KXoeKIqcKv4p-c4oeM4oehMl3CpAriiKkvK-KJoSjiiKnip7vin5zil7TiiaHil4fiiqPiipnil4xwYXRoKE5laWdofD054oqhKSniipo9MOKfnMKkCg== "Uiua solution for 2024/10")|[**11**](https://adventofcode.com/2024/day/11) [<img height=12 src=".assets/rs.svg">](Rust/2024/11.rs "Rust solution for 2024/11") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipzii5XiirjiiIorQDDih6ExMAoKTmV4dCAg4oaQIOKNoyjCpDHCsDB84omh4ouV4oavMl_iiJ7CsDDil78y4oq44qe7wrDii5V8wqTDlzIwMjQpCkJsaW5rIOKGkCDiiKco4oinKOKNnOKsmjBnZXQr4oqZOilOZXh0KcKwbWFw4oqZKG1hcC5bXSkKCi8r4o2lQmxpbms1MCDin5wvK-KNpUJsaW5rMjUgbWFw4oqV4oqD4oqi4qe74oq44oqbCg== "Uiua solution for 2024/11")|[**12**](https://adventofcode.com/2024/day/12) [<img height=12 src=".assets/rs.svg">](Rust/2024/12.rs "Rust solution for 2024/12") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiirjiiaBAXG4KCuKKnOKWoSsx4oqD4o2c4pmt4oqbKOKHoeKWsykKCkEg4oaQIC8r4omhKC8rwqziiIo6K-KKguKfnMKv4ouvMV8ywqQp4p-cwqQKQiDihpAgKAogIOKXtOKZreKCguKKnistMcOXMuKLr-KHoTTin5zCpMOXMgogIC8r4omhKOKGpeKKgyjil78yLyt8w5cyL8OX4qeI4omgKeKIijorLTHDlzLijZzihpjigoLih4zii6_ih6E0wqQpCikKCuKIqSgvK8OXKeKKmSziiaHil4fiioNCIEEg4p-c4omh4peH4qe7Cg== "Uiua solution for 2024/12")|[**13**](https://adventofcode.com/2024/day/13) [<img height=12 src=".assets/rs.svg">](Rust/2024/13.rs "Rust solution for 2024/13") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrihq_iiJ5fM18y4oqc4ouV4oq44oiKK0Aw4oehMTAKCivCpFsuMCAxMDAwMDAwMDAwMDAwMF0uCuKIqSgvK-KJoeKNoygvK8OXM18x4omhKMKvwrAw4oqDL-KXvy_Dt-KniCgvLcOX4oeMKSniip_in5zijZzihpnigoLih4x8MCkpCg== "Uiua solution for 2024/13")|[**14**](https://adventofcode.com/2024/day/14) [<img height=12 src=".assets/rs.svg">](Rust/2024/14.rs "Rust solution for 2024/14") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipzii5XiirjiiIriioJALStAMOKHoTEwCuKGmTLiioLiipkxMDFfMTAz4oqD4oaY4oaZwq_ijZwo4pe_NCnii4Uw4oq44qe7CuKGr-KInl8yXzI6CgpTaW0g4oaQIOKXvzorw5fiipkoOsKw4oqf4qS4wq8yfMKkKQoKQSDihpAgL8OX4oqV4qe7LuKKm-KWveKJoDAvw5fiirjijYnCsS0rMcOXMuKKmcKk4oq4U2ltMTAwCkIg4oaQIOKKouKKmj3iirgv4oan4oy1L-KEguKNieKNnOKMhSjDt-KKg-Knuy8rKSjCsOKImi3CpCku4qS4MVNpbeKHoS_Dl-KkmeKIqcKkCgriioNCIEEK "Uiua solution for 2024/14")|[**15**](https://adventofcode.com/2024/day/15)|
|
||||
|[**9**](https://adventofcode.com/2024/day/9) [<img height=12 src=".assets/rs.svg">](Rust/2024/09.rs "Rust solution for 2024/09")|[**10**](https://adventofcode.com/2024/day/10) [<img height=12 src=".assets/rs.svg">](Rust/2024/10.rs "Rust solution for 2024/10") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiaHii5XiirjiiaBAXG4KCk5laWdoIOKGkCDilr09KzHiirjiiqI64p-c4qyaMOKKoSviioIwW-KXoeKIqcKv4p-c4oeM4oehMl3CpAriiKkvK-KJoSjiiKnip7vin5zil7TiiaHil4fiiqPiipnil4xwYXRoKE5laWdofD054oqhKSniipo9MOKfnMKkCg== "Uiua solution for 2024/10")|[**11**](https://adventofcode.com/2024/day/11) [<img height=12 src=".assets/rs.svg">](Rust/2024/11.rs "Rust solution for 2024/11") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipzii5XiirjiiIorQDDih6ExMAoKTmV4dCAg4oaQIOKNoyjCpDHCsDB84omh4ouV4oavMl_iiJ7CsDDil78y4oq44qe7wrDii5V8wqTDlzIwMjQpCkJsaW5rIOKGkCDiiKco4oinKOKNnOKsmjBnZXQr4oqZOilOZXh0KcKwbWFw4oqZKG1hcC5bXSkKCi8r4o2lQmxpbms1MCDin5wvK-KNpUJsaW5rMjUgbWFw4oqV4oqD4oqi4qe74oq44oqbCg== "Uiua solution for 2024/11")|[**12**](https://adventofcode.com/2024/day/12) [<img height=12 src=".assets/rs.svg">](Rust/2024/12.rs "Rust solution for 2024/12") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiirjiiaBAXG4KCuKKnOKWoSsx4oqD4o2c4pmt4oqbKOKHoeKWsykKCkEg4oaQIC8r4omhKC8rwqziiIo6K-KKguKfnMKv4ouvMV8ywqQp4p-cwqQKQiDihpAgKAogIOKXtOKZreKCguKKnistMcOXMuKLr-KHoTTin5zCpMOXMgogIC8r4omhKOKGpeKKgyjil78yLyt8w5cyL8OX4qeI4omgKeKIijorLTHDlzLijZzihpjigoLih4zii6_ih6E0wqQpCikKCuKIqSgvK8OXKeKKmSziiaHil4fiioNCIEEg4p-c4omh4peH4qe7Cg== "Uiua solution for 2024/12")|[**13**](https://adventofcode.com/2024/day/13) [<img height=12 src=".assets/rs.svg">](Rust/2024/13.rs "Rust solution for 2024/13") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrihq_iiJ5fM18y4oqc4ouV4oq44oiKK0Aw4oehMTAKCivCpFsuMCAxMDAwMDAwMDAwMDAwMF0uCuKIqSgvK-KJoeKNoygvK8OXM18x4omhKMKvwrAw4oqDL-KXvy_Dt-KniCgvLcOX4oeMKSniip_in5zijZzihpnigoLih4x8MCkpCg== "Uiua solution for 2024/13")|[**14**](https://adventofcode.com/2024/day/14) [<img height=12 src=".assets/rs.svg">](Rust/2024/14.rs "Rust solution for 2024/14") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipzii5XiirjiiIriioJALStAMOKHoTEwCuKGmTLiioLiipkxMDFfMTAz4oqD4oaY4oaZwq_ijZwo4pe_NCnii4Uw4oq44qe7CuKGr-KInl8yXzI6CgpTaW0g4oaQIOKXvzorw5fiipkoOsKw4oqf4qS4wq8yfMKkKQoKQSDihpAgL8OX4oqV4qe7LuKKm-KWveKJoDAvw5fiirjijYnCsS0rMcOXMuKKmcKk4oq4U2ltMTAwCkIg4oaQIOKKouKKmj3iirgv4oan4oy1L-KEguKNieKNnOKMhSjDt-KKg-Knuy8rKSjCsOKImi3CpCku4qS4MVNpbeKHoS_Dl-KkmeKIqcKkCgriioNCIEEK "Uiua solution for 2024/14")|[**15**](https://adventofcode.com/2024/day/15) [<img height=12 src=".assets/rs.svg">](Rust/2024/15.rs "Rust solution for 2024/15")|
|
||||
|[**16**](https://adventofcode.com/2024/day/16)|[**17**](https://adventofcode.com/2024/day/17)|[**18**](https://adventofcode.com/2024/day/18)|[**19**](https://adventofcode.com/2024/day/19)|[**20**](https://adventofcode.com/2024/day/20)|[**21**](https://adventofcode.com/2024/day/21)|[**22**](https://adventofcode.com/2024/day/22)|
|
||||
|[**23**](https://adventofcode.com/2024/day/23)|[**24**](https://adventofcode.com/2024/day/24)|[**25**](https://adventofcode.com/2024/day/25)|26|27|28|29|
|
||||
|30|31||||||
|
||||
|
|
226
Rust/2024/15.rs
Normal file
226
Rust/2024/15.rs
Normal file
|
@ -0,0 +1,226 @@
|
|||
#![feature(test)]
|
||||
|
||||
use aoc::grid::Direction;
|
||||
|
||||
struct Input {
|
||||
grid: Grid,
|
||||
directions: Vec<Direction>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Grid {
|
||||
width: usize,
|
||||
height: usize,
|
||||
cells: Vec<Cell>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Cell {
|
||||
Empty,
|
||||
Wall,
|
||||
Robot,
|
||||
Box,
|
||||
BoxLeft,
|
||||
BoxRight,
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
fn is_pushable(&self, row: usize, col: usize, direction: Direction) -> bool {
|
||||
let Some((dst_col, dst_row)) = direction.step(col, row, self.width, self.height) else {
|
||||
return self.get(row, col) == Cell::Empty;
|
||||
};
|
||||
|
||||
match (self.get(row, col), direction) {
|
||||
(Cell::Empty, _) => true,
|
||||
(Cell::Wall, _) => false,
|
||||
(Cell::Robot | Cell::Box, _) => self.is_pushable(dst_row, dst_col, direction),
|
||||
(Cell::BoxLeft | Cell::BoxRight, Direction::East | Direction::West) => {
|
||||
self.is_pushable(dst_row, dst_col, direction)
|
||||
}
|
||||
(Cell::BoxLeft, Direction::North | Direction::South) => {
|
||||
self.is_pushable(dst_row, dst_col, direction)
|
||||
&& self.is_pushable(dst_row, dst_col + 1, direction)
|
||||
}
|
||||
(Cell::BoxRight, Direction::North | Direction::South) => {
|
||||
self.is_pushable(dst_row, dst_col, direction)
|
||||
&& self.is_pushable(dst_row, dst_col - 1, direction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, row: usize, col: usize, direction: Direction) {
|
||||
debug_assert!(self.is_pushable(row, col, direction));
|
||||
|
||||
let (dst_col, dst_row) = direction.step(col, row, self.width, self.height).unwrap();
|
||||
|
||||
match (self.get(row, col), direction) {
|
||||
(Cell::Empty, _) => {}
|
||||
(Cell::Wall, _) => panic!("Cannot move walls"),
|
||||
(cell @ (Cell::Robot | Cell::Box), _) => {
|
||||
self.push(dst_row, dst_col, direction);
|
||||
self.set(dst_row, dst_col, cell);
|
||||
self.set(row, col, Cell::Empty);
|
||||
}
|
||||
(cell @ (Cell::BoxLeft | Cell::BoxRight), Direction::East | Direction::West) => {
|
||||
self.push(dst_row, dst_col, direction);
|
||||
self.set(dst_row, dst_col, cell);
|
||||
self.set(row, col, Cell::Empty);
|
||||
}
|
||||
(cell @ Cell::BoxLeft, Direction::North | Direction::South) => {
|
||||
debug_assert_eq!(self.get(row, col + 1), Cell::BoxRight);
|
||||
self.push(dst_row, dst_col, direction);
|
||||
self.push(dst_row, dst_col + 1, direction);
|
||||
self.set(dst_row, dst_col, cell);
|
||||
self.set(dst_row, dst_col + 1, Cell::BoxRight);
|
||||
self.set(row, col, Cell::Empty);
|
||||
self.set(row, col + 1, Cell::Empty);
|
||||
}
|
||||
(Cell::BoxRight, Direction::North | Direction::South) => {
|
||||
debug_assert_eq!(self.get(row, col - 1), Cell::BoxLeft);
|
||||
self.push(row, col - 1, direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self, directions: impl Iterator<Item = Direction>) {
|
||||
let Some((mut i, mut j)) = self.coords().find(|&(i, j)| self.get(i, j) == Cell::Robot)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
for direction in directions {
|
||||
debug_assert_eq!(self.get(i, j), Cell::Robot);
|
||||
if self.is_pushable(i, j, direction) {
|
||||
self.push(i, j, direction);
|
||||
debug_assert_eq!(self.get(i, j), Cell::Empty);
|
||||
(j, i) = direction.step(j, i, self.width, self.height).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand(&self) -> Grid {
|
||||
let width = self.width * 2;
|
||||
let height = self.height;
|
||||
let cells = self
|
||||
.cells
|
||||
.iter()
|
||||
.flat_map(|&c| match c {
|
||||
Cell::Empty => [Cell::Empty, Cell::Empty],
|
||||
Cell::Wall => [Cell::Wall, Cell::Wall],
|
||||
Cell::Robot => [Cell::Robot, Cell::Empty],
|
||||
Cell::Box | Cell::BoxLeft | Cell::BoxRight => [Cell::BoxLeft, Cell::BoxRight],
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
debug_assert_eq!(cells.len(), width * height);
|
||||
Grid {
|
||||
width,
|
||||
height,
|
||||
cells,
|
||||
}
|
||||
}
|
||||
|
||||
fn score(&self) -> usize {
|
||||
self.coords()
|
||||
.filter(move |&(i, j)| matches!(self.get(i, j), Cell::Box | Cell::BoxLeft))
|
||||
.map(move |(i, j)| i * 100 + j)
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn coords(&self) -> impl Iterator<Item = (usize, usize)> {
|
||||
let width = self.width;
|
||||
(0..self.height).flat_map(move |i| (0..width).map(move |j| (i, j)))
|
||||
}
|
||||
|
||||
fn get(&self, row: usize, col: usize) -> Cell {
|
||||
self.cells[self.idx(row, col)]
|
||||
}
|
||||
|
||||
fn set(&mut self, row: usize, col: usize, cell: Cell) -> Cell {
|
||||
let idx = self.idx(row, col);
|
||||
std::mem::replace(&mut self.cells[idx], cell)
|
||||
}
|
||||
|
||||
fn idx(&self, row: usize, col: usize) -> usize {
|
||||
debug_assert!((0..self.height).contains(&row));
|
||||
debug_assert!((0..self.width).contains(&col));
|
||||
row * self.width + col
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> FromIterator<I> for Grid
|
||||
where
|
||||
I: IntoIterator<Item = Cell>,
|
||||
{
|
||||
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
|
||||
let mut iter = iter.into_iter();
|
||||
let Some(fst) = iter.next() else {
|
||||
return Self {
|
||||
width: 0,
|
||||
height: 0,
|
||||
cells: Vec::new(),
|
||||
};
|
||||
};
|
||||
|
||||
let mut width = 0;
|
||||
let mut height = 1;
|
||||
let mut cells = Vec::new();
|
||||
for c in fst.into_iter() {
|
||||
cells.push(c);
|
||||
width += 1;
|
||||
}
|
||||
|
||||
for row in iter {
|
||||
let mut row_width = 0;
|
||||
height += 1;
|
||||
for c in row.into_iter() {
|
||||
cells.push(c);
|
||||
row_width += 1;
|
||||
}
|
||||
debug_assert_eq!(row_width, width);
|
||||
}
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
cells,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
let mut lines = input.trim().lines();
|
||||
|
||||
let grid = lines
|
||||
.by_ref()
|
||||
.take_while(|l| !l.is_empty())
|
||||
.map(|l| {
|
||||
l.trim().bytes().map(|b| match b {
|
||||
b'.' => Cell::Empty,
|
||||
b'#' => Cell::Wall,
|
||||
b'@' => Cell::Robot,
|
||||
b'O' => Cell::Box,
|
||||
b'[' => Cell::BoxLeft,
|
||||
b']' => Cell::BoxRight,
|
||||
_ => panic!(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let directions = lines.flat_map(|l| l.chars()).map(Into::into).collect();
|
||||
|
||||
Input { grid, directions }
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> usize {
|
||||
let mut grid = input.grid.clone();
|
||||
grid.execute(input.directions.iter().copied());
|
||||
grid.score()
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> usize {
|
||||
let mut grid = input.grid.expand();
|
||||
grid.execute(input.directions.iter().copied());
|
||||
grid.score()
|
||||
}
|
||||
|
||||
aoc::main!(2024, 15, ex: 1, 2);
|
|
@ -13,7 +13,7 @@ aoc::year! {
|
|||
"12.rs",
|
||||
"13.rs",
|
||||
"14.rs",
|
||||
// "15.rs",
|
||||
"15.rs",
|
||||
// "16.rs",
|
||||
// "17.rs",
|
||||
// "18.rs",
|
||||
|
|
|
@ -402,3 +402,6 @@ path = "2024/13.rs"
|
|||
[[bin]]
|
||||
name = "2024_14"
|
||||
path = "2024/14.rs"
|
||||
[[bin]]
|
||||
name = "2024_15"
|
||||
path = "2024/15.rs"
|
||||
|
|
10
examples/2024/15/1
Normal file
10
examples/2024/15/1
Normal file
|
@ -0,0 +1,10 @@
|
|||
########
|
||||
#..O.O.#
|
||||
##@.O..#
|
||||
#...O..#
|
||||
#.#.O..#
|
||||
#...O..#
|
||||
#......#
|
||||
########
|
||||
|
||||
<^^>>>vv<v>>v<<
|
1
examples/2024/15/1.1
Normal file
1
examples/2024/15/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
2028
|
1
examples/2024/15/1.2
Normal file
1
examples/2024/15/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
1751
|
21
examples/2024/15/2
Normal file
21
examples/2024/15/2
Normal file
|
@ -0,0 +1,21 @@
|
|||
##########
|
||||
#..O..O.O#
|
||||
#......O.#
|
||||
#.OO..O.O#
|
||||
#..O@..O.#
|
||||
#O#..O...#
|
||||
#O..O..O.#
|
||||
#.OO.O.OO#
|
||||
#....O...#
|
||||
##########
|
||||
|
||||
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
|
||||
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
|
||||
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
|
||||
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
|
||||
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
|
||||
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
|
||||
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
|
||||
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
|
||||
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
|
||||
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
|
1
examples/2024/15/2.1
Normal file
1
examples/2024/15/2.1
Normal file
|
@ -0,0 +1 @@
|
|||
10092
|
1
examples/2024/15/2.2
Normal file
1
examples/2024/15/2.2
Normal file
|
@ -0,0 +1 @@
|
|||
9021
|
Loading…
Add table
Add a link
Reference in a new issue