├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── day01a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day01b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day02a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day02b ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day03a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day03b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day04a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day04b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day05a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day05b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day06a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day06b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day07a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day07b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day08a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day08b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day09a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day09b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day10a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day10b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day11a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day11b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day12a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day12b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day13a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day13b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day14a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day14b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day15a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day15b ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day16a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day16b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day17a ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day17b ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day18a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day18b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day19a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day19b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day20a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day20b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day21a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day21b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day22a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day22b ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day23a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day23b ├── .cargo │ └── config ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day24a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day24b ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── day25a ├── Cargo.lock ├── Cargo.toml ├── input.txt └── src │ └── main.rs ├── res ├── bench.log ├── bench.ods ├── bench_ms.log └── graph.png └── runner ├── Cargo.lock ├── Cargo.toml └── src ├── bin ├── bench.rs ├── runner-par.rs └── runner.rs └── lib.rs /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # Funding links 2 | github: 3 | - timvisee 4 | custom: 5 | - "https://timvisee.com/donate" 6 | patreon: timvisee 7 | ko_fi: timvisee 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .*.sw[po] 2 | target/ 3 | **/*.rs.bk 4 | .idea/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2020 in Rust 2 | My [Advent of Code 2020][aoc-2020] solutions in the Rust programming language. 3 | This repository holds a separate Rust project for each day and part. 4 | 5 | This year I attempt to develop a standalone, short, compact and fast solution 6 | for each problem (day part). 7 | 8 | I've written an article about my solutions here: 9 | https://timvisee.com/blog/solving-aoc-2020-in-under-a-second/ 10 | 11 | ## Timings 12 | Here is how long each solution takes to run to completion. 13 | All solutions are measured (non scientifically) with [`hyperfine`][hyperfine] on 14 | a `i5-4670k @ 3.8Ghz` machine running Linux. 15 | Timings include binary loading, execution, input and output timings. 16 | 17 | ![Benchmark](./res/graph.png) 18 | 19 | | | part A | part B | 20 | |:-----------------------------------------------|:------------------------------------|:------------------------------------| 21 | | [day 1](https://adventofcode.com/2020/day/1) | [` 0.170ms`](./day01a/src/main.rs) | [` 0.007ms`](./day01b/src/main.rs) | 22 | | [day 2](https://adventofcode.com/2020/day/2) | [` 0.553ms`](./day02a/src/main.rs) | [` 0.069ms`](./day02b/src/main.rs) | 23 | | [day 3](https://adventofcode.com/2020/day/3) | [` 0.009ms`](./day03a/src/main.rs) | [` 0.013ms`](./day03b/src/main.rs) | 24 | | [day 4](https://adventofcode.com/2020/day/4) | [` 0.157ms`](./day04a/src/main.rs) | [` 0.185ms`](./day04b/src/main.rs) | 25 | | [day 5](https://adventofcode.com/2020/day/5) | [` 0.004ms`](./day05a/src/main.rs) | [` 0.011ms`](./day05b/src/main.rs) | 26 | | [day 6](https://adventofcode.com/2020/day/6) | [` 0.031ms`](./day06a/src/main.rs) | [` 0.056ms`](./day06b/src/main.rs) | 27 | | [day 7](https://adventofcode.com/2020/day/7) | [` 0.002ms`](./day07a/src/main.rs) | [` 1.71 ms`](./day07b/src/main.rs) | 28 | | [day 8](https://adventofcode.com/2020/day/8) | [` 0.022ms`](./day08a/src/main.rs) | [` 0.131ms`](./day08b/src/main.rs) | 29 | | [day 9](https://adventofcode.com/2020/day/9) | [` 0.043ms`](./day09a/src/main.rs) | [` 0.025ms`](./day09b/src/main.rs) | 30 | | [day 10](https://adventofcode.com/2020/day/10) | [` 0.004ms`](./day10a/src/main.rs) | [` 0.005ms`](./day10b/src/main.rs) | 31 | | [day 11](https://adventofcode.com/2020/day/11) | [` 0.007ms`](./day11a/src/main.rs) | [` 6.56 ms`](./day11b/src/main.rs) | 32 | | [day 12](https://adventofcode.com/2020/day/12) | [` 0.011ms`](./day12a/src/main.rs) | [` 0.011ms`](./day12b/src/main.rs) | 33 | | [day 13](https://adventofcode.com/2020/day/13) | [` 0.002ms`](./day13a/src/main.rs) | [` 0.004ms`](./day13b/src/main.rs) | 34 | | [day 14](https://adventofcode.com/2020/day/14) | [` 0.276ms`](./day14a/src/main.rs) | [` 6.10 ms`](./day14b/src/main.rs) | 35 | | [day 15](https://adventofcode.com/2020/day/15) | [` 0.227ms`](./day15a/src/main.rs) | [`511 ms`](./day15b/src/main.rs) | 36 | | [day 16](https://adventofcode.com/2020/day/16) | [` 0.209ms`](./day16a/src/main.rs) | [` 0.526ms`](./day16b/src/main.rs) | 37 | | [day 17](https://adventofcode.com/2020/day/17) | [` 0.357ms`](./day17a/src/main.rs) | [` 7.98 ms`](./day17b/src/main.rs) | 38 | | [day 18](https://adventofcode.com/2020/day/18) | [` 0.246ms`](./day18a/src/main.rs) | [` 0.228ms`](./day18b/src/main.rs) | 39 | | [day 19](https://adventofcode.com/2020/day/19) | [` 0.364ms`](./day19a/src/main.rs) | [` 0.523ms`](./day19b/src/main.rs) | 40 | | [day 20](https://adventofcode.com/2020/day/20) | [` 0.111ms`](./day20a/src/main.rs) | [` 0.460ms`](./day20b/src/main.rs) | 41 | | [day 21](https://adventofcode.com/2020/day/21) | [` 0.464ms`](./day21a/src/main.rs) | [` 0.293ms`](./day21b/src/main.rs) | 42 | | [day 22](https://adventofcode.com/2020/day/22) | [` 0.003ms`](./day22a/src/main.rs) | [` 3.42 ms`](./day22b/src/main.rs) | 43 | | [day 23](https://adventofcode.com/2020/day/23) | [` 0.005ms`](./day23a/src/main.rs) | [`192 ms`](./day23b/src/main.rs) | 44 | | [day 24](https://adventofcode.com/2020/day/24) | [` 0.105ms`](./day24a/src/main.rs) | [` 43.2 ms`](./day24b/src/main.rs) | 45 | | [day 25](https://adventofcode.com/2020/day/25) | [` 27.9 ms`](./day25a/src/main.rs) | | 46 | 47 | | | one-by-one | parallel | 48 | |:-------------|:---------------------------------------|:-------------------------------------------| 49 | | _everything_ | [`699ms`](./runner/src/bin/runner.rs) | [`511ms`](./runner/src/bin/runner-par.rs) | 50 | 51 | ## Run solutions 52 | Each Rust project contains a `input.txt` file, holding the puzzle input. Simply 53 | run the project to see the solution appear. 54 | 55 | ```bash 56 | # One solution requires large stack size, set to allow unlimited size 57 | ulimit -s unlimited 58 | 59 | # Switch to day 1a, and run it 60 | cd day01a 61 | cargo run --release 62 | 63 | # Or run everything in parallel 64 | cd ../runner 65 | cargo run --release --bin runner-par 66 | 67 | # Or benchmark every day 68 | cd ../runner 69 | cargo run --release --bin bench 70 | ``` 71 | 72 | Some solutions might require Rust Nightly. 73 | 74 | ## Other years 75 | - [2024](https://github.com/timvisee/advent-of-code-2024) 76 | - [2023](https://github.com/timvisee/advent-of-code-2023) 77 | - [2022](https://github.com/timvisee/advent-of-code-2022) 78 | - [2021](https://github.com/timvisee/advent-of-code-2021) 79 | - [2020](https://github.com/timvisee/advent-of-code-2020) _(current)_ 80 | - [2019](https://github.com/timvisee/advent-of-code-2019) 81 | - [2018](https://github.com/timvisee/advent-of-code-2018) 82 | - [2017](https://github.com/timvisee/advent-of-code-2017) 83 | 84 | ## License 85 | This project is released under the GNU GPL-3.0 license. 86 | Check out the [LICENSE](LICENSE) file for more information. 87 | 88 | [aoc-2020]: https://adventofcode.com/2020 89 | [hyperfine]: https://github.com/sharkdp/hyperfine 90 | -------------------------------------------------------------------------------- /day01a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day01a" 5 | version = "0.1.0" 6 | dependencies = [ 7 | "itertools", 8 | ] 9 | 10 | [[package]] 11 | name = "either" 12 | version = "1.6.1" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 15 | 16 | [[package]] 17 | name = "itertools" 18 | version = "0.9.0" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" 21 | dependencies = [ 22 | "either", 23 | ] 24 | -------------------------------------------------------------------------------- /day01a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day01a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | itertools = "0.9" 12 | -------------------------------------------------------------------------------- /day01a/input.txt: -------------------------------------------------------------------------------- 1 | 1082 2 | 1770 3 | 1104 4 | 1180 5 | 1939 6 | 1952 7 | 1330 8 | 1569 9 | 1120 10 | 1281 11 | 1144 12 | 1091 13 | 2008 14 | 1967 15 | 1863 16 | 1819 17 | 1813 18 | 1986 19 | 1099 20 | 1860 21 | 1686 22 | 1063 23 | 1620 24 | 1107 25 | 1095 26 | 951 27 | 1897 28 | 1246 29 | 1264 30 | 1562 31 | 1151 32 | 1980 33 | 1942 34 | 1416 35 | 1170 36 | 1258 37 | 1075 38 | 1882 39 | 1329 40 | 2003 41 | 66 42 | 1249 43 | 1302 44 | 1221 45 | 1828 46 | 1154 47 | 1662 48 | 1103 49 | 1879 50 | 1205 51 | 1936 52 | 1472 53 | 1816 54 | 1071 55 | 1237 56 | 1467 57 | 1919 58 | 942 59 | 74 60 | 1178 61 | 1949 62 | 1947 63 | 1613 64 | 1931 65 | 1332 66 | 24 67 | 1987 68 | 1796 69 | 1256 70 | 1981 71 | 1158 72 | 1114 73 | 2004 74 | 1696 75 | 1775 76 | 1718 77 | 1102 78 | 1998 79 | 1540 80 | 1129 81 | 1870 82 | 1841 83 | 1582 84 | 1173 85 | 1417 86 | 1604 87 | 1214 88 | 1941 89 | 1440 90 | 1381 91 | 1149 92 | 1111 93 | 1766 94 | 1747 95 | 1940 96 | 960 97 | 1449 98 | 1171 99 | 1584 100 | 1926 101 | 1065 102 | 1832 103 | 1633 104 | 1245 105 | 1889 106 | 1906 107 | 1198 108 | 1959 109 | 1340 110 | 1951 111 | 1347 112 | 1097 113 | 1660 114 | 1957 115 | 1134 116 | 1730 117 | 1105 118 | 1124 119 | 1073 120 | 1679 121 | 1397 122 | 1963 123 | 1136 124 | 1983 125 | 1806 126 | 1964 127 | 1821 128 | 1997 129 | 1254 130 | 1823 131 | 1092 132 | 1119 133 | 2000 134 | 1089 135 | 1933 136 | 1478 137 | 1923 138 | 1576 139 | 1571 140 | 415 141 | 1875 142 | 1937 143 | 1112 144 | 1831 145 | 1969 146 | 1506 147 | 1929 148 | 1960 149 | 1322 150 | 110 151 | 1141 152 | 1080 153 | 1603 154 | 1126 155 | 1036 156 | 1762 157 | 1904 158 | 1122 159 | 1988 160 | 1962 161 | 1958 162 | 1953 163 | 1068 164 | 1188 165 | 1483 166 | 1518 167 | 1471 168 | 1961 169 | 1217 170 | 1559 171 | 1789 172 | 1523 173 | 2007 174 | 1093 175 | 1745 176 | 1955 177 | 1948 178 | 1474 179 | 1628 180 | 691 181 | 1398 182 | 1876 183 | 1650 184 | 1838 185 | 1950 186 | 1088 187 | 1697 188 | 1977 189 | 1364 190 | 1966 191 | 1945 192 | 1975 193 | 1606 194 | 1974 195 | 1847 196 | 1570 197 | 1148 198 | 1599 199 | 1772 200 | 1970 201 | -------------------------------------------------------------------------------- /day01a/src/main.rs: -------------------------------------------------------------------------------- 1 | use itertools::Itertools; 2 | 3 | pub fn main() { 4 | println!( 5 | "{}", 6 | include_str!("../input.txt") 7 | .lines() 8 | .map(|i| i.parse::().unwrap()) 9 | .combinations(2) 10 | .filter(|i| i.iter().sum::() == 2020) 11 | .next() 12 | .map(|i| i.iter().product::()) 13 | .unwrap() 14 | ); 15 | } 16 | -------------------------------------------------------------------------------- /day01b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day01b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day01b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day01b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day01b/input.txt: -------------------------------------------------------------------------------- 1 | 1082 2 | 1770 3 | 1104 4 | 1180 5 | 1939 6 | 1952 7 | 1330 8 | 1569 9 | 1120 10 | 1281 11 | 1144 12 | 1091 13 | 2008 14 | 1967 15 | 1863 16 | 1819 17 | 1813 18 | 1986 19 | 1099 20 | 1860 21 | 1686 22 | 1063 23 | 1620 24 | 1107 25 | 1095 26 | 951 27 | 1897 28 | 1246 29 | 1264 30 | 1562 31 | 1151 32 | 1980 33 | 1942 34 | 1416 35 | 1170 36 | 1258 37 | 1075 38 | 1882 39 | 1329 40 | 2003 41 | 66 42 | 1249 43 | 1302 44 | 1221 45 | 1828 46 | 1154 47 | 1662 48 | 1103 49 | 1879 50 | 1205 51 | 1936 52 | 1472 53 | 1816 54 | 1071 55 | 1237 56 | 1467 57 | 1919 58 | 942 59 | 74 60 | 1178 61 | 1949 62 | 1947 63 | 1613 64 | 1931 65 | 1332 66 | 24 67 | 1987 68 | 1796 69 | 1256 70 | 1981 71 | 1158 72 | 1114 73 | 2004 74 | 1696 75 | 1775 76 | 1718 77 | 1102 78 | 1998 79 | 1540 80 | 1129 81 | 1870 82 | 1841 83 | 1582 84 | 1173 85 | 1417 86 | 1604 87 | 1214 88 | 1941 89 | 1440 90 | 1381 91 | 1149 92 | 1111 93 | 1766 94 | 1747 95 | 1940 96 | 960 97 | 1449 98 | 1171 99 | 1584 100 | 1926 101 | 1065 102 | 1832 103 | 1633 104 | 1245 105 | 1889 106 | 1906 107 | 1198 108 | 1959 109 | 1340 110 | 1951 111 | 1347 112 | 1097 113 | 1660 114 | 1957 115 | 1134 116 | 1730 117 | 1105 118 | 1124 119 | 1073 120 | 1679 121 | 1397 122 | 1963 123 | 1136 124 | 1983 125 | 1806 126 | 1964 127 | 1821 128 | 1997 129 | 1254 130 | 1823 131 | 1092 132 | 1119 133 | 2000 134 | 1089 135 | 1933 136 | 1478 137 | 1923 138 | 1576 139 | 1571 140 | 415 141 | 1875 142 | 1937 143 | 1112 144 | 1831 145 | 1969 146 | 1506 147 | 1929 148 | 1960 149 | 1322 150 | 110 151 | 1141 152 | 1080 153 | 1603 154 | 1126 155 | 1036 156 | 1762 157 | 1904 158 | 1122 159 | 1988 160 | 1962 161 | 1958 162 | 1953 163 | 1068 164 | 1188 165 | 1483 166 | 1518 167 | 1471 168 | 1961 169 | 1217 170 | 1559 171 | 1789 172 | 1523 173 | 2007 174 | 1093 175 | 1745 176 | 1955 177 | 1948 178 | 1474 179 | 1628 180 | 691 181 | 1398 182 | 1876 183 | 1650 184 | 1838 185 | 1950 186 | 1088 187 | 1697 188 | 1977 189 | 1364 190 | 1966 191 | 1945 192 | 1975 193 | 1606 194 | 1974 195 | 1847 196 | 1570 197 | 1148 198 | 1599 199 | 1772 200 | 1970 201 | -------------------------------------------------------------------------------- /day01b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut items: Vec = include_str!("../input.txt") 3 | .lines() 4 | .map(|i| i.parse().unwrap()) 5 | .collect(); 6 | items.sort_unstable(); 7 | 8 | for a in &items { 9 | for b in &items { 10 | for c in &items { 11 | if a + b + c == 2020 { 12 | println!("{}", a * b * c); 13 | return; 14 | } 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /day02a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day02a" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "regex", 19 | ] 20 | 21 | [[package]] 22 | name = "memchr" 23 | version = "2.4.1" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 26 | 27 | [[package]] 28 | name = "regex" 29 | version = "1.5.4" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 32 | dependencies = [ 33 | "aho-corasick", 34 | "memchr", 35 | "regex-syntax", 36 | ] 37 | 38 | [[package]] 39 | name = "regex-syntax" 40 | version = "0.6.25" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 43 | -------------------------------------------------------------------------------- /day02a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day02a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | regex = "1.4" 12 | -------------------------------------------------------------------------------- /day02a/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | 3 | pub fn main() { 4 | let re = Regex::new(r#"^(\d+)-(\d+) (.): (.+)$"#).unwrap(); 5 | println!( 6 | "{}", 7 | include_str!("../input.txt") 8 | .lines() 9 | .map(|p| re.captures(p).unwrap()) 10 | .filter(|p| { 11 | let len = p[4].matches(&p[3]).count(); 12 | len >= p[1].parse().unwrap() && len <= p[2].parse().unwrap() 13 | }) 14 | .count(), 15 | ); 16 | } 17 | -------------------------------------------------------------------------------- /day02b/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = true 7 | panic = "abort" 8 | codegen-units = 1 9 | -------------------------------------------------------------------------------- /day02b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "arrayvec" 7 | version = "0.5.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" 10 | 11 | [[package]] 12 | name = "atoi" 13 | version = "0.4.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" 16 | dependencies = [ 17 | "num-traits", 18 | ] 19 | 20 | [[package]] 21 | name = "autocfg" 22 | version = "1.0.1" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 25 | 26 | [[package]] 27 | name = "bitflags" 28 | version = "1.3.2" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 31 | 32 | [[package]] 33 | name = "bitvec" 34 | version = "0.19.6" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33" 37 | dependencies = [ 38 | "funty", 39 | "radium", 40 | "tap", 41 | "wyz", 42 | ] 43 | 44 | [[package]] 45 | name = "cfg-if" 46 | version = "1.0.0" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 49 | 50 | [[package]] 51 | name = "day02b" 52 | version = "0.1.0" 53 | dependencies = [ 54 | "atoi", 55 | "nom", 56 | ] 57 | 58 | [[package]] 59 | name = "funty" 60 | version = "1.1.0" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" 63 | 64 | [[package]] 65 | name = "lexical-core" 66 | version = "0.7.6" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" 69 | dependencies = [ 70 | "arrayvec", 71 | "bitflags", 72 | "cfg-if", 73 | "ryu", 74 | "static_assertions", 75 | ] 76 | 77 | [[package]] 78 | name = "memchr" 79 | version = "2.3.4" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" 82 | 83 | [[package]] 84 | name = "nom" 85 | version = "6.2.1" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "9c5c51b9083a3c620fa67a2a635d1ce7d95b897e957d6b28ff9a5da960a103a6" 88 | dependencies = [ 89 | "bitvec", 90 | "funty", 91 | "lexical-core", 92 | "memchr", 93 | "version_check", 94 | ] 95 | 96 | [[package]] 97 | name = "num-traits" 98 | version = "0.2.14" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 101 | dependencies = [ 102 | "autocfg", 103 | ] 104 | 105 | [[package]] 106 | name = "radium" 107 | version = "0.5.3" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" 110 | 111 | [[package]] 112 | name = "ryu" 113 | version = "1.0.6" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" 116 | 117 | [[package]] 118 | name = "static_assertions" 119 | version = "1.1.0" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" 122 | 123 | [[package]] 124 | name = "tap" 125 | version = "1.0.1" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" 128 | 129 | [[package]] 130 | name = "version_check" 131 | version = "0.9.3" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" 134 | 135 | [[package]] 136 | name = "wyz" 137 | version = "0.2.0" 138 | source = "registry+https://github.com/rust-lang/crates.io-index" 139 | checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" 140 | -------------------------------------------------------------------------------- /day02b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day02b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | atoi = "0.4" 12 | nom = "6.0" 13 | -------------------------------------------------------------------------------- /day02b/src/main.rs: -------------------------------------------------------------------------------- 1 | use atoi::atoi; 2 | use nom::{character::complete::alpha1, *}; 3 | 4 | pub fn main() { 5 | println!( 6 | "{}", 7 | entries(include_bytes!("../input.txt")) 8 | .unwrap() 9 | .1 10 | .into_iter() 11 | .filter(|p| *p) 12 | .count() 13 | ); 14 | } 15 | 16 | named!(usize<&[u8], usize>, map_opt!(nom::character::complete::digit1, atoi)); 17 | 18 | named!(minmax<&[u8], (usize, usize)>, separated_pair!(usize, char!('-'), usize)); 19 | 20 | named!(needle<&[u8], u8>, terminated!(nom::number::complete::u8, char!(':'))); 21 | 22 | named!(entry<&[u8], (usize, usize, u8, &[u8])>, 23 | do_parse!( 24 | mm: minmax >> 25 | char!(' ') >> 26 | needle: needle >> 27 | char!(' ') >> 28 | haystack: alpha1 >> 29 | (mm.0, mm.1, needle, haystack) 30 | ) 31 | ); 32 | 33 | named!(test_entry<&[u8], bool>, map!(entry, |e| ((e.3[e.0 - 1] == e.2) != (e.3[e.1 - 1] == e.2)))); 34 | 35 | named!(entries<&[u8], Vec>, separated_list1!(tag!("\n"), test_entry)); 36 | -------------------------------------------------------------------------------- /day03a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day03a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day03a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day03a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day03a/input.txt: -------------------------------------------------------------------------------- 1 | ...............#...#..#...#.... 2 | ...#....##.....##...######..#.. 3 | ....#.....#.##..#...#..#....... 4 | ...#..........#.#....#......#.. 5 | ....#.#...#.#.......#......##.# 6 | ....#.#..........#.....#.##.... 7 | ##...#.#.##......#......#.#.#.. 8 | #.#.#........#....#..#.#....... 9 | ..#...##..#..#.......#....###.. 10 | .#.#..........#...#........#..# 11 | .#..#......#....#.#...#...#.#.# 12 | ..#.........#..##.....#.#.##.#. 13 | .#......#...#....#.....#......# 14 | ........#..##..##.........#..#. 15 | .....#....###..#....##........# 16 | .###...#..##..#.##......##...## 17 | .#...#...#..#.#..#...##.....#.. 18 | .......#....#....#.#...#....... 19 | .##.......#.##...#.#...#..#.... 20 | #.#...#......#....#.......#.... 21 | ..###...............####...#.#. 22 | .##.#....#......#..#...#.#..#.# 23 | .............#.#.#......##..... 24 | #....#.#.#........#....##...#.. 25 | ...##....##...##..#...#........ 26 | ..##......#...#......#...###... 27 | ...#...##......##.#.#..#....... 28 | #......#..#...#.#..#......#..## 29 | .#..#..#........##....##....... 30 | .#...........###.###.##...#.... 31 | ............#.#...........#.#.. 32 | #...#........#...#...#..#.#.#.# 33 | ...#.......#.#.#..#.#.......... 34 | ......#..#..#....##..##..##.... 35 | ........##...#......#..#.#..... 36 | ..#.#.......#......#........... 37 | #.#.....#......##..........#.#. 38 | #.....###.#...#...#..#....#...# 39 | .##.#...#............##.....#.. 40 | ###....#.#.....#.......##...... 41 | ##.......##.....#..#...#..##.## 42 | ....#.##............###...#..## 43 | .###..#...##.#..#..##..#....... 44 | .##.##..####.....#.........#... 45 | ....####..#...#....#.#...#..... 46 | ..##....#..#......#...........# 47 | ..........#......#..##.......#. 48 | .................#.#.#........# 49 | #.......##.#...##.......##.##.. 50 | .#................#.#.....##..# 51 | ......#..#............##.#..... 52 | ...##............#.....#.....#. 53 | ##.###.#.......#....#.........# 54 | ......#.....#.#.#.#......#..... 55 | ......#.##......#......##...... 56 | ..#....#...#..#.....#..#....#.# 57 | .#.##.##.....#.......#.......#. 58 | ...#..#.#......##....##..#..... 59 | .#.....#......##...#..#.#....#. 60 | ..#......#....#..#..###.#.#.... 61 | .....#........#.#...#..#.#..... 62 | ....#.#.......#...##.....####.. 63 | #..#..##...#...........#...#..# 64 | .#..#...#.....#.....#.#.....#.# 65 | ..##..###.....#...#.#.#.......# 66 | #..##.##......###..#......###.. 67 | #..#...#.......#....#.#...#.#.# 68 | ...........###..#...#..##....#. 69 | .....#...........###.......#... 70 | ##......#.......#......##.#..#. 71 | #.................#........#... 72 | #.#.............#......#...#..# 73 | ......#.#....#....#....#....#.# 74 | ..#...#....#..#....#....#..#... 75 | #....#......#..#...#..#....#.#. 76 | ..#.....#..#...#...#.......#... 77 | .#........###..##.#..#......... 78 | .....##.#.....#..###..#........ 79 | ...#...#.###....######......#.. 80 | .###.#..#.....#.....#..#...#... 81 | ##..#.#......#.........#...#..# 82 | ###...##..###.......#..##.#.#.# 83 | .#...................#..#...##. 84 | .#...#...###...#.......#......# 85 | ....#.....##....#...##.#...#.## 86 | ..#....#......#...#..###....... 87 | .........#..........##........# 88 | ...#.........##..#....#..###... 89 | ......#.##....#.#........#.#.## 90 | ....#..#...#.............#....# 91 | #..#.....#.#.....#....#........ 92 | ....#..#...####.#.###..#....... 93 | .....#....#....#..#..#.#.....#. 94 | #..##....#.....#.#............. 95 | .##...#..#.......#...##.###.... 96 | ##......#...##....#......##.... 97 | #......#.#......#.#..#......#.. 98 | .#...#......#............###..# 99 | .#..#.#.....#.#.....#..#....#.. 100 | .#............#...#..#..#...##. 101 | ...#...#....#.#.#....#....#.... 102 | ........#......###.#....##.##.# 103 | ......#.#..................##.. 104 | ..#..#........#..##..........## 105 | .#...#.#....#####.#.#..#....... 106 | .....#..#.#..#.....#.#........# 107 | #.#..#..#.#..........#..#..#... 108 | .##........#.#.......#........# 109 | .....#....#..##...#......##.... 110 | ....#.##.##...##.#.........#.#. 111 | ...#...#..#.#.......#.......#.. 112 | .................##..#......... 113 | ..##.##....#...#.##.......#..#. 114 | ....#...........#.....###....## 115 | .#..................#.......... 116 | ....#.##....#......##.#..#.##.# 117 | #......#..#.#....##...####...#. 118 | #.....#.#.##...........#.##...# 119 | .......#...##..#.........##.... 120 | .#...#..........##......#..#.#. 121 | #...#.#......#.....#........#.. 122 | ........#.....#.......##......# 123 | .#..#...........#...#..#..#.... 124 | ......#.##......##.#......#..## 125 | ......#....#..#................ 126 | ##.#.......#......#..#....##.## 127 | ..#...###..#.......#...#.#....# 128 | .....#...#.........#.....#..#.# 129 | ..#.....#.........#..#...#.#... 130 | .#.........##..#.#...#.....##.. 131 | ..........#.#.#...#....#....#.. 132 | ....#.###.#..#..#..#.#........# 133 | ..#...##...##.#.#.....#.#..##.. 134 | .#..#......#.####..#.......#..# 135 | ##.......#.....#.#.#..#...##..# 136 | .##......##...##.........#..#.. 137 | ..#.##..#......#......#..#..#.. 138 | ..#..#.##..#........#....#.#... 139 | ##.####...##.#..##.........#..# 140 | .#.......#.#..#.....#.##....... 141 | ........#.........#...........# 142 | ..#...#.####.....##.##.#....#.# 143 | .....#..##.#..###.##.#.#...#... 144 | #..##..##....#...#....#...#.... 145 | .###.#....#..#.......#......### 146 | .#....##.....#.#........#...#.# 147 | #.#.#..#..#...#....#..#.....#.. 148 | ....##...#.##..#............#.. 149 | ##......###...#...#...#....#... 150 | #.#...#.#..#..##.##..##..#....# 151 | .#...#.#....#..##.#..##..#..... 152 | .............#..#..#.#.....#... 153 | #........#..........#....###... 154 | ..#..#......#...#........#..... 155 | .#..#.............#.........#.. 156 | #.....#....##..#..#.#.#..#..... 157 | ...#......#.........#.#....##.# 158 | ..#.......##....###..#......... 159 | .#.......#......#.............. 160 | .#...##.....##...###.#....#.#.. 161 | ......#....#.........#.......#. 162 | ##.......#..##....###.#.....##. 163 | .....#......####.....#......#.. 164 | ....#....#..###....#.....##...# 165 | ...#...#.#........#.....#..#.## 166 | #..#....#.#...###...##.....##.. 167 | #.....##...#.#............#.... 168 | ##....#.......#.#.#....#.#.###. 169 | #........#...#...####.#....#.#. 170 | .##.#......#........#..#.....#. 171 | ...##.#.......#...#.#.##..#...# 172 | ......#.........##..#....#..... 173 | .#......#...........#......#... 174 | ......#.#.#.#..#.#....#....#..# 175 | ##.................##...#.#.... 176 | ........#.........#..#..#...### 177 | .#........#........#....#..#... 178 | ....###.##.##......#.#...#....# 179 | #......#.#..............#...... 180 | .......#..#....#..##.........#. 181 | #............#....#............ 182 | #...#.#.........##..###...##... 183 | .#....#.#.#.....#..#..##....... 184 | .............##.#.#.......#..#. 185 | #...#..##.#..###.....##..#..... 186 | ...#.#.....#...#......##.#..#.. 187 | #..........#.##.#...#...#.#...# 188 | ...#.#...#.........#.#..#.#..#. 189 | #....#.................#....... 190 | ..#..#.#.#..#.#..##...#.#...... 191 | ..#....#.#.#...#.....#...#..... 192 | #...#.###......#.......#..#..#. 193 | #.#.##.##..............#.#.#..# 194 | #..........#.#.........#.###... 195 | ...........#.......#.#..#...... 196 | ....#.#..#....#....#....##..... 197 | #..#...##########.........#.#.. 198 | ..#.............##........#.#.. 199 | #.#.##......#...#.#....##..##.. 200 | ..##..#.#.#....#......#.#..#.#. 201 | .#.#...#................#...... 202 | #...#...#.....##.#...#.......#. 203 | .....##.......#...#......#.##.. 204 | ....#.##..........#.#.##....##. 205 | ...........##........#.#.#.#... 206 | ..#...........###.#....#..#.... 207 | ..#.#...#...#.#........#.....#. 208 | .##......##.....#........#..... 209 | ....#.......#....#...#.##...#.. 210 | .#.....##.....#...##........... 211 | #....#.##.##...#...###.#####... 212 | ..#.#......#.#.......#....#..#. 213 | ..........#...#...........#.... 214 | .........#..#...#...#......#.## 215 | .#...#.#...#.###.##..#........# 216 | #....#.....#.......##....#..... 217 | #.....#..#.....#...##.......#.. 218 | #.#.#.#.............#.....#...# 219 | ...#.....#.....#............... 220 | ..##.###.#.#........#.........# 221 | ...##..#.##..#....#.#...#.#.... 222 | ...##...#...##.#.........#.#..# 223 | .###......#....#.........#.#... 224 | ###.#.#...#.......#...#.....#.# 225 | ...#.#.......#.....####........ 226 | ......#..#.....###.#....#..#... 227 | ..####...#...#..#......#...#... 228 | #..............##....#......##. 229 | ..##..###...##..#.#.........#.. 230 | #..#.#...#.......#.........##.. 231 | ..##....#......#...#..##....... 232 | ..#.#..#..###.....#.....#...### 233 | .#..#.##.....##.#.#.#........#. 234 | ..#.#.........#................ 235 | ..#...........#.#...##.#...#..# 236 | .....#........#..#.....#....#.. 237 | #.#....#...........##.....#..## 238 | ##.......#.....#.....#.#......# 239 | .##............####.#........## 240 | ....##.#.....#......#....#...#. 241 | .#.#...##.#..##..#..........#.. 242 | ..........................#.... 243 | ##..##..#..#.#....#.....#...... 244 | ...#.#........#.#.##.#.....#..# 245 | #..#....#...#..#...#........#.# 246 | #.......#####...#..#..#.#...... 247 | #.##....#......#......#..###... 248 | ..#.......#...........#.....##. 249 | #...#....#..#......##...#...... 250 | ...##.#..##........#..###...... 251 | ##.#...........#.............## 252 | #.....#..#.....#.....#......... 253 | .#..........#..#......###...... 254 | ...#...#..##.......#...#...#.#. 255 | ..####......#.....#..#.#......# 256 | ....#..#.....#.#............... 257 | .#.......#....#.....#..##....#. 258 | .....#.........#..........##... 259 | #...........#..#.....#........# 260 | ............#..##...#...#.#.... 261 | ##.............####...#.....#.. 262 | .....#......#....##.#.....##... 263 | ...........#.....#.#..#.#...... 264 | .#.#......#....#.............## 265 | ##...#.#.......##........#..... 266 | #..#.....#.#.......####...#..#. 267 | ....#.#...#....#.###..#..#.#... 268 | .....#.#..#......#.##.#####.#.. 269 | .....#....##..........#......#. 270 | #.......#........##.......##... 271 | #...#..#..##.#....#...#...#.... 272 | ...#..##..#...#..........#..... 273 | #..#....###.....#......##...... 274 | ...###......#.....#....#....... 275 | #.#...#.#..###.####.......#.#.# 276 | ...#......#.#..##..#.....#..... 277 | #.#............#.....##.#...... 278 | #..#......##...##..#...#.#..### 279 | #.....#...#..#..#....#..###.... 280 | ####..###....#..##....#..#..... 281 | ..##.#.....#.......##....#.#.#. 282 | ##..#.#.......#.#...##.#..#.#.. 283 | ..#.#.#.##.#.#.#...........#... 284 | ...#.##.....#....#..#.#..#..... 285 | ...#..#.........#..........#..# 286 | #...#..#.....#.#.#............. 287 | ##.#....##.##...#...#..#..#..#. 288 | ....####..##..##.#..#...##..... 289 | ##.....##.#.#...#.#.......###.. 290 | #..#.#....#......#.......##...# 291 | #.#...............#..#..#...... 292 | .....##...##..#........#......# 293 | .#..#............##......#....# 294 | ......#.#..#..##.#......#.....# 295 | ..#.#.............#...#......## 296 | ....#.#..#..#...##...#..##..... 297 | #.#.............#...#.#..#....# 298 | #..#..#.##....#....#...#....... 299 | ....#..#..........#.....##...#. 300 | ..#####.......#...#..........#. 301 | ....#......##.......#..##.#.#.# 302 | #...#.#.....#....#....##....... 303 | ..##.#.#..#.#...#.....##.....#. 304 | #.#..#....#............#..#.#.. 305 | ...#.##..##..#.#...###......#.# 306 | ......##.......#....#......###. 307 | ....#..##.......#..#.#....#..#. 308 | #............#..........##..#.# 309 | ..#.....#..#.#..##..#....##.#.. 310 | .....#.....#....##.#.#......#.# 311 | ...####.......###..#...#.#....# 312 | .#.##.....##.....##..#..#...... 313 | ...#..###..##.................. 314 | ##..##.....#.#..#..#.#........# 315 | .#.#........##.........#....#.. 316 | ........#......###....#...#.... 317 | ......#...........#...........# 318 | .#...###...#.........#.#...#..# 319 | .....#..........#......##....#. 320 | .#.#...#........##.......#.#... 321 | .....##.....##....#...#...#..#. 322 | ..#.....................##...## 323 | #..#.#......##...##..#......#.. 324 | -------------------------------------------------------------------------------- /day03a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let map: Vec<&[u8]> = include_str!("../input.txt") 3 | .lines() 4 | .map(|l| l.as_bytes()) 5 | .collect(); 6 | 7 | println!( 8 | "{}", 9 | (0..map.len() - 1) 10 | .map(|i| ((i + 1) * 3, i + 1)) 11 | .filter(|(x, y)| map[*y][*x % map[0].len()] == b'#') 12 | .count() 13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /day03b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day03b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day03b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day03b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day03b/input.txt: -------------------------------------------------------------------------------- 1 | ...............#...#..#...#.... 2 | ...#....##.....##...######..#.. 3 | ....#.....#.##..#...#..#....... 4 | ...#..........#.#....#......#.. 5 | ....#.#...#.#.......#......##.# 6 | ....#.#..........#.....#.##.... 7 | ##...#.#.##......#......#.#.#.. 8 | #.#.#........#....#..#.#....... 9 | ..#...##..#..#.......#....###.. 10 | .#.#..........#...#........#..# 11 | .#..#......#....#.#...#...#.#.# 12 | ..#.........#..##.....#.#.##.#. 13 | .#......#...#....#.....#......# 14 | ........#..##..##.........#..#. 15 | .....#....###..#....##........# 16 | .###...#..##..#.##......##...## 17 | .#...#...#..#.#..#...##.....#.. 18 | .......#....#....#.#...#....... 19 | .##.......#.##...#.#...#..#.... 20 | #.#...#......#....#.......#.... 21 | ..###...............####...#.#. 22 | .##.#....#......#..#...#.#..#.# 23 | .............#.#.#......##..... 24 | #....#.#.#........#....##...#.. 25 | ...##....##...##..#...#........ 26 | ..##......#...#......#...###... 27 | ...#...##......##.#.#..#....... 28 | #......#..#...#.#..#......#..## 29 | .#..#..#........##....##....... 30 | .#...........###.###.##...#.... 31 | ............#.#...........#.#.. 32 | #...#........#...#...#..#.#.#.# 33 | ...#.......#.#.#..#.#.......... 34 | ......#..#..#....##..##..##.... 35 | ........##...#......#..#.#..... 36 | ..#.#.......#......#........... 37 | #.#.....#......##..........#.#. 38 | #.....###.#...#...#..#....#...# 39 | .##.#...#............##.....#.. 40 | ###....#.#.....#.......##...... 41 | ##.......##.....#..#...#..##.## 42 | ....#.##............###...#..## 43 | .###..#...##.#..#..##..#....... 44 | .##.##..####.....#.........#... 45 | ....####..#...#....#.#...#..... 46 | ..##....#..#......#...........# 47 | ..........#......#..##.......#. 48 | .................#.#.#........# 49 | #.......##.#...##.......##.##.. 50 | .#................#.#.....##..# 51 | ......#..#............##.#..... 52 | ...##............#.....#.....#. 53 | ##.###.#.......#....#.........# 54 | ......#.....#.#.#.#......#..... 55 | ......#.##......#......##...... 56 | ..#....#...#..#.....#..#....#.# 57 | .#.##.##.....#.......#.......#. 58 | ...#..#.#......##....##..#..... 59 | .#.....#......##...#..#.#....#. 60 | ..#......#....#..#..###.#.#.... 61 | .....#........#.#...#..#.#..... 62 | ....#.#.......#...##.....####.. 63 | #..#..##...#...........#...#..# 64 | .#..#...#.....#.....#.#.....#.# 65 | ..##..###.....#...#.#.#.......# 66 | #..##.##......###..#......###.. 67 | #..#...#.......#....#.#...#.#.# 68 | ...........###..#...#..##....#. 69 | .....#...........###.......#... 70 | ##......#.......#......##.#..#. 71 | #.................#........#... 72 | #.#.............#......#...#..# 73 | ......#.#....#....#....#....#.# 74 | ..#...#....#..#....#....#..#... 75 | #....#......#..#...#..#....#.#. 76 | ..#.....#..#...#...#.......#... 77 | .#........###..##.#..#......... 78 | .....##.#.....#..###..#........ 79 | ...#...#.###....######......#.. 80 | .###.#..#.....#.....#..#...#... 81 | ##..#.#......#.........#...#..# 82 | ###...##..###.......#..##.#.#.# 83 | .#...................#..#...##. 84 | .#...#...###...#.......#......# 85 | ....#.....##....#...##.#...#.## 86 | ..#....#......#...#..###....... 87 | .........#..........##........# 88 | ...#.........##..#....#..###... 89 | ......#.##....#.#........#.#.## 90 | ....#..#...#.............#....# 91 | #..#.....#.#.....#....#........ 92 | ....#..#...####.#.###..#....... 93 | .....#....#....#..#..#.#.....#. 94 | #..##....#.....#.#............. 95 | .##...#..#.......#...##.###.... 96 | ##......#...##....#......##.... 97 | #......#.#......#.#..#......#.. 98 | .#...#......#............###..# 99 | .#..#.#.....#.#.....#..#....#.. 100 | .#............#...#..#..#...##. 101 | ...#...#....#.#.#....#....#.... 102 | ........#......###.#....##.##.# 103 | ......#.#..................##.. 104 | ..#..#........#..##..........## 105 | .#...#.#....#####.#.#..#....... 106 | .....#..#.#..#.....#.#........# 107 | #.#..#..#.#..........#..#..#... 108 | .##........#.#.......#........# 109 | .....#....#..##...#......##.... 110 | ....#.##.##...##.#.........#.#. 111 | ...#...#..#.#.......#.......#.. 112 | .................##..#......... 113 | ..##.##....#...#.##.......#..#. 114 | ....#...........#.....###....## 115 | .#..................#.......... 116 | ....#.##....#......##.#..#.##.# 117 | #......#..#.#....##...####...#. 118 | #.....#.#.##...........#.##...# 119 | .......#...##..#.........##.... 120 | .#...#..........##......#..#.#. 121 | #...#.#......#.....#........#.. 122 | ........#.....#.......##......# 123 | .#..#...........#...#..#..#.... 124 | ......#.##......##.#......#..## 125 | ......#....#..#................ 126 | ##.#.......#......#..#....##.## 127 | ..#...###..#.......#...#.#....# 128 | .....#...#.........#.....#..#.# 129 | ..#.....#.........#..#...#.#... 130 | .#.........##..#.#...#.....##.. 131 | ..........#.#.#...#....#....#.. 132 | ....#.###.#..#..#..#.#........# 133 | ..#...##...##.#.#.....#.#..##.. 134 | .#..#......#.####..#.......#..# 135 | ##.......#.....#.#.#..#...##..# 136 | .##......##...##.........#..#.. 137 | ..#.##..#......#......#..#..#.. 138 | ..#..#.##..#........#....#.#... 139 | ##.####...##.#..##.........#..# 140 | .#.......#.#..#.....#.##....... 141 | ........#.........#...........# 142 | ..#...#.####.....##.##.#....#.# 143 | .....#..##.#..###.##.#.#...#... 144 | #..##..##....#...#....#...#.... 145 | .###.#....#..#.......#......### 146 | .#....##.....#.#........#...#.# 147 | #.#.#..#..#...#....#..#.....#.. 148 | ....##...#.##..#............#.. 149 | ##......###...#...#...#....#... 150 | #.#...#.#..#..##.##..##..#....# 151 | .#...#.#....#..##.#..##..#..... 152 | .............#..#..#.#.....#... 153 | #........#..........#....###... 154 | ..#..#......#...#........#..... 155 | .#..#.............#.........#.. 156 | #.....#....##..#..#.#.#..#..... 157 | ...#......#.........#.#....##.# 158 | ..#.......##....###..#......... 159 | .#.......#......#.............. 160 | .#...##.....##...###.#....#.#.. 161 | ......#....#.........#.......#. 162 | ##.......#..##....###.#.....##. 163 | .....#......####.....#......#.. 164 | ....#....#..###....#.....##...# 165 | ...#...#.#........#.....#..#.## 166 | #..#....#.#...###...##.....##.. 167 | #.....##...#.#............#.... 168 | ##....#.......#.#.#....#.#.###. 169 | #........#...#...####.#....#.#. 170 | .##.#......#........#..#.....#. 171 | ...##.#.......#...#.#.##..#...# 172 | ......#.........##..#....#..... 173 | .#......#...........#......#... 174 | ......#.#.#.#..#.#....#....#..# 175 | ##.................##...#.#.... 176 | ........#.........#..#..#...### 177 | .#........#........#....#..#... 178 | ....###.##.##......#.#...#....# 179 | #......#.#..............#...... 180 | .......#..#....#..##.........#. 181 | #............#....#............ 182 | #...#.#.........##..###...##... 183 | .#....#.#.#.....#..#..##....... 184 | .............##.#.#.......#..#. 185 | #...#..##.#..###.....##..#..... 186 | ...#.#.....#...#......##.#..#.. 187 | #..........#.##.#...#...#.#...# 188 | ...#.#...#.........#.#..#.#..#. 189 | #....#.................#....... 190 | ..#..#.#.#..#.#..##...#.#...... 191 | ..#....#.#.#...#.....#...#..... 192 | #...#.###......#.......#..#..#. 193 | #.#.##.##..............#.#.#..# 194 | #..........#.#.........#.###... 195 | ...........#.......#.#..#...... 196 | ....#.#..#....#....#....##..... 197 | #..#...##########.........#.#.. 198 | ..#.............##........#.#.. 199 | #.#.##......#...#.#....##..##.. 200 | ..##..#.#.#....#......#.#..#.#. 201 | .#.#...#................#...... 202 | #...#...#.....##.#...#.......#. 203 | .....##.......#...#......#.##.. 204 | ....#.##..........#.#.##....##. 205 | ...........##........#.#.#.#... 206 | ..#...........###.#....#..#.... 207 | ..#.#...#...#.#........#.....#. 208 | .##......##.....#........#..... 209 | ....#.......#....#...#.##...#.. 210 | .#.....##.....#...##........... 211 | #....#.##.##...#...###.#####... 212 | ..#.#......#.#.......#....#..#. 213 | ..........#...#...........#.... 214 | .........#..#...#...#......#.## 215 | .#...#.#...#.###.##..#........# 216 | #....#.....#.......##....#..... 217 | #.....#..#.....#...##.......#.. 218 | #.#.#.#.............#.....#...# 219 | ...#.....#.....#............... 220 | ..##.###.#.#........#.........# 221 | ...##..#.##..#....#.#...#.#.... 222 | ...##...#...##.#.........#.#..# 223 | .###......#....#.........#.#... 224 | ###.#.#...#.......#...#.....#.# 225 | ...#.#.......#.....####........ 226 | ......#..#.....###.#....#..#... 227 | ..####...#...#..#......#...#... 228 | #..............##....#......##. 229 | ..##..###...##..#.#.........#.. 230 | #..#.#...#.......#.........##.. 231 | ..##....#......#...#..##....... 232 | ..#.#..#..###.....#.....#...### 233 | .#..#.##.....##.#.#.#........#. 234 | ..#.#.........#................ 235 | ..#...........#.#...##.#...#..# 236 | .....#........#..#.....#....#.. 237 | #.#....#...........##.....#..## 238 | ##.......#.....#.....#.#......# 239 | .##............####.#........## 240 | ....##.#.....#......#....#...#. 241 | .#.#...##.#..##..#..........#.. 242 | ..........................#.... 243 | ##..##..#..#.#....#.....#...... 244 | ...#.#........#.#.##.#.....#..# 245 | #..#....#...#..#...#........#.# 246 | #.......#####...#..#..#.#...... 247 | #.##....#......#......#..###... 248 | ..#.......#...........#.....##. 249 | #...#....#..#......##...#...... 250 | ...##.#..##........#..###...... 251 | ##.#...........#.............## 252 | #.....#..#.....#.....#......... 253 | .#..........#..#......###...... 254 | ...#...#..##.......#...#...#.#. 255 | ..####......#.....#..#.#......# 256 | ....#..#.....#.#............... 257 | .#.......#....#.....#..##....#. 258 | .....#.........#..........##... 259 | #...........#..#.....#........# 260 | ............#..##...#...#.#.... 261 | ##.............####...#.....#.. 262 | .....#......#....##.#.....##... 263 | ...........#.....#.#..#.#...... 264 | .#.#......#....#.............## 265 | ##...#.#.......##........#..... 266 | #..#.....#.#.......####...#..#. 267 | ....#.#...#....#.###..#..#.#... 268 | .....#.#..#......#.##.#####.#.. 269 | .....#....##..........#......#. 270 | #.......#........##.......##... 271 | #...#..#..##.#....#...#...#.... 272 | ...#..##..#...#..........#..... 273 | #..#....###.....#......##...... 274 | ...###......#.....#....#....... 275 | #.#...#.#..###.####.......#.#.# 276 | ...#......#.#..##..#.....#..... 277 | #.#............#.....##.#...... 278 | #..#......##...##..#...#.#..### 279 | #.....#...#..#..#....#..###.... 280 | ####..###....#..##....#..#..... 281 | ..##.#.....#.......##....#.#.#. 282 | ##..#.#.......#.#...##.#..#.#.. 283 | ..#.#.#.##.#.#.#...........#... 284 | ...#.##.....#....#..#.#..#..... 285 | ...#..#.........#..........#..# 286 | #...#..#.....#.#.#............. 287 | ##.#....##.##...#...#..#..#..#. 288 | ....####..##..##.#..#...##..... 289 | ##.....##.#.#...#.#.......###.. 290 | #..#.#....#......#.......##...# 291 | #.#...............#..#..#...... 292 | .....##...##..#........#......# 293 | .#..#............##......#....# 294 | ......#.#..#..##.#......#.....# 295 | ..#.#.............#...#......## 296 | ....#.#..#..#...##...#..##..... 297 | #.#.............#...#.#..#....# 298 | #..#..#.##....#....#...#....... 299 | ....#..#..........#.....##...#. 300 | ..#####.......#...#..........#. 301 | ....#......##.......#..##.#.#.# 302 | #...#.#.....#....#....##....... 303 | ..##.#.#..#.#...#.....##.....#. 304 | #.#..#....#............#..#.#.. 305 | ...#.##..##..#.#...###......#.# 306 | ......##.......#....#......###. 307 | ....#..##.......#..#.#....#..#. 308 | #............#..........##..#.# 309 | ..#.....#..#.#..##..#....##.#.. 310 | .....#.....#....##.#.#......#.# 311 | ...####.......###..#...#.#....# 312 | .#.##.....##.....##..#..#...... 313 | ...#..###..##.................. 314 | ##..##.....#.#..#..#.#........# 315 | .#.#........##.........#....#.. 316 | ........#......###....#...#.... 317 | ......#...........#...........# 318 | .#...###...#.........#.#...#..# 319 | .....#..........#......##....#. 320 | .#.#...#........##.......#.#... 321 | .....##.....##....#...#...#..#. 322 | ..#.....................##...## 323 | #..#.#......##...##..#......#.. 324 | -------------------------------------------------------------------------------- /day03b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let map: Vec<&[u8]> = include_str!("../input.txt") 3 | .lines() 4 | .map(|l| l.as_bytes()) 5 | .collect(); 6 | 7 | println!( 8 | "{}", 9 | (0..5) 10 | .map(|i| ((1 + i * 2) % 8, 1 + i / 4)) 11 | .map(|(xx, yy)| (0..) 12 | .map(|i| ((i + 1) * xx, (i + 1) * yy)) 13 | .take_while(|(_, y)| y < &map.len()) 14 | .filter(|(x, y)| map[*y][*x % map[0].len()] == b'#') 15 | .count()) 16 | .product::() 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /day04a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day04a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day04a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day04a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day04a/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | 3 | const REQ_FIELDS: [&'static str; 7] = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; 4 | 5 | pub fn main() { 6 | println!( 7 | "{}", 8 | include_str!("../input.txt") 9 | .split("\n\n") 10 | .map(|fields| fields 11 | .split_ascii_whitespace() 12 | .map(|field| field.split(':').next().unwrap()) 13 | .collect::>()) 14 | .filter(|passport| REQ_FIELDS.iter().all(|item| passport.contains(item))) 15 | .count(), 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /day04b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day04b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day04b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day04b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day04b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | use std::collections::HashMap; 4 | 5 | const REQ_FIELDS: [&'static str; 7] = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; 6 | const EYE_COLORS: [&'static str; 7] = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]; 7 | 8 | pub fn main() { 9 | println!( 10 | "{}", 11 | include_str!("../input.txt") 12 | .split("\n\n") 13 | .map(|fields| fields 14 | .split_ascii_whitespace() 15 | .map(|field| field.split_once(':').unwrap()) 16 | .collect::>()) 17 | .filter(|passport| REQ_FIELDS.iter().all(|item| passport.contains_key(item))) 18 | .filter(|passport| passport.iter().all(|(f, v)| validate_field(f, v))) 19 | .count(), 20 | ); 21 | } 22 | 23 | /// Validate a password field value. 24 | fn validate_field(field: &str, value: &str) -> bool { 25 | match field { 26 | "byr" => value.parse::().unwrap().wrapping_sub(1920) <= 82, 27 | "iyr" => value.parse::().unwrap().wrapping_sub(2010) <= 10, 28 | "eyr" => value.parse::().unwrap().wrapping_sub(2020) <= 10, 29 | "hgt" => { 30 | if value.ends_with("cm") && value.len() == 5 { 31 | value[0..3].parse::().unwrap().wrapping_sub(150) <= 43 32 | } else if value.ends_with("in") && value.len() == 4 { 33 | value[0..2].parse::().unwrap().wrapping_sub(59) <= 27 34 | } else { 35 | false 36 | } 37 | } 38 | "hcl" => value.len() == 7, 39 | "ecl" => EYE_COLORS.iter().any(|v| v == &value), 40 | "pid" => value.len() == 9, 41 | "cid" => true, 42 | _ => panic!("unknown field type"), 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /day05a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day05a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day05a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day05a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day05a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | println!( 3 | "{}", 4 | include_bytes!("../input.txt") 5 | .chunks(11) 6 | .map(|b| b[..10] 7 | .iter() 8 | .fold(0, |id, b| (id << 1) | (!b & 4) as usize >> 2)) 9 | .max() 10 | .unwrap(), 11 | ); 12 | } 13 | -------------------------------------------------------------------------------- /day05b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day05b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day05b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day05b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day05b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut ids = include_bytes!("../input.txt") 3 | .chunks(11) 4 | .map(|b| { 5 | b[..10] 6 | .iter() 7 | .fold(0, |id, b| (id << 1) | (!b & 4) as usize >> 2) 8 | }) 9 | .collect::>(); 10 | ids.sort_unstable(); 11 | 12 | println!( 13 | "{}", 14 | ids.windows(2).find(|n| n[0] == n[1] - 2).unwrap()[0] + 1, 15 | ); 16 | } 17 | -------------------------------------------------------------------------------- /day06a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day06a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day06a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day06a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day06a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | println!( 3 | "{}", 4 | include_str!("../input.txt") 5 | .split("\n\n") 6 | .map(|g| g 7 | .bytes() 8 | .filter(|b| b != &b'\n') 9 | .fold(0u32, |map, b| map | 1 << b - b'a') 10 | .count_ones()) 11 | .sum::() 12 | ); 13 | } 14 | -------------------------------------------------------------------------------- /day06b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day06b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day06b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day06b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day06b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | println!( 3 | "{}", 4 | include_str!("../input.txt") 5 | .split("\n\n") 6 | .map(|g| g 7 | .lines() 8 | .map(|l| l.bytes().fold(0, |map, b| map | 1 << b - b'a')) 9 | .fold(std::u32::MAX, |map, g| map & g) 10 | .count_ones()) 11 | .sum::() 12 | ); 13 | } 14 | -------------------------------------------------------------------------------- /day07a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day07a" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "lazy_static", 19 | "regex", 20 | ] 21 | 22 | [[package]] 23 | name = "lazy_static" 24 | version = "1.4.0" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 27 | 28 | [[package]] 29 | name = "memchr" 30 | version = "2.4.1" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 33 | 34 | [[package]] 35 | name = "regex" 36 | version = "1.5.4" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 39 | dependencies = [ 40 | "aho-corasick", 41 | "memchr", 42 | "regex-syntax", 43 | ] 44 | 45 | [[package]] 46 | name = "regex-syntax" 47 | version = "0.6.25" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 50 | -------------------------------------------------------------------------------- /day07a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day07a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | lazy_static = "1.4" 12 | regex = "1.4" 13 | -------------------------------------------------------------------------------- /day07a/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | 3 | lazy_static::lazy_static! { 4 | static ref RE_RULE: Regex = Regex::new(r#"^([a-z ]+) bags contain (.*)$"#).unwrap(); 5 | static ref RE_CONT: Regex = Regex::new(r#"\d ([a-z ]+) b"#).unwrap(); 6 | } 7 | 8 | pub fn main() { 9 | let rules: Vec<_> = include_str!("../input.txt") 10 | .lines() 11 | .map(parse_bag) 12 | .collect(); 13 | 14 | let (mut bags, mut cursor) = (vec!["shiny gold"], 0); 15 | 16 | while let Some(target) = bags.get(cursor) { 17 | let extra = rules 18 | .iter() 19 | .filter(|(color, cont)| cont.contains(target) && !bags.contains(color)) 20 | .map(|(color, _)| *color) 21 | .collect::>(); 22 | bags.extend_from_slice(&extra); 23 | cursor += 1; 24 | } 25 | 26 | println!("{}", bags.len() - 1); 27 | } 28 | 29 | /// Parse bag ruleset. 30 | #[inline(always)] 31 | fn parse_bag<'a>(rule: &'a str) -> (&'a str, Vec<&str>) { 32 | let captures = RE_RULE.captures(rule).unwrap(); 33 | ( 34 | captures.get(1).unwrap().as_str(), 35 | RE_CONT 36 | .captures_iter(captures.get(2).unwrap().as_str()) 37 | .map(|cond| cond.get(1).unwrap().as_str()) 38 | .collect(), 39 | ) 40 | } 41 | -------------------------------------------------------------------------------- /day07b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day07b" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "lazy_static", 19 | "regex", 20 | ] 21 | 22 | [[package]] 23 | name = "lazy_static" 24 | version = "1.4.0" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 27 | 28 | [[package]] 29 | name = "memchr" 30 | version = "2.4.1" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 33 | 34 | [[package]] 35 | name = "regex" 36 | version = "1.5.4" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 39 | dependencies = [ 40 | "aho-corasick", 41 | "memchr", 42 | "regex-syntax", 43 | ] 44 | 45 | [[package]] 46 | name = "regex-syntax" 47 | version = "0.6.25" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 50 | -------------------------------------------------------------------------------- /day07b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day07b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | lazy_static = "1.4" 12 | regex = "1.4" 13 | -------------------------------------------------------------------------------- /day07b/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use std::collections::HashMap; 3 | 4 | lazy_static::lazy_static! { 5 | static ref RE_RULE: Regex = Regex::new(r#"^([a-z ]+) bags contain (.*)$"#).unwrap(); 6 | static ref RE_CONT: Regex = Regex::new(r#"(\d) ([a-z ]+) b"#).unwrap(); 7 | } 8 | 9 | pub fn main() { 10 | let rules: HashMap<_, _> = include_str!("../input.txt") 11 | .lines() 12 | .map(parse_bag) 13 | .collect(); 14 | println!("{}", bags("shiny gold", &rules) - 1); 15 | } 16 | 17 | /// Parse bag ruleset. 18 | #[inline(always)] 19 | fn parse_bag<'a>(rule: &'a str) -> (&'a str, Vec<(&str, usize)>) { 20 | let captures = RE_RULE.captures(rule).unwrap(); 21 | ( 22 | captures.get(1).unwrap().as_str(), 23 | RE_CONT 24 | .captures_iter(captures.get(2).unwrap().as_str()) 25 | .map(|cond| (cond.get(2).unwrap().as_str(), cond[1].parse().unwrap())) 26 | .collect(), 27 | ) 28 | } 29 | 30 | /// Count bags in bags. 31 | fn bags(color: &str, rules: &HashMap<&str, Vec<(&str, usize)>>) -> usize { 32 | 1 + rules[color] 33 | .iter() 34 | .map(|(color, count)| bags(color, rules) * count) 35 | .sum::() 36 | } 37 | -------------------------------------------------------------------------------- /day08a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "atoi" 5 | version = "0.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" 8 | dependencies = [ 9 | "num-traits", 10 | ] 11 | 12 | [[package]] 13 | name = "autocfg" 14 | version = "1.0.1" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 17 | 18 | [[package]] 19 | name = "day08a" 20 | version = "0.1.0" 21 | dependencies = [ 22 | "atoi", 23 | ] 24 | 25 | [[package]] 26 | name = "num-traits" 27 | version = "0.2.14" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 30 | dependencies = [ 31 | "autocfg", 32 | ] 33 | -------------------------------------------------------------------------------- /day08a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day08a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | atoi = "0.4" 12 | -------------------------------------------------------------------------------- /day08a/input.txt: -------------------------------------------------------------------------------- 1 | nop +456 2 | nop +38 3 | acc +9 4 | jmp +153 5 | acc +15 6 | nop +560 7 | jmp +452 8 | acc +26 9 | acc +42 10 | jmp +376 11 | acc -5 12 | acc +12 13 | acc -5 14 | jmp +15 15 | jmp +1 16 | acc -9 17 | jmp +533 18 | acc +19 19 | acc +33 20 | acc +34 21 | jmp -6 22 | nop +404 23 | nop +140 24 | acc +0 25 | jmp +123 26 | acc +45 27 | acc +0 28 | jmp +496 29 | jmp +487 30 | acc +9 31 | acc +34 32 | jmp +484 33 | acc +0 34 | acc -14 35 | jmp +466 36 | acc +40 37 | acc +6 38 | acc +30 39 | jmp +444 40 | nop +386 41 | jmp +215 42 | acc +43 43 | acc +5 44 | nop -4 45 | jmp +535 46 | jmp -13 47 | acc +3 48 | acc +7 49 | acc +49 50 | acc -1 51 | jmp +245 52 | acc +9 53 | acc +31 54 | nop +142 55 | jmp +554 56 | acc +3 57 | jmp +493 58 | nop +399 59 | jmp +232 60 | acc -16 61 | acc +33 62 | jmp +410 63 | acc +33 64 | acc +5 65 | acc -17 66 | jmp +272 67 | acc -3 68 | acc +37 69 | jmp +181 70 | jmp -12 71 | nop +344 72 | acc +5 73 | acc -16 74 | jmp +290 75 | nop +530 76 | acc +15 77 | acc +3 78 | jmp +343 79 | acc +2 80 | acc +19 81 | jmp +298 82 | acc +43 83 | acc +25 84 | acc -19 85 | jmp +439 86 | acc +43 87 | acc +45 88 | acc +20 89 | jmp +355 90 | acc +13 91 | acc +24 92 | acc -15 93 | nop +396 94 | jmp +215 95 | acc -7 96 | acc +17 97 | jmp +441 98 | acc -8 99 | acc -19 100 | jmp +505 101 | jmp +282 102 | acc -17 103 | acc -8 104 | acc +20 105 | jmp +359 106 | acc +26 107 | acc +14 108 | acc +47 109 | acc +3 110 | jmp +298 111 | acc +31 112 | nop +205 113 | acc +0 114 | acc +7 115 | jmp +389 116 | acc -5 117 | acc +47 118 | jmp +94 119 | acc -13 120 | jmp +358 121 | acc -13 122 | jmp +134 123 | acc +8 124 | acc -19 125 | jmp +312 126 | acc +43 127 | acc +17 128 | jmp +97 129 | jmp +48 130 | nop +253 131 | acc +48 132 | acc -7 133 | acc -2 134 | jmp +23 135 | acc +26 136 | acc +14 137 | acc -14 138 | acc +17 139 | jmp +18 140 | acc +14 141 | acc +8 142 | jmp +341 143 | acc +35 144 | jmp +227 145 | acc +15 146 | acc -7 147 | jmp -95 148 | acc -19 149 | jmp -59 150 | jmp -31 151 | acc -6 152 | acc -4 153 | acc +24 154 | jmp +84 155 | acc -15 156 | jmp +82 157 | nop +74 158 | acc +8 159 | acc +9 160 | acc +13 161 | jmp +194 162 | jmp +376 163 | acc +34 164 | nop -16 165 | jmp -90 166 | acc +4 167 | acc +43 168 | nop +215 169 | jmp -147 170 | acc +0 171 | acc +11 172 | acc -15 173 | acc +23 174 | jmp +130 175 | acc +40 176 | jmp +106 177 | acc -4 178 | acc -18 179 | acc +18 180 | nop +329 181 | jmp +230 182 | acc +19 183 | nop +172 184 | acc +43 185 | jmp +304 186 | acc +44 187 | nop +213 188 | nop +195 189 | acc +6 190 | jmp -79 191 | acc +41 192 | acc -11 193 | acc +18 194 | acc -9 195 | jmp -25 196 | acc +27 197 | acc -6 198 | acc +31 199 | jmp -56 200 | acc +5 201 | acc +12 202 | acc +32 203 | acc +34 204 | jmp -189 205 | acc +32 206 | acc +5 207 | acc -16 208 | jmp +301 209 | nop +108 210 | nop -108 211 | jmp -141 212 | acc -12 213 | jmp +273 214 | acc +3 215 | jmp +140 216 | acc +7 217 | acc -11 218 | acc -17 219 | nop +194 220 | jmp -122 221 | acc -14 222 | nop +186 223 | acc +24 224 | jmp +277 225 | nop +341 226 | acc +18 227 | jmp -64 228 | acc +45 229 | acc +42 230 | jmp +52 231 | acc +39 232 | nop +91 233 | nop -8 234 | jmp +217 235 | acc +44 236 | acc +15 237 | jmp +72 238 | acc +24 239 | jmp -231 240 | acc -16 241 | nop +55 242 | nop +262 243 | acc +40 244 | jmp +234 245 | jmp -14 246 | acc +31 247 | nop -177 248 | acc +40 249 | jmp +343 250 | acc -8 251 | jmp -169 252 | acc +30 253 | acc +12 254 | acc -11 255 | jmp +41 256 | acc +9 257 | acc -9 258 | jmp +65 259 | acc +38 260 | acc +14 261 | jmp +335 262 | acc -19 263 | acc +38 264 | acc +16 265 | acc -11 266 | jmp +230 267 | jmp -71 268 | acc +48 269 | acc -13 270 | nop -255 271 | jmp +1 272 | jmp -220 273 | acc +2 274 | jmp +157 275 | jmp -105 276 | acc -16 277 | acc -5 278 | jmp -196 279 | acc +30 280 | jmp +139 281 | jmp +83 282 | acc -3 283 | acc -12 284 | jmp +254 285 | jmp -60 286 | acc +33 287 | jmp -37 288 | acc +17 289 | acc -14 290 | jmp +93 291 | nop +178 292 | acc +38 293 | acc +47 294 | jmp -89 295 | jmp +271 296 | acc +43 297 | acc +32 298 | jmp -240 299 | acc +26 300 | acc +32 301 | acc +30 302 | nop +284 303 | jmp +169 304 | acc -7 305 | acc +37 306 | jmp +102 307 | acc +4 308 | jmp +86 309 | jmp -123 310 | acc +0 311 | acc -14 312 | acc +18 313 | jmp +1 314 | jmp -5 315 | jmp -36 316 | jmp +148 317 | acc -17 318 | acc -14 319 | acc +28 320 | acc +15 321 | jmp +79 322 | jmp -289 323 | acc +42 324 | acc -5 325 | acc +13 326 | jmp +240 327 | acc -10 328 | acc -18 329 | acc -16 330 | jmp +103 331 | acc +21 332 | jmp +32 333 | nop +118 334 | acc +22 335 | acc -16 336 | acc +15 337 | jmp -186 338 | acc -2 339 | acc -14 340 | acc +22 341 | acc +16 342 | jmp +73 343 | acc -6 344 | jmp -225 345 | acc -18 346 | nop +113 347 | acc +50 348 | acc -6 349 | jmp +181 350 | acc +41 351 | jmp +1 352 | nop +92 353 | acc +23 354 | jmp +190 355 | acc +39 356 | acc +0 357 | acc +33 358 | jmp +111 359 | nop -63 360 | nop -81 361 | acc +9 362 | acc +35 363 | jmp +50 364 | acc +11 365 | jmp -295 366 | nop +230 367 | acc +34 368 | acc +12 369 | acc +47 370 | jmp +126 371 | acc +0 372 | nop -1 373 | acc +19 374 | acc -16 375 | jmp -360 376 | acc +29 377 | acc -2 378 | jmp -110 379 | acc +2 380 | acc +50 381 | jmp -36 382 | jmp -107 383 | jmp +178 384 | acc -11 385 | jmp +181 386 | nop +115 387 | nop +186 388 | jmp +95 389 | jmp +1 390 | nop +148 391 | acc +2 392 | acc +49 393 | jmp +173 394 | acc +38 395 | jmp +178 396 | acc +28 397 | acc +6 398 | acc +15 399 | jmp +110 400 | acc +49 401 | nop +100 402 | jmp +57 403 | acc +45 404 | nop +65 405 | acc +43 406 | acc +12 407 | jmp -272 408 | jmp -260 409 | nop +100 410 | jmp -224 411 | jmp +142 412 | jmp +52 413 | jmp -34 414 | jmp -110 415 | acc +35 416 | nop -112 417 | jmp +16 418 | jmp -18 419 | jmp -157 420 | jmp +81 421 | acc +1 422 | jmp -107 423 | acc +16 424 | acc +23 425 | jmp -255 426 | acc +22 427 | jmp +42 428 | nop +168 429 | acc +41 430 | jmp -311 431 | jmp -163 432 | jmp +118 433 | nop +4 434 | acc +18 435 | jmp +54 436 | jmp -414 437 | nop -181 438 | acc +10 439 | acc +23 440 | jmp -321 441 | nop -322 442 | acc -9 443 | jmp +101 444 | nop -7 445 | acc +35 446 | acc +46 447 | jmp -312 448 | nop +64 449 | nop -386 450 | jmp -280 451 | acc +16 452 | jmp -156 453 | acc +13 454 | nop -131 455 | jmp +1 456 | jmp -416 457 | jmp +15 458 | jmp -94 459 | jmp -330 460 | nop +93 461 | nop -205 462 | acc +48 463 | jmp -19 464 | jmp -70 465 | nop +21 466 | acc -5 467 | acc +19 468 | jmp +62 469 | acc +22 470 | jmp -448 471 | jmp -77 472 | acc +26 473 | acc -2 474 | jmp +70 475 | acc -2 476 | acc +21 477 | jmp -195 478 | nop -114 479 | jmp +107 480 | acc +37 481 | acc +6 482 | jmp -436 483 | acc +48 484 | jmp +96 485 | jmp -121 486 | acc +0 487 | jmp -74 488 | jmp +1 489 | acc +27 490 | acc +2 491 | jmp -279 492 | acc +7 493 | acc +0 494 | jmp +1 495 | jmp -413 496 | acc +6 497 | jmp -180 498 | acc +18 499 | acc +10 500 | jmp -437 501 | jmp -338 502 | nop -456 503 | jmp -463 504 | acc +1 505 | nop -54 506 | jmp -168 507 | acc +27 508 | jmp -479 509 | acc +42 510 | jmp -408 511 | jmp +85 512 | acc -16 513 | acc +24 514 | jmp -391 515 | jmp -206 516 | nop +8 517 | jmp +1 518 | acc +38 519 | nop -473 520 | jmp -94 521 | acc +10 522 | acc -14 523 | jmp -425 524 | acc +17 525 | nop -208 526 | acc +39 527 | jmp -265 528 | acc +3 529 | jmp -284 530 | acc +19 531 | acc +5 532 | nop -111 533 | acc +22 534 | jmp -309 535 | acc +12 536 | acc +39 537 | jmp -151 538 | acc +33 539 | acc -14 540 | jmp -450 541 | acc +16 542 | nop +50 543 | jmp -188 544 | acc -13 545 | acc +15 546 | acc +4 547 | jmp -484 548 | acc +27 549 | jmp -98 550 | acc +34 551 | jmp -120 552 | jmp -537 553 | acc +43 554 | acc -8 555 | acc -6 556 | jmp -405 557 | acc -8 558 | nop -179 559 | acc -11 560 | jmp -264 561 | acc +24 562 | jmp -280 563 | acc -6 564 | acc +1 565 | jmp -353 566 | acc -18 567 | jmp -58 568 | acc +1 569 | acc -7 570 | acc -2 571 | acc +44 572 | jmp -115 573 | nop -328 574 | acc +27 575 | acc +2 576 | jmp +20 577 | acc +14 578 | acc +34 579 | jmp -460 580 | nop -445 581 | acc -9 582 | acc +24 583 | acc -11 584 | jmp -72 585 | jmp -434 586 | jmp -370 587 | acc +35 588 | acc +43 589 | acc +45 590 | acc +44 591 | jmp -287 592 | jmp -546 593 | nop -474 594 | acc -6 595 | jmp -357 596 | nop -163 597 | nop -218 598 | nop -342 599 | jmp -570 600 | acc +44 601 | acc +4 602 | acc +35 603 | acc +6 604 | jmp -541 605 | jmp -274 606 | acc +48 607 | acc -18 608 | jmp -171 609 | acc -13 610 | acc -14 611 | acc +25 612 | acc +26 613 | jmp +1 -------------------------------------------------------------------------------- /day08a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let program: Vec<(&[u8], isize)> = include_bytes!("../input.txt") 3 | .split(|b| b == &b'\n') 4 | .map(|ins| (&ins[0..3], atoi::atoi(&ins[4..]).unwrap())) 5 | .collect(); 6 | 7 | let (mut visited, mut pc, mut acc) = (vec![], 0, 0); 8 | while !visited.contains(&pc) { 9 | visited.push(pc); 10 | match program[pc] { 11 | (b"acc", val) => acc += val, 12 | (b"jmp", val) => pc += val as usize - 1, 13 | _ => {} 14 | } 15 | pc += 1; 16 | } 17 | 18 | println!("{}", acc); 19 | } 20 | -------------------------------------------------------------------------------- /day08b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "atoi" 5 | version = "0.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" 8 | dependencies = [ 9 | "num-traits", 10 | ] 11 | 12 | [[package]] 13 | name = "autocfg" 14 | version = "1.0.1" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 17 | 18 | [[package]] 19 | name = "day08b" 20 | version = "0.1.0" 21 | dependencies = [ 22 | "atoi", 23 | ] 24 | 25 | [[package]] 26 | name = "num-traits" 27 | version = "0.2.14" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 30 | dependencies = [ 31 | "autocfg", 32 | ] 33 | -------------------------------------------------------------------------------- /day08b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day08b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | atoi = "0.4" 12 | -------------------------------------------------------------------------------- /day08b/input.txt: -------------------------------------------------------------------------------- 1 | nop +456 2 | nop +38 3 | acc +9 4 | jmp +153 5 | acc +15 6 | nop +560 7 | jmp +452 8 | acc +26 9 | acc +42 10 | jmp +376 11 | acc -5 12 | acc +12 13 | acc -5 14 | jmp +15 15 | jmp +1 16 | acc -9 17 | jmp +533 18 | acc +19 19 | acc +33 20 | acc +34 21 | jmp -6 22 | nop +404 23 | nop +140 24 | acc +0 25 | jmp +123 26 | acc +45 27 | acc +0 28 | jmp +496 29 | jmp +487 30 | acc +9 31 | acc +34 32 | jmp +484 33 | acc +0 34 | acc -14 35 | jmp +466 36 | acc +40 37 | acc +6 38 | acc +30 39 | jmp +444 40 | nop +386 41 | jmp +215 42 | acc +43 43 | acc +5 44 | nop -4 45 | jmp +535 46 | jmp -13 47 | acc +3 48 | acc +7 49 | acc +49 50 | acc -1 51 | jmp +245 52 | acc +9 53 | acc +31 54 | nop +142 55 | jmp +554 56 | acc +3 57 | jmp +493 58 | nop +399 59 | jmp +232 60 | acc -16 61 | acc +33 62 | jmp +410 63 | acc +33 64 | acc +5 65 | acc -17 66 | jmp +272 67 | acc -3 68 | acc +37 69 | jmp +181 70 | jmp -12 71 | nop +344 72 | acc +5 73 | acc -16 74 | jmp +290 75 | nop +530 76 | acc +15 77 | acc +3 78 | jmp +343 79 | acc +2 80 | acc +19 81 | jmp +298 82 | acc +43 83 | acc +25 84 | acc -19 85 | jmp +439 86 | acc +43 87 | acc +45 88 | acc +20 89 | jmp +355 90 | acc +13 91 | acc +24 92 | acc -15 93 | nop +396 94 | jmp +215 95 | acc -7 96 | acc +17 97 | jmp +441 98 | acc -8 99 | acc -19 100 | jmp +505 101 | jmp +282 102 | acc -17 103 | acc -8 104 | acc +20 105 | jmp +359 106 | acc +26 107 | acc +14 108 | acc +47 109 | acc +3 110 | jmp +298 111 | acc +31 112 | nop +205 113 | acc +0 114 | acc +7 115 | jmp +389 116 | acc -5 117 | acc +47 118 | jmp +94 119 | acc -13 120 | jmp +358 121 | acc -13 122 | jmp +134 123 | acc +8 124 | acc -19 125 | jmp +312 126 | acc +43 127 | acc +17 128 | jmp +97 129 | jmp +48 130 | nop +253 131 | acc +48 132 | acc -7 133 | acc -2 134 | jmp +23 135 | acc +26 136 | acc +14 137 | acc -14 138 | acc +17 139 | jmp +18 140 | acc +14 141 | acc +8 142 | jmp +341 143 | acc +35 144 | jmp +227 145 | acc +15 146 | acc -7 147 | jmp -95 148 | acc -19 149 | jmp -59 150 | jmp -31 151 | acc -6 152 | acc -4 153 | acc +24 154 | jmp +84 155 | acc -15 156 | jmp +82 157 | nop +74 158 | acc +8 159 | acc +9 160 | acc +13 161 | jmp +194 162 | jmp +376 163 | acc +34 164 | nop -16 165 | jmp -90 166 | acc +4 167 | acc +43 168 | nop +215 169 | jmp -147 170 | acc +0 171 | acc +11 172 | acc -15 173 | acc +23 174 | jmp +130 175 | acc +40 176 | jmp +106 177 | acc -4 178 | acc -18 179 | acc +18 180 | nop +329 181 | jmp +230 182 | acc +19 183 | nop +172 184 | acc +43 185 | jmp +304 186 | acc +44 187 | nop +213 188 | nop +195 189 | acc +6 190 | jmp -79 191 | acc +41 192 | acc -11 193 | acc +18 194 | acc -9 195 | jmp -25 196 | acc +27 197 | acc -6 198 | acc +31 199 | jmp -56 200 | acc +5 201 | acc +12 202 | acc +32 203 | acc +34 204 | jmp -189 205 | acc +32 206 | acc +5 207 | acc -16 208 | jmp +301 209 | nop +108 210 | nop -108 211 | jmp -141 212 | acc -12 213 | jmp +273 214 | acc +3 215 | jmp +140 216 | acc +7 217 | acc -11 218 | acc -17 219 | nop +194 220 | jmp -122 221 | acc -14 222 | nop +186 223 | acc +24 224 | jmp +277 225 | nop +341 226 | acc +18 227 | jmp -64 228 | acc +45 229 | acc +42 230 | jmp +52 231 | acc +39 232 | nop +91 233 | nop -8 234 | jmp +217 235 | acc +44 236 | acc +15 237 | jmp +72 238 | acc +24 239 | jmp -231 240 | acc -16 241 | nop +55 242 | nop +262 243 | acc +40 244 | jmp +234 245 | jmp -14 246 | acc +31 247 | nop -177 248 | acc +40 249 | jmp +343 250 | acc -8 251 | jmp -169 252 | acc +30 253 | acc +12 254 | acc -11 255 | jmp +41 256 | acc +9 257 | acc -9 258 | jmp +65 259 | acc +38 260 | acc +14 261 | jmp +335 262 | acc -19 263 | acc +38 264 | acc +16 265 | acc -11 266 | jmp +230 267 | jmp -71 268 | acc +48 269 | acc -13 270 | nop -255 271 | jmp +1 272 | jmp -220 273 | acc +2 274 | jmp +157 275 | jmp -105 276 | acc -16 277 | acc -5 278 | jmp -196 279 | acc +30 280 | jmp +139 281 | jmp +83 282 | acc -3 283 | acc -12 284 | jmp +254 285 | jmp -60 286 | acc +33 287 | jmp -37 288 | acc +17 289 | acc -14 290 | jmp +93 291 | nop +178 292 | acc +38 293 | acc +47 294 | jmp -89 295 | jmp +271 296 | acc +43 297 | acc +32 298 | jmp -240 299 | acc +26 300 | acc +32 301 | acc +30 302 | nop +284 303 | jmp +169 304 | acc -7 305 | acc +37 306 | jmp +102 307 | acc +4 308 | jmp +86 309 | jmp -123 310 | acc +0 311 | acc -14 312 | acc +18 313 | jmp +1 314 | jmp -5 315 | jmp -36 316 | jmp +148 317 | acc -17 318 | acc -14 319 | acc +28 320 | acc +15 321 | jmp +79 322 | jmp -289 323 | acc +42 324 | acc -5 325 | acc +13 326 | jmp +240 327 | acc -10 328 | acc -18 329 | acc -16 330 | jmp +103 331 | acc +21 332 | jmp +32 333 | nop +118 334 | acc +22 335 | acc -16 336 | acc +15 337 | jmp -186 338 | acc -2 339 | acc -14 340 | acc +22 341 | acc +16 342 | jmp +73 343 | acc -6 344 | jmp -225 345 | acc -18 346 | nop +113 347 | acc +50 348 | acc -6 349 | jmp +181 350 | acc +41 351 | jmp +1 352 | nop +92 353 | acc +23 354 | jmp +190 355 | acc +39 356 | acc +0 357 | acc +33 358 | jmp +111 359 | nop -63 360 | nop -81 361 | acc +9 362 | acc +35 363 | jmp +50 364 | acc +11 365 | jmp -295 366 | nop +230 367 | acc +34 368 | acc +12 369 | acc +47 370 | jmp +126 371 | acc +0 372 | nop -1 373 | acc +19 374 | acc -16 375 | jmp -360 376 | acc +29 377 | acc -2 378 | jmp -110 379 | acc +2 380 | acc +50 381 | jmp -36 382 | jmp -107 383 | jmp +178 384 | acc -11 385 | jmp +181 386 | nop +115 387 | nop +186 388 | jmp +95 389 | jmp +1 390 | nop +148 391 | acc +2 392 | acc +49 393 | jmp +173 394 | acc +38 395 | jmp +178 396 | acc +28 397 | acc +6 398 | acc +15 399 | jmp +110 400 | acc +49 401 | nop +100 402 | jmp +57 403 | acc +45 404 | nop +65 405 | acc +43 406 | acc +12 407 | jmp -272 408 | jmp -260 409 | nop +100 410 | jmp -224 411 | jmp +142 412 | jmp +52 413 | jmp -34 414 | jmp -110 415 | acc +35 416 | nop -112 417 | jmp +16 418 | jmp -18 419 | jmp -157 420 | jmp +81 421 | acc +1 422 | jmp -107 423 | acc +16 424 | acc +23 425 | jmp -255 426 | acc +22 427 | jmp +42 428 | nop +168 429 | acc +41 430 | jmp -311 431 | jmp -163 432 | jmp +118 433 | nop +4 434 | acc +18 435 | jmp +54 436 | jmp -414 437 | nop -181 438 | acc +10 439 | acc +23 440 | jmp -321 441 | nop -322 442 | acc -9 443 | jmp +101 444 | nop -7 445 | acc +35 446 | acc +46 447 | jmp -312 448 | nop +64 449 | nop -386 450 | jmp -280 451 | acc +16 452 | jmp -156 453 | acc +13 454 | nop -131 455 | jmp +1 456 | jmp -416 457 | jmp +15 458 | jmp -94 459 | jmp -330 460 | nop +93 461 | nop -205 462 | acc +48 463 | jmp -19 464 | jmp -70 465 | nop +21 466 | acc -5 467 | acc +19 468 | jmp +62 469 | acc +22 470 | jmp -448 471 | jmp -77 472 | acc +26 473 | acc -2 474 | jmp +70 475 | acc -2 476 | acc +21 477 | jmp -195 478 | nop -114 479 | jmp +107 480 | acc +37 481 | acc +6 482 | jmp -436 483 | acc +48 484 | jmp +96 485 | jmp -121 486 | acc +0 487 | jmp -74 488 | jmp +1 489 | acc +27 490 | acc +2 491 | jmp -279 492 | acc +7 493 | acc +0 494 | jmp +1 495 | jmp -413 496 | acc +6 497 | jmp -180 498 | acc +18 499 | acc +10 500 | jmp -437 501 | jmp -338 502 | nop -456 503 | jmp -463 504 | acc +1 505 | nop -54 506 | jmp -168 507 | acc +27 508 | jmp -479 509 | acc +42 510 | jmp -408 511 | jmp +85 512 | acc -16 513 | acc +24 514 | jmp -391 515 | jmp -206 516 | nop +8 517 | jmp +1 518 | acc +38 519 | nop -473 520 | jmp -94 521 | acc +10 522 | acc -14 523 | jmp -425 524 | acc +17 525 | nop -208 526 | acc +39 527 | jmp -265 528 | acc +3 529 | jmp -284 530 | acc +19 531 | acc +5 532 | nop -111 533 | acc +22 534 | jmp -309 535 | acc +12 536 | acc +39 537 | jmp -151 538 | acc +33 539 | acc -14 540 | jmp -450 541 | acc +16 542 | nop +50 543 | jmp -188 544 | acc -13 545 | acc +15 546 | acc +4 547 | jmp -484 548 | acc +27 549 | jmp -98 550 | acc +34 551 | jmp -120 552 | jmp -537 553 | acc +43 554 | acc -8 555 | acc -6 556 | jmp -405 557 | acc -8 558 | nop -179 559 | acc -11 560 | jmp -264 561 | acc +24 562 | jmp -280 563 | acc -6 564 | acc +1 565 | jmp -353 566 | acc -18 567 | jmp -58 568 | acc +1 569 | acc -7 570 | acc -2 571 | acc +44 572 | jmp -115 573 | nop -328 574 | acc +27 575 | acc +2 576 | jmp +20 577 | acc +14 578 | acc +34 579 | jmp -460 580 | nop -445 581 | acc -9 582 | acc +24 583 | acc -11 584 | jmp -72 585 | jmp -434 586 | jmp -370 587 | acc +35 588 | acc +43 589 | acc +45 590 | acc +44 591 | jmp -287 592 | jmp -546 593 | nop -474 594 | acc -6 595 | jmp -357 596 | nop -163 597 | nop -218 598 | nop -342 599 | jmp -570 600 | acc +44 601 | acc +4 602 | acc +35 603 | acc +6 604 | jmp -541 605 | jmp -274 606 | acc +48 607 | acc -18 608 | jmp -171 609 | acc -13 610 | acc -14 611 | acc +25 612 | acc +26 613 | jmp +1 -------------------------------------------------------------------------------- /day08b/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::mem; 2 | 3 | pub fn main() { 4 | let mut program: Vec<(&[u8], i32)> = include_bytes!("../input.txt") 5 | .split(|b| b == &b'\n') 6 | .map(|ins| (&ins[0..3], atoi::atoi(&ins[4..]).unwrap())) 7 | .collect(); 8 | 9 | let mut other_op: &[u8] = b"nop"; 10 | for pc in run(&program).0 { 11 | if program[pc].0 == b"jmp" { 12 | mem::swap(&mut program[pc].0, &mut other_op); 13 | if let (_, Some(acc)) = run(&program) { 14 | println!("{}", acc); 15 | break; 16 | } 17 | mem::swap(&mut program[pc].0, &mut other_op); 18 | } 19 | } 20 | } 21 | 22 | /// Run program. Return `Some(acc)` on success, `None` on infinite loop. 23 | #[inline(always)] 24 | fn run(program: &[(&[u8], i32)]) -> (Vec, Option) { 25 | let (mut visited, mut pc, mut acc) = (Vec::with_capacity(64), 0, 0); 26 | while !visited.contains(&pc) { 27 | visited.push(pc); 28 | match program.get(pc) { 29 | Some((b"acc", val)) => acc += val, 30 | Some((b"jmp", val)) => pc += *val as usize - 1, 31 | None => return (visited, Some(acc)), 32 | _ => {} 33 | } 34 | pc += 1; 35 | } 36 | (visited, None) 37 | } 38 | -------------------------------------------------------------------------------- /day09a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day09a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day09a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day09a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day09a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let nums: Vec = include_str!("../input.txt") 3 | .lines() 4 | .map(|n| n.parse().unwrap()) 5 | .collect(); 6 | 7 | println!( 8 | "{}", 9 | nums.windows(26) 10 | .find(|set| { 11 | for i in 0..24 { 12 | for j in (i + 1)..25 { 13 | if set[i] + set[j] == set[25] { 14 | return false; 15 | } 16 | } 17 | } 18 | true 19 | }) 20 | .unwrap()[25] 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /day09b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day09b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day09b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day09b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day09b/src/main.rs: -------------------------------------------------------------------------------- 1 | const FIND: usize = 22406676; 2 | 3 | pub fn main() { 4 | let nums: Vec = include_str!("../input.txt") 5 | .lines() 6 | .map(|n| n.parse().unwrap()) 7 | .collect(); 8 | 9 | let (mut tail, mut head, mut tot) = (0, 1, nums[0] + nums[1]); 10 | while tot != FIND { 11 | while tot < FIND { 12 | head += 1; 13 | tot += nums[head]; 14 | } 15 | while tot > FIND { 16 | tot -= nums[tail]; 17 | tail += 1; 18 | } 19 | } 20 | 21 | let set = &nums[tail..=head]; 22 | println!("{}", set.iter().min().unwrap() + set.iter().max().unwrap()); 23 | } 24 | -------------------------------------------------------------------------------- /day10a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day10a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day10a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day10a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day10a/input.txt: -------------------------------------------------------------------------------- 1 | 160 2 | 34 3 | 123 4 | 159 5 | 148 6 | 93 7 | 165 8 | 56 9 | 179 10 | 103 11 | 171 12 | 44 13 | 110 14 | 170 15 | 147 16 | 98 17 | 25 18 | 37 19 | 137 20 | 71 21 | 5 22 | 6 23 | 121 24 | 28 25 | 19 26 | 134 27 | 18 28 | 7 29 | 66 30 | 90 31 | 88 32 | 181 33 | 89 34 | 41 35 | 156 36 | 46 37 | 8 38 | 61 39 | 124 40 | 9 41 | 161 42 | 72 43 | 13 44 | 172 45 | 111 46 | 59 47 | 105 48 | 51 49 | 109 50 | 27 51 | 152 52 | 117 53 | 52 54 | 68 55 | 95 56 | 164 57 | 116 58 | 75 59 | 78 60 | 180 61 | 81 62 | 47 63 | 104 64 | 12 65 | 133 66 | 175 67 | 16 68 | 149 69 | 135 70 | 99 71 | 112 72 | 38 73 | 67 74 | 53 75 | 153 76 | 2 77 | 136 78 | 113 79 | 17 80 | 145 81 | 106 82 | 31 83 | 45 84 | 169 85 | 146 86 | 168 87 | 26 88 | 36 89 | 118 90 | 62 91 | 65 92 | 142 93 | 130 94 | 1 95 | 140 96 | 84 97 | 94 98 | 141 99 | 122 100 | 22 101 | 48 102 | 102 103 | 60 104 | 178 105 | 127 106 | 73 107 | 74 108 | 87 109 | 182 110 | 35 111 | -------------------------------------------------------------------------------- /day10a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut nums: Vec = include_str!("../input.txt") 3 | .lines() 4 | .map(|n| n.parse().unwrap()) 5 | .collect(); 6 | nums.sort_unstable(); 7 | 8 | let (one, three) = nums 9 | .windows(2) 10 | .fold((1, 1), |(one, three), val| match val[1] - val[0] { 11 | 1 => (one + 1, three), 12 | 3 => (one, three + 1), 13 | _ => unreachable!(), 14 | }); 15 | 16 | println!("{}", one * three); 17 | } 18 | -------------------------------------------------------------------------------- /day10b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day10b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day10b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day10b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day10b/input.txt: -------------------------------------------------------------------------------- 1 | 160 2 | 34 3 | 123 4 | 159 5 | 148 6 | 93 7 | 165 8 | 56 9 | 179 10 | 103 11 | 171 12 | 44 13 | 110 14 | 170 15 | 147 16 | 98 17 | 25 18 | 37 19 | 137 20 | 71 21 | 5 22 | 6 23 | 121 24 | 28 25 | 19 26 | 134 27 | 18 28 | 7 29 | 66 30 | 90 31 | 88 32 | 181 33 | 89 34 | 41 35 | 156 36 | 46 37 | 8 38 | 61 39 | 124 40 | 9 41 | 161 42 | 72 43 | 13 44 | 172 45 | 111 46 | 59 47 | 105 48 | 51 49 | 109 50 | 27 51 | 152 52 | 117 53 | 52 54 | 68 55 | 95 56 | 164 57 | 116 58 | 75 59 | 78 60 | 180 61 | 81 62 | 47 63 | 104 64 | 12 65 | 133 66 | 175 67 | 16 68 | 149 69 | 135 70 | 99 71 | 112 72 | 38 73 | 67 74 | 53 75 | 153 76 | 2 77 | 136 78 | 113 79 | 17 80 | 145 81 | 106 82 | 31 83 | 45 84 | 169 85 | 146 86 | 168 87 | 26 88 | 36 89 | 118 90 | 62 91 | 65 92 | 142 93 | 130 94 | 1 95 | 140 96 | 84 97 | 94 98 | 141 99 | 122 100 | 22 101 | 48 102 | 102 103 | 60 104 | 178 105 | 127 106 | 73 107 | 74 108 | 87 109 | 182 110 | 35 111 | -------------------------------------------------------------------------------- /day10b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut nums: Vec = include_str!("../input.txt") 3 | .lines() 4 | .map(|n| n.parse().unwrap()) 5 | .collect(); 6 | nums.sort_unstable(); 7 | 8 | println!( 9 | "{}", 10 | 2 * nums 11 | .windows(2) 12 | .collect::>() 13 | .split(|n| n[1] - n[0] == 3) 14 | .map(|n| match n.len() { 15 | 4 => 7, 16 | 3 => 4, 17 | 2 => 2, 18 | _ => 1, 19 | }) 20 | .product::() 21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /day11a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day11a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day11a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day11a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day11a/input.txt: -------------------------------------------------------------------------------- 1 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL 2 | LLLLLLLLLL.LLL.LLLLL.LLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL..LLLLL.LLLLLLLLLLLLLLLL 3 | LL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 4 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLL..LLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL 5 | LLLLL.LLLLLLLLLLLLLL.LLL.LL.LLLL.LLL.LLLLLL.LLLLL.LLLLLLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 6 | LLLLL.LLLL..LLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLL.LL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 7 | .......L...L.L...L......L..LLL..L.............L..L....L.LL.......LL......L..LL..L..L.L.LL..L.L.... 8 | LLLLL..LLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL..LLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLLLLL.LLL 9 | LLLLLLLLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLL.LLL..LL.LLLLLLLLLL.LLLL.LLLLLLLLLLLLL.LL.LLLLLL.LLLLLL 10 | L.LLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLL..LLLLLLL.LLLLLLLL..LLLLLLLL.LLLL.LLL.LL.LLLLLL.L.LLLLL.L.LLLLLL 11 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL 12 | LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 13 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLL.L.LLLLLLL.L.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 14 | .....LL..L....LL.L....L....L.....L.L...L......LLL..L.L....L..LLL...L.L..L.L..LL...L..L.L.....L.L.L 15 | LLLLL.LLL..LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL 16 | LLLLL.LLLL.LLLL.LL.L.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 17 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLLLL.LLL.LL.LLLLLLLLLLLLLLLL 18 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LL.LLLL.LLLLLL.LL.LLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 19 | .LLLL.LLLL.LLLLLLLLL.L.LLL..LLLLLLL.LLLLLLLLLLLLL.LLL.LLLLLLLLLLLLL.LLLL.LLL.LLLL.LLLLLLLLL.LLLLLL 20 | LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL...LLLLLL.LL.LLLLLL.LLL.LLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 21 | .......L.L...L..L..L.LL.LL.L....L.L..LL..L.L.L.LL...LLL...LLLL..LLLL.......L...L....LL.....L.LL... 22 | LLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 23 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLL 24 | LLLLLLLLLL.LLLLLLLLL.LL.LLL.LLLLL.L.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLL.LLL.LLLLLLLLL.LLLLLL 25 | LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLLLLL.LLLLLLLL.LL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 26 | LLLLL..LLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLLL.LL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 27 | L...LL.LL.L....LL..L.L..L..L.L.LL.L.....L..LL.......L..LLL..L...LL......LL..LL..L.L.LL..LL.L...... 28 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLL.LLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 29 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL..LLLLL.LLLLLLLLL.LLLLLL 30 | LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 31 | LLLLL.LLLLLLLLLLLLLL.LLLL.L.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLL..LLLLLL 32 | .LLLL..L...L..LL...L...L.L.L..LLL.L....L..L..L.....L.L..............LL.L....L..L......LL..LL..L... 33 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LL.LLLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 34 | LLLLL.LLLLLLLLLLL.LL..LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 35 | LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 36 | LLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 37 | LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLL.LLL.LLLLLLLL.LLLL.L.LLLLLLLLLLL.LLLLLLLLLLLLLLLL 38 | LLLLL.L.LLLLLLLLLLLLLLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLLL.LLLLL 39 | LLLLL.LLLL.LL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL..LLLLLLLLL.L.LLLLLLLLLLLLLLL.LLLLLLL 40 | L.LLL.LL.L.L...L.L....L.....L.....LL..........LL...L..L.L....L..L.L....L.L.LL.L.......LL.....L.L.. 41 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLL.LLLLLLLL.LLLL.LLLLLL..LLLLL.LLLLLLLLL.LLLLLL 42 | LLLLL.LLLL.LLLLLLLLLLLLLLLLLLL.LLLL.LLLLL.L.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLL.LL.LLLLLL 43 | LLLL..LLLL.LLLLLLLLL.LLLLLL.LLL.LLL.LLLLLL..LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 44 | LLLLLLLLLLLLLLLLL.LL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LL.LLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL 45 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.L.LLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 46 | ...L.......LL....L......L...L..L.L..L...L.L.....LLLL.L...L.....L...L.....LL...........L..L...L...L 47 | LLLLLLLLLLLL.LLLLLLL.LLLLLL.LLL.LLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLL 48 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL 49 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.L.LLLLL.LLLLLLLLLLLLLL.LL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLL.LLLLLLLL 50 | LLLLL.LLLL.LLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLL 51 | LLLL...LLL.LLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 52 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLL.LLLL.L.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 53 | .LLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLL.L.LLLLLLLLLLLLLLLLL..LLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLL.L.LLLLLL 54 | ........L.L..LL.....L..L.L...L..LL.LL..L.L..L...........L.L...LLLL.L..L..........L..L....L..LL...L 55 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 56 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLL.L.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LL.LLL.LLLLLL.LLLLLLLLL.LLLLLL 57 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLL.LL 58 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LL.LLLL.LLLL...LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.L 59 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 60 | LLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLL.LL.LL.LLLLLLLLLL.LLLLLLLLLLLLLLLL 61 | .L.....LL......LLLLLL....L..LL...L.L..LLL.L..............L..LLL.L..L.L........LLLL....L..L..L..... 62 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLL.LLLLLL.LLLLLLLLLLLLLLLL 63 | LLLLL.LLLLLLLLLLLLLL.LLL.LL.LLLLLLL.LLLLLLL..LLLLLLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 64 | LLLLL.LLLL.LLLLL.LLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 65 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.L.LLLL.LLLLLL.LLLLLLLLL.LLLL.L 66 | LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLLL.L.LLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 67 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.L.LLLLLLLLLLLL..LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 68 | ...L.LLLL....LL....LL.L...LLLL.LL......L....L.L.L.L........LLLL.LL....L.L..L.L....LL..L....L...... 69 | LLLLL.LLLL.LLLLLLLL..LLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 70 | LLLLLLLLLL.LLL.LLLLL.LLL.LLL.LLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 71 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLL..LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 72 | LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 73 | ....LLL...LL.........L.....LL...L.L...LL......L...LL..L.L.....L..L....L.L.....L.L...L..LL...L.L... 74 | LLLLLLLL.L.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLL.LLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLL.LL..LLLLL 75 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLL.LLLLL.LLLLLLLLLLLLLLLLL 76 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLL.LL.LLLLLLLLL.LLLLLL 77 | LLLL..LLLL.L.LLLLL.L.L.LLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 78 | LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLL.L.LLLLLLLL.L.LLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLL.L 79 | LLLLLLLLL.LLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.L.LLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL..LLLLL 80 | ...LL...LL.L.....L.L.LL......L..L.L...L.L..L..L..........L...L.....L...L......L..LLL...L.L..LLL... 81 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 82 | LLLLL..LLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL..LLLLL 83 | LLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLL.LLLLLLLLL.LLLLLL 84 | LLLLL.LLLL.LLLLLLL..LLLLLLL.LLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 85 | LLLLLLLLLLLLLLLLLLLL.LLLLL..LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 86 | ....L....L.....LLLLL...L.L...LL...L.LL.........L.L.L..L..LL.....L..LLL..L.L......LL.L.LLLL..L..LLL 87 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.L.L.LLLLLLLLL.LLLLLLLLL.LLLLLL 88 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLL.LLLLLLLLL.LL.LLLLLLLLLLLLL.LLLLLLLL..LLLLLL 89 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 90 | LLLLL.LL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLL.LLLLLL..LLLLLLLLLLLLLLLL..LLLLLL 91 | LLLLL.L.LL.LLLL.LLLL.LLLLLL..LLLLLL.LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 92 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 93 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL..LLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 94 | LLLLL.LLLL.LLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 95 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLL.LL.LLLLLLL.LL.LLLLLL.LLLLLL.LLLLLLLLL..LLLLL 96 | LLL.L.LLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 97 | L.LLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL..LLLLLLLLLLLLLLLLLL.LLL.LLLLLL 98 | -------------------------------------------------------------------------------- /day11a/src/main.rs: -------------------------------------------------------------------------------- 1 | const COL: isize = 98; 2 | const ROW: isize = 97; 3 | 4 | const NONE: u8 = 0; 5 | const EMPTY: u8 = 1; 6 | const FULL: u8 = 2; 7 | 8 | pub fn main() { 9 | // Find seat indices 10 | let seats: Vec = include_bytes!("../input.txt") 11 | .into_iter() 12 | .enumerate() 13 | .filter(|(_, s)| s == &&b'L') 14 | .map(|(i, _)| i - (i / (COL as usize + 1))) 15 | .collect(); 16 | 17 | // Create seat map, mark seat positions as empty 18 | let mut cur = [NONE; (ROW * COL) as usize]; 19 | for i in &seats { 20 | cur[*i] = EMPTY; 21 | } 22 | let mut prev = cur; 23 | 24 | // Track seat neighbours indices 25 | #[rustfmt::skip] 26 | let seats: Vec<(usize, Vec)> = seats 27 | .iter() 28 | .map(|i| (*i, (0..9) 29 | .filter(|r| r != &4) 30 | .map(|r| ((*i as isize % COL) + r % 3 - 1, (*i as isize / COL) + r / 3 - 1)) 31 | .filter(|(x, y)| *x >= 0 && *y >= 0 && *x < COL && *y < ROW) 32 | .map(|(x, y)| (y * COL + x) as usize) 33 | .filter(|i| cur[*i] == EMPTY) 34 | .collect(), 35 | )) 36 | .collect(); 37 | 38 | while { 39 | for (i, visible) in &seats { 40 | let occup = visible.iter().filter(|i| prev[**i] == FULL).count(); 41 | let (cur_seat, prev_seat) = (&mut cur[*i], prev[*i]); 42 | 43 | if prev_seat == EMPTY && occup == 0 { 44 | *cur_seat = FULL; 45 | } else if prev_seat == FULL && occup >= 4 { 46 | *cur_seat = EMPTY; 47 | } else { 48 | *cur_seat = prev_seat; 49 | } 50 | } 51 | 52 | std::mem::swap(&mut cur, &mut prev); 53 | cur != prev 54 | } {} 55 | 56 | println!("{}", cur.iter().filter(|s| s == &&FULL).count()); 57 | } 58 | -------------------------------------------------------------------------------- /day11b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day11b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day11b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day11b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day11b/input.txt: -------------------------------------------------------------------------------- 1 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL 2 | LLLLLLLLLL.LLL.LLLLL.LLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL..LLLLL.LLLLLLLLLLLLLLLL 3 | LL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 4 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLL..LLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL 5 | LLLLL.LLLLLLLLLLLLLL.LLL.LL.LLLL.LLL.LLLLLL.LLLLL.LLLLLLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 6 | LLLLL.LLLL..LLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLL.LL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 7 | .......L...L.L...L......L..LLL..L.............L..L....L.LL.......LL......L..LL..L..L.L.LL..L.L.... 8 | LLLLL..LLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL..LLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLLLLL.LLL 9 | LLLLLLLLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLL.LLL..LL.LLLLLLLLLL.LLLL.LLLLLLLLLLLLL.LL.LLLLLL.LLLLLL 10 | L.LLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLL..LLLLLLL.LLLLLLLL..LLLLLLLL.LLLL.LLL.LL.LLLLLL.L.LLLLL.L.LLLLLL 11 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL 12 | LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 13 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLL.L.LLLLLLL.L.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 14 | .....LL..L....LL.L....L....L.....L.L...L......LLL..L.L....L..LLL...L.L..L.L..LL...L..L.L.....L.L.L 15 | LLLLL.LLL..LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL 16 | LLLLL.LLLL.LLLL.LL.L.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 17 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLLLL.LLL.LL.LLLLLLLLLLLLLLLL 18 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LL.LLLL.LLLLLL.LL.LLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 19 | .LLLL.LLLL.LLLLLLLLL.L.LLL..LLLLLLL.LLLLLLLLLLLLL.LLL.LLLLLLLLLLLLL.LLLL.LLL.LLLL.LLLLLLLLL.LLLLLL 20 | LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL...LLLLLL.LL.LLLLLL.LLL.LLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 21 | .......L.L...L..L..L.LL.LL.L....L.L..LL..L.L.L.LL...LLL...LLLL..LLLL.......L...L....LL.....L.LL... 22 | LLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 23 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLL 24 | LLLLLLLLLL.LLLLLLLLL.LL.LLL.LLLLL.L.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLL.LLL.LLLLLLLLL.LLLLLL 25 | LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLLLLL.LLLLLLLL.LL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 26 | LLLLL..LLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLLL.LL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 27 | L...LL.LL.L....LL..L.L..L..L.L.LL.L.....L..LL.......L..LLL..L...LL......LL..LL..L.L.LL..LL.L...... 28 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLL.LLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 29 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL..LLLLL.LLLLLLLLL.LLLLLL 30 | LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 31 | LLLLL.LLLLLLLLLLLLLL.LLLL.L.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLL..LLLLLL 32 | .LLLL..L...L..LL...L...L.L.L..LLL.L....L..L..L.....L.L..............LL.L....L..L......LL..LL..L... 33 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LL.LLLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 34 | LLLLL.LLLLLLLLLLL.LL..LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 35 | LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 36 | LLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 37 | LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLL.LLL.LLLLLLLL.LLLL.L.LLLLLLLLLLL.LLLLLLLLLLLLLLLL 38 | LLLLL.L.LLLLLLLLLLLLLLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLLL.LLLLL 39 | LLLLL.LLLL.LL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL..LLLLLLLLL.L.LLLLLLLLLLLLLLL.LLLLLLL 40 | L.LLL.LL.L.L...L.L....L.....L.....LL..........LL...L..L.L....L..L.L....L.L.LL.L.......LL.....L.L.. 41 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLL.LLLLLLLL.LLLL.LLLLLL..LLLLL.LLLLLLLLL.LLLLLL 42 | LLLLL.LLLL.LLLLLLLLLLLLLLLLLLL.LLLL.LLLLL.L.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLL.LL.LLLLLL 43 | LLLL..LLLL.LLLLLLLLL.LLLLLL.LLL.LLL.LLLLLL..LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 44 | LLLLLLLLLLLLLLLLL.LL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LL.LLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL 45 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.L.LLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 46 | ...L.......LL....L......L...L..L.L..L...L.L.....LLLL.L...L.....L...L.....LL...........L..L...L...L 47 | LLLLLLLLLLLL.LLLLLLL.LLLLLL.LLL.LLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLL 48 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL 49 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.L.LLLLL.LLLLLLLLLLLLLL.LL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLL.LLLLLLLL 50 | LLLLL.LLLL.LLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLL 51 | LLLL...LLL.LLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL 52 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLL.LLLL.L.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 53 | .LLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLL.L.LLLLLLLLLLLLLLLLL..LLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLL.L.LLLLLL 54 | ........L.L..LL.....L..L.L...L..LL.LL..L.L..L...........L.L...LLLL.L..L..........L..L....L..LL...L 55 | LLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 56 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLL.L.LLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LL.LLL.LLLLLL.LLLLLLLLL.LLLLLL 57 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLL.LL 58 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LL.LLLL.LLLL...LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.L 59 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 60 | LLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLL.LL.LL.LLLLLLLLLL.LLLLLLLLLLLLLLLL 61 | .L.....LL......LLLLLL....L..LL...L.L..LLL.L..............L..LLL.L..L.L........LLLL....L..L..L..... 62 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLL.LLLLLL.LLLLLLLLLLLLLLLL 63 | LLLLL.LLLLLLLLLLLLLL.LLL.LL.LLLLLLL.LLLLLLL..LLLLLLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 64 | LLLLL.LLLL.LLLLL.LLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 65 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.L.LLLL.LLLLLL.LLLLLLLLL.LLLL.L 66 | LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLLL.L.LLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 67 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.L.LLLLLLLLLLLL..LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 68 | ...L.LLLL....LL....LL.L...LLLL.LL......L....L.L.L.L........LLLL.LL....L.L..L.L....LL..L....L...... 69 | LLLLL.LLLL.LLLLLLLL..LLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 70 | LLLLLLLLLL.LLL.LLLLL.LLL.LLL.LLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 71 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLL..LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 72 | LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 73 | ....LLL...LL.........L.....LL...L.L...LL......L...LL..L.L.....L..L....L.L.....L.L...L..LL...L.L... 74 | LLLLLLLL.L.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLL.LLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLL.LL..LLLLL 75 | LLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLL.LLLLL.LLLLLLLLLLLLLLLLL 76 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLL.LL.LLLLLLLLL.LLLLLL 77 | LLLL..LLLL.L.LLLLL.L.L.LLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 78 | LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLL.L.LLLLLLLL.L.LLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLL.L 79 | LLLLLLLLL.LLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.L.LLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL..LLLLL 80 | ...LL...LL.L.....L.L.LL......L..L.L...L.L..L..L..........L...L.....L...L......L..LLL...L.L..LLL... 81 | LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLL.LLLLLL.LLLLLLLLL.LLLLLL 82 | LLLLL..LLL.LLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL..LLLLL 83 | LLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLL.LLLLLLLLL.LLLLLL 84 | LLLLL.LLLL.LLLLLLL..LLLLLLL.LLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 85 | LLLLLLLLLLLLLLLLLLLL.LLLLL..LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLL.LLLLLLLLL.LLLLLL 86 | ....L....L.....LLLLL...L.L...LL...L.LL.........L.L.L..L..LL.....L..LLL..L.L......LL.L.LLLL..L..LLL 87 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.L.L.LLLLLLLLL.LLLLLLLLL.LLLLLL 88 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLL.LLLLLLLLL.LL.LLLLLLLLLLLLL.LLLLLLLL..LLLLLL 89 | LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL 90 | LLLLL.LL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLL.LLLLLL..LLLLLLLLLLLLLLLL..LLLLLL 91 | LLLLL.L.LL.LLLL.LLLL.LLLLLL..LLLLLL.LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 92 | LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL 93 | LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL..LLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 94 | LLLLL.LLLL.LLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL 95 | LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLL.LL.LLLLLLL.LL.LLLLLL.LLLLLL.LLLLLLLLL..LLLLL 96 | LLL.L.LLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLL 97 | L.LLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL..LLLLLLLLLLLLLLLLLL.LLL.LLLLLL 98 | -------------------------------------------------------------------------------- /day11b/src/main.rs: -------------------------------------------------------------------------------- 1 | const COL: isize = 98; 2 | const ROW: isize = 97; 3 | 4 | const NONE: u8 = 0; 5 | const EMPTY: u8 = 1; 6 | const FULL: u8 = 2; 7 | 8 | pub fn main() { 9 | // Find seat indices 10 | let seats: Vec = include_bytes!("../input.txt") 11 | .into_iter() 12 | .enumerate() 13 | .filter(|(_, s)| s == &&b'L') 14 | .map(|(i, _)| i - (i / (COL as usize + 1))) 15 | .collect(); 16 | 17 | // Create seat map, mark seat positions as empty 18 | let mut cur = [NONE; (ROW * COL) as usize]; 19 | for i in &seats { 20 | cur[*i] = EMPTY; 21 | } 22 | let mut prev = cur; 23 | 24 | // Track seat neighbours indices 25 | #[rustfmt::skip] 26 | let seats: Vec<(usize, Vec)> = seats 27 | .into_iter() 28 | .map(|i| (i, (0..9) 29 | .filter(|r| r != &4) 30 | .map(|r| (r % 3 - 1, r / 3 - 1)) 31 | .filter_map(|(rx, ry)| (1..) 32 | .map(|f| ((i as isize % COL) + rx * f, (i as isize / COL) + ry * f)) 33 | .take_while(|(x, y)| *x >= 0 && *y >= 0 && *x < COL && *y < ROW) 34 | .map(|(x, y)| (y * COL + x) as usize) 35 | .filter(|i| cur[*i] == EMPTY) 36 | .next() 37 | ) 38 | .collect(), 39 | )) 40 | .collect(); 41 | 42 | while { 43 | for (i, visible) in &seats { 44 | let occup = visible.iter().filter(|i| prev[**i] == FULL).count(); 45 | let (cur_seat, prev_seat) = (&mut cur[*i], prev[*i]); 46 | 47 | if prev_seat == EMPTY && occup == 0 { 48 | *cur_seat = FULL; 49 | } else if prev_seat == FULL && occup >= 5 { 50 | *cur_seat = EMPTY; 51 | } else { 52 | *cur_seat = prev_seat; 53 | } 54 | } 55 | 56 | std::mem::swap(&mut cur, &mut prev); 57 | cur != prev 58 | } {} 59 | 60 | println!("{}", cur.iter().filter(|s| s == &&FULL).count()); 61 | } 62 | -------------------------------------------------------------------------------- /day12a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "atoi" 5 | version = "0.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" 8 | dependencies = [ 9 | "num-traits", 10 | ] 11 | 12 | [[package]] 13 | name = "autocfg" 14 | version = "1.0.1" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 17 | 18 | [[package]] 19 | name = "day12a" 20 | version = "0.1.0" 21 | dependencies = [ 22 | "atoi", 23 | ] 24 | 25 | [[package]] 26 | name = "num-traits" 27 | version = "0.2.14" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 30 | dependencies = [ 31 | "autocfg", 32 | ] 33 | -------------------------------------------------------------------------------- /day12a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day12a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | atoi = "0.4" 12 | -------------------------------------------------------------------------------- /day12a/input.txt: -------------------------------------------------------------------------------- 1 | S2 2 | W5 3 | F20 4 | E3 5 | S5 6 | R90 7 | W5 8 | F48 9 | R180 10 | E3 11 | S3 12 | E5 13 | S3 14 | F83 15 | S1 16 | W5 17 | F81 18 | W3 19 | R90 20 | F88 21 | S2 22 | R90 23 | E2 24 | L90 25 | W4 26 | F77 27 | E1 28 | R90 29 | S4 30 | E2 31 | F89 32 | N2 33 | L90 34 | N2 35 | N2 36 | E3 37 | L180 38 | N1 39 | F82 40 | R90 41 | S3 42 | F64 43 | W1 44 | R180 45 | S2 46 | R180 47 | F28 48 | L180 49 | S3 50 | F100 51 | E1 52 | S4 53 | S5 54 | R90 55 | F3 56 | W5 57 | N4 58 | F9 59 | N5 60 | E4 61 | R90 62 | F83 63 | L180 64 | W3 65 | N5 66 | W2 67 | R180 68 | W1 69 | S4 70 | L90 71 | S5 72 | R90 73 | R90 74 | F36 75 | L180 76 | W1 77 | F88 78 | E3 79 | R90 80 | F13 81 | W1 82 | F13 83 | W4 84 | R90 85 | E3 86 | F98 87 | S4 88 | E5 89 | R90 90 | F77 91 | E5 92 | L180 93 | N5 94 | R90 95 | E1 96 | F70 97 | L180 98 | F54 99 | E5 100 | F9 101 | L180 102 | N3 103 | F77 104 | E2 105 | F81 106 | E2 107 | S1 108 | F41 109 | L90 110 | F32 111 | S3 112 | F66 113 | N2 114 | E4 115 | R90 116 | F80 117 | R90 118 | W1 119 | R90 120 | S3 121 | F65 122 | S4 123 | R90 124 | W4 125 | S4 126 | R90 127 | W4 128 | S2 129 | E1 130 | F44 131 | R90 132 | F53 133 | E3 134 | F67 135 | S5 136 | L90 137 | R90 138 | W2 139 | S2 140 | E3 141 | N5 142 | F97 143 | N3 144 | E1 145 | R90 146 | W4 147 | N5 148 | S4 149 | F38 150 | N5 151 | N1 152 | W4 153 | S3 154 | E1 155 | S3 156 | F87 157 | E1 158 | R90 159 | E2 160 | L180 161 | S1 162 | L90 163 | F29 164 | R90 165 | S2 166 | F95 167 | N2 168 | E5 169 | F24 170 | W1 171 | F71 172 | E3 173 | L90 174 | F16 175 | L90 176 | S5 177 | L90 178 | W1 179 | F95 180 | L90 181 | W2 182 | F97 183 | E4 184 | R90 185 | N5 186 | F77 187 | E2 188 | L90 189 | F71 190 | L90 191 | W2 192 | R90 193 | F75 194 | E1 195 | S1 196 | F28 197 | F86 198 | R90 199 | E3 200 | F9 201 | N2 202 | W4 203 | S1 204 | L90 205 | F10 206 | W4 207 | L90 208 | S5 209 | E1 210 | L90 211 | W2 212 | S3 213 | F41 214 | L90 215 | S5 216 | F73 217 | L90 218 | S4 219 | F80 220 | W4 221 | R180 222 | S4 223 | E3 224 | F89 225 | E5 226 | N5 227 | L90 228 | F66 229 | N4 230 | W1 231 | F93 232 | W1 233 | F27 234 | F68 235 | N4 236 | F23 237 | L90 238 | F14 239 | S3 240 | F74 241 | N2 242 | E2 243 | R90 244 | S4 245 | F85 246 | N2 247 | L180 248 | L180 249 | F55 250 | S5 251 | E5 252 | F42 253 | N2 254 | R90 255 | F53 256 | S4 257 | F70 258 | E2 259 | F73 260 | S4 261 | F71 262 | L90 263 | E1 264 | S3 265 | E5 266 | N3 267 | E2 268 | N5 269 | L270 270 | F69 271 | E5 272 | F30 273 | N1 274 | R90 275 | N4 276 | F15 277 | E4 278 | R180 279 | F12 280 | E2 281 | F95 282 | E2 283 | S4 284 | F13 285 | S1 286 | E1 287 | N4 288 | E3 289 | L270 290 | E4 291 | S5 292 | E3 293 | N2 294 | R180 295 | S2 296 | W2 297 | L90 298 | S3 299 | W4 300 | R90 301 | E1 302 | L180 303 | W5 304 | F23 305 | E5 306 | F67 307 | R90 308 | N2 309 | W4 310 | L90 311 | S1 312 | L90 313 | F17 314 | W2 315 | F80 316 | E1 317 | F13 318 | S2 319 | E3 320 | S4 321 | F46 322 | F70 323 | R180 324 | E3 325 | L90 326 | E4 327 | L90 328 | S3 329 | F31 330 | W5 331 | R90 332 | F21 333 | S5 334 | L90 335 | F12 336 | L90 337 | F70 338 | S1 339 | R180 340 | W5 341 | F14 342 | S5 343 | W5 344 | S1 345 | F56 346 | L90 347 | F43 348 | S4 349 | N4 350 | E1 351 | R180 352 | S1 353 | R90 354 | E3 355 | N4 356 | R90 357 | S2 358 | E5 359 | S4 360 | W4 361 | R90 362 | F88 363 | S4 364 | R90 365 | F79 366 | L90 367 | N2 368 | E3 369 | F74 370 | L90 371 | F72 372 | W1 373 | N3 374 | R90 375 | E2 376 | F38 377 | W3 378 | L90 379 | E3 380 | N2 381 | R90 382 | E2 383 | L90 384 | N3 385 | E1 386 | F96 387 | E3 388 | S4 389 | R180 390 | F28 391 | S2 392 | F93 393 | L270 394 | N3 395 | R90 396 | N2 397 | E4 398 | S1 399 | F47 400 | L270 401 | F9 402 | N2 403 | F80 404 | S4 405 | L180 406 | N5 407 | W2 408 | L90 409 | W4 410 | N5 411 | L90 412 | F82 413 | R90 414 | N3 415 | F4 416 | R90 417 | F96 418 | W5 419 | R90 420 | F28 421 | W2 422 | S2 423 | F16 424 | S4 425 | R90 426 | F9 427 | N5 428 | W4 429 | F78 430 | E3 431 | F84 432 | L90 433 | F42 434 | W2 435 | F43 436 | W4 437 | L90 438 | F95 439 | E1 440 | R90 441 | F59 442 | L90 443 | F82 444 | W1 445 | F17 446 | S4 447 | R180 448 | F91 449 | L180 450 | N1 451 | R90 452 | N3 453 | F57 454 | L90 455 | F90 456 | N3 457 | R90 458 | F98 459 | N5 460 | R90 461 | N2 462 | R90 463 | F4 464 | S2 465 | F80 466 | S2 467 | N2 468 | W4 469 | L90 470 | E2 471 | F67 472 | R90 473 | W4 474 | L180 475 | F22 476 | L90 477 | S3 478 | F9 479 | N5 480 | W2 481 | L180 482 | S3 483 | F23 484 | S4 485 | W4 486 | F90 487 | S2 488 | F32 489 | S3 490 | E5 491 | R90 492 | E1 493 | F64 494 | R180 495 | F17 496 | E3 497 | R90 498 | N2 499 | E1 500 | F90 501 | W3 502 | N5 503 | F19 504 | S5 505 | E4 506 | N5 507 | R90 508 | W1 509 | F18 510 | R180 511 | E2 512 | R90 513 | F76 514 | L90 515 | N2 516 | F96 517 | F38 518 | R180 519 | F94 520 | S4 521 | W4 522 | S4 523 | F63 524 | E4 525 | L90 526 | F69 527 | S2 528 | W3 529 | N1 530 | L270 531 | N1 532 | L90 533 | N4 534 | R90 535 | E5 536 | F67 537 | F85 538 | S1 539 | F2 540 | F68 541 | W2 542 | R90 543 | E4 544 | R90 545 | S3 546 | W4 547 | S2 548 | N5 549 | F13 550 | R180 551 | W1 552 | F31 553 | S1 554 | F23 555 | S1 556 | R90 557 | F86 558 | L90 559 | N5 560 | W3 561 | F47 562 | R90 563 | R270 564 | S1 565 | L90 566 | S1 567 | F40 568 | N4 569 | R180 570 | N1 571 | L180 572 | W3 573 | S1 574 | W5 575 | L90 576 | W5 577 | F41 578 | S4 579 | E2 580 | N5 581 | E4 582 | N1 583 | F97 584 | L90 585 | F62 586 | N5 587 | R90 588 | F98 589 | R180 590 | S5 591 | L270 592 | N5 593 | W5 594 | N4 595 | E3 596 | F38 597 | L90 598 | S2 599 | F79 600 | R270 601 | E1 602 | F14 603 | W1 604 | F38 605 | E3 606 | L180 607 | N4 608 | L90 609 | S5 610 | F90 611 | S1 612 | F7 613 | N1 614 | F11 615 | W4 616 | R180 617 | F73 618 | L90 619 | F54 620 | F25 621 | W5 622 | S3 623 | F39 624 | E2 625 | F37 626 | E2 627 | S4 628 | F94 629 | W5 630 | S2 631 | L180 632 | W3 633 | F70 634 | S3 635 | R180 636 | W1 637 | N5 638 | R90 639 | S4 640 | F12 641 | S3 642 | F45 643 | E1 644 | N3 645 | R90 646 | S2 647 | L180 648 | E4 649 | S3 650 | W5 651 | F9 652 | S4 653 | R90 654 | F19 655 | W3 656 | N4 657 | W5 658 | R90 659 | F16 660 | W3 661 | S4 662 | W1 663 | S3 664 | R180 665 | S4 666 | E4 667 | N1 668 | W3 669 | F10 670 | N5 671 | E2 672 | F61 673 | E4 674 | S1 675 | W3 676 | N5 677 | W2 678 | L90 679 | W5 680 | N2 681 | W1 682 | F8 683 | W4 684 | S5 685 | L180 686 | F76 687 | N1 688 | R180 689 | S3 690 | R180 691 | S3 692 | E1 693 | R90 694 | W1 695 | F38 696 | R90 697 | F74 698 | N2 699 | F60 700 | L90 701 | E2 702 | S3 703 | F78 704 | W1 705 | F57 706 | R90 707 | F39 708 | N5 709 | F2 710 | E3 711 | R90 712 | F81 713 | E2 714 | F2 715 | S3 716 | R90 717 | F95 718 | S4 719 | F1 720 | E2 721 | N5 722 | L270 723 | F59 724 | W2 725 | R90 726 | S1 727 | F42 728 | E2 729 | N5 730 | E4 731 | L90 732 | W1 733 | F17 734 | E2 735 | F91 736 | R90 737 | E3 738 | F30 739 | S1 740 | R90 741 | W5 742 | F74 743 | W5 744 | N1 745 | N4 746 | R180 747 | S5 748 | R180 749 | F92 750 | N4 751 | N3 752 | E3 753 | R90 754 | F60 755 | S4 756 | L90 757 | E4 758 | R270 759 | F3 760 | E1 761 | L180 762 | E1 763 | S5 764 | F84 765 | E3 766 | L90 767 | N3 768 | F27 769 | W1 770 | F58 771 | N1 772 | F93 773 | N2 774 | F30 775 | L90 776 | N2 777 | L90 778 | N2 779 | F99 780 | W4 781 | S2 782 | F13 783 | L90 784 | N3 785 | W4 786 | F100 -------------------------------------------------------------------------------- /day12a/src/main.rs: -------------------------------------------------------------------------------- 1 | #[rustfmt::skip] 2 | pub fn main() { 3 | let (x, y, _) = include_bytes!("../input.txt") 4 | .split(|b| b == &b'\n') 5 | .fold((0, 0, 0b01000100u8), |(x, y, d), i| { 6 | let v: isize = atoi::atoi(&i[1..]).unwrap(); 7 | match i[0] { 8 | b'N' => (x, y - v, d), 9 | b'S' => (x, y + v, d), 10 | b'E' => (x + v, y, d), 11 | b'W' => (x - v, y, d), 12 | b'L' => (x, y, d.rotate_left(v as u32 / 90)), 13 | b'R' => (x, y, d.rotate_right(v as u32 / 90)), 14 | b'F' => ( 15 | x + (((d & 4) >> 2) as isize - (d & 1) as isize) * v, 16 | y + (((d & 2) >> 1) as isize - ((d & 8) >> 3) as isize) * v, 17 | d, 18 | ), 19 | _ => unreachable!(), 20 | } 21 | }); 22 | println!("{}", x.abs() + y.abs()); 23 | } 24 | -------------------------------------------------------------------------------- /day12b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "atoi" 5 | version = "0.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" 8 | dependencies = [ 9 | "num-traits", 10 | ] 11 | 12 | [[package]] 13 | name = "autocfg" 14 | version = "1.0.1" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 17 | 18 | [[package]] 19 | name = "day12b" 20 | version = "0.1.0" 21 | dependencies = [ 22 | "atoi", 23 | ] 24 | 25 | [[package]] 26 | name = "num-traits" 27 | version = "0.2.14" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 30 | dependencies = [ 31 | "autocfg", 32 | ] 33 | -------------------------------------------------------------------------------- /day12b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day12b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | atoi = "0.4" 12 | -------------------------------------------------------------------------------- /day12b/input.txt: -------------------------------------------------------------------------------- 1 | S2 2 | W5 3 | F20 4 | E3 5 | S5 6 | R90 7 | W5 8 | F48 9 | R180 10 | E3 11 | S3 12 | E5 13 | S3 14 | F83 15 | S1 16 | W5 17 | F81 18 | W3 19 | R90 20 | F88 21 | S2 22 | R90 23 | E2 24 | L90 25 | W4 26 | F77 27 | E1 28 | R90 29 | S4 30 | E2 31 | F89 32 | N2 33 | L90 34 | N2 35 | N2 36 | E3 37 | L180 38 | N1 39 | F82 40 | R90 41 | S3 42 | F64 43 | W1 44 | R180 45 | S2 46 | R180 47 | F28 48 | L180 49 | S3 50 | F100 51 | E1 52 | S4 53 | S5 54 | R90 55 | F3 56 | W5 57 | N4 58 | F9 59 | N5 60 | E4 61 | R90 62 | F83 63 | L180 64 | W3 65 | N5 66 | W2 67 | R180 68 | W1 69 | S4 70 | L90 71 | S5 72 | R90 73 | R90 74 | F36 75 | L180 76 | W1 77 | F88 78 | E3 79 | R90 80 | F13 81 | W1 82 | F13 83 | W4 84 | R90 85 | E3 86 | F98 87 | S4 88 | E5 89 | R90 90 | F77 91 | E5 92 | L180 93 | N5 94 | R90 95 | E1 96 | F70 97 | L180 98 | F54 99 | E5 100 | F9 101 | L180 102 | N3 103 | F77 104 | E2 105 | F81 106 | E2 107 | S1 108 | F41 109 | L90 110 | F32 111 | S3 112 | F66 113 | N2 114 | E4 115 | R90 116 | F80 117 | R90 118 | W1 119 | R90 120 | S3 121 | F65 122 | S4 123 | R90 124 | W4 125 | S4 126 | R90 127 | W4 128 | S2 129 | E1 130 | F44 131 | R90 132 | F53 133 | E3 134 | F67 135 | S5 136 | L90 137 | R90 138 | W2 139 | S2 140 | E3 141 | N5 142 | F97 143 | N3 144 | E1 145 | R90 146 | W4 147 | N5 148 | S4 149 | F38 150 | N5 151 | N1 152 | W4 153 | S3 154 | E1 155 | S3 156 | F87 157 | E1 158 | R90 159 | E2 160 | L180 161 | S1 162 | L90 163 | F29 164 | R90 165 | S2 166 | F95 167 | N2 168 | E5 169 | F24 170 | W1 171 | F71 172 | E3 173 | L90 174 | F16 175 | L90 176 | S5 177 | L90 178 | W1 179 | F95 180 | L90 181 | W2 182 | F97 183 | E4 184 | R90 185 | N5 186 | F77 187 | E2 188 | L90 189 | F71 190 | L90 191 | W2 192 | R90 193 | F75 194 | E1 195 | S1 196 | F28 197 | F86 198 | R90 199 | E3 200 | F9 201 | N2 202 | W4 203 | S1 204 | L90 205 | F10 206 | W4 207 | L90 208 | S5 209 | E1 210 | L90 211 | W2 212 | S3 213 | F41 214 | L90 215 | S5 216 | F73 217 | L90 218 | S4 219 | F80 220 | W4 221 | R180 222 | S4 223 | E3 224 | F89 225 | E5 226 | N5 227 | L90 228 | F66 229 | N4 230 | W1 231 | F93 232 | W1 233 | F27 234 | F68 235 | N4 236 | F23 237 | L90 238 | F14 239 | S3 240 | F74 241 | N2 242 | E2 243 | R90 244 | S4 245 | F85 246 | N2 247 | L180 248 | L180 249 | F55 250 | S5 251 | E5 252 | F42 253 | N2 254 | R90 255 | F53 256 | S4 257 | F70 258 | E2 259 | F73 260 | S4 261 | F71 262 | L90 263 | E1 264 | S3 265 | E5 266 | N3 267 | E2 268 | N5 269 | L270 270 | F69 271 | E5 272 | F30 273 | N1 274 | R90 275 | N4 276 | F15 277 | E4 278 | R180 279 | F12 280 | E2 281 | F95 282 | E2 283 | S4 284 | F13 285 | S1 286 | E1 287 | N4 288 | E3 289 | L270 290 | E4 291 | S5 292 | E3 293 | N2 294 | R180 295 | S2 296 | W2 297 | L90 298 | S3 299 | W4 300 | R90 301 | E1 302 | L180 303 | W5 304 | F23 305 | E5 306 | F67 307 | R90 308 | N2 309 | W4 310 | L90 311 | S1 312 | L90 313 | F17 314 | W2 315 | F80 316 | E1 317 | F13 318 | S2 319 | E3 320 | S4 321 | F46 322 | F70 323 | R180 324 | E3 325 | L90 326 | E4 327 | L90 328 | S3 329 | F31 330 | W5 331 | R90 332 | F21 333 | S5 334 | L90 335 | F12 336 | L90 337 | F70 338 | S1 339 | R180 340 | W5 341 | F14 342 | S5 343 | W5 344 | S1 345 | F56 346 | L90 347 | F43 348 | S4 349 | N4 350 | E1 351 | R180 352 | S1 353 | R90 354 | E3 355 | N4 356 | R90 357 | S2 358 | E5 359 | S4 360 | W4 361 | R90 362 | F88 363 | S4 364 | R90 365 | F79 366 | L90 367 | N2 368 | E3 369 | F74 370 | L90 371 | F72 372 | W1 373 | N3 374 | R90 375 | E2 376 | F38 377 | W3 378 | L90 379 | E3 380 | N2 381 | R90 382 | E2 383 | L90 384 | N3 385 | E1 386 | F96 387 | E3 388 | S4 389 | R180 390 | F28 391 | S2 392 | F93 393 | L270 394 | N3 395 | R90 396 | N2 397 | E4 398 | S1 399 | F47 400 | L270 401 | F9 402 | N2 403 | F80 404 | S4 405 | L180 406 | N5 407 | W2 408 | L90 409 | W4 410 | N5 411 | L90 412 | F82 413 | R90 414 | N3 415 | F4 416 | R90 417 | F96 418 | W5 419 | R90 420 | F28 421 | W2 422 | S2 423 | F16 424 | S4 425 | R90 426 | F9 427 | N5 428 | W4 429 | F78 430 | E3 431 | F84 432 | L90 433 | F42 434 | W2 435 | F43 436 | W4 437 | L90 438 | F95 439 | E1 440 | R90 441 | F59 442 | L90 443 | F82 444 | W1 445 | F17 446 | S4 447 | R180 448 | F91 449 | L180 450 | N1 451 | R90 452 | N3 453 | F57 454 | L90 455 | F90 456 | N3 457 | R90 458 | F98 459 | N5 460 | R90 461 | N2 462 | R90 463 | F4 464 | S2 465 | F80 466 | S2 467 | N2 468 | W4 469 | L90 470 | E2 471 | F67 472 | R90 473 | W4 474 | L180 475 | F22 476 | L90 477 | S3 478 | F9 479 | N5 480 | W2 481 | L180 482 | S3 483 | F23 484 | S4 485 | W4 486 | F90 487 | S2 488 | F32 489 | S3 490 | E5 491 | R90 492 | E1 493 | F64 494 | R180 495 | F17 496 | E3 497 | R90 498 | N2 499 | E1 500 | F90 501 | W3 502 | N5 503 | F19 504 | S5 505 | E4 506 | N5 507 | R90 508 | W1 509 | F18 510 | R180 511 | E2 512 | R90 513 | F76 514 | L90 515 | N2 516 | F96 517 | F38 518 | R180 519 | F94 520 | S4 521 | W4 522 | S4 523 | F63 524 | E4 525 | L90 526 | F69 527 | S2 528 | W3 529 | N1 530 | L270 531 | N1 532 | L90 533 | N4 534 | R90 535 | E5 536 | F67 537 | F85 538 | S1 539 | F2 540 | F68 541 | W2 542 | R90 543 | E4 544 | R90 545 | S3 546 | W4 547 | S2 548 | N5 549 | F13 550 | R180 551 | W1 552 | F31 553 | S1 554 | F23 555 | S1 556 | R90 557 | F86 558 | L90 559 | N5 560 | W3 561 | F47 562 | R90 563 | R270 564 | S1 565 | L90 566 | S1 567 | F40 568 | N4 569 | R180 570 | N1 571 | L180 572 | W3 573 | S1 574 | W5 575 | L90 576 | W5 577 | F41 578 | S4 579 | E2 580 | N5 581 | E4 582 | N1 583 | F97 584 | L90 585 | F62 586 | N5 587 | R90 588 | F98 589 | R180 590 | S5 591 | L270 592 | N5 593 | W5 594 | N4 595 | E3 596 | F38 597 | L90 598 | S2 599 | F79 600 | R270 601 | E1 602 | F14 603 | W1 604 | F38 605 | E3 606 | L180 607 | N4 608 | L90 609 | S5 610 | F90 611 | S1 612 | F7 613 | N1 614 | F11 615 | W4 616 | R180 617 | F73 618 | L90 619 | F54 620 | F25 621 | W5 622 | S3 623 | F39 624 | E2 625 | F37 626 | E2 627 | S4 628 | F94 629 | W5 630 | S2 631 | L180 632 | W3 633 | F70 634 | S3 635 | R180 636 | W1 637 | N5 638 | R90 639 | S4 640 | F12 641 | S3 642 | F45 643 | E1 644 | N3 645 | R90 646 | S2 647 | L180 648 | E4 649 | S3 650 | W5 651 | F9 652 | S4 653 | R90 654 | F19 655 | W3 656 | N4 657 | W5 658 | R90 659 | F16 660 | W3 661 | S4 662 | W1 663 | S3 664 | R180 665 | S4 666 | E4 667 | N1 668 | W3 669 | F10 670 | N5 671 | E2 672 | F61 673 | E4 674 | S1 675 | W3 676 | N5 677 | W2 678 | L90 679 | W5 680 | N2 681 | W1 682 | F8 683 | W4 684 | S5 685 | L180 686 | F76 687 | N1 688 | R180 689 | S3 690 | R180 691 | S3 692 | E1 693 | R90 694 | W1 695 | F38 696 | R90 697 | F74 698 | N2 699 | F60 700 | L90 701 | E2 702 | S3 703 | F78 704 | W1 705 | F57 706 | R90 707 | F39 708 | N5 709 | F2 710 | E3 711 | R90 712 | F81 713 | E2 714 | F2 715 | S3 716 | R90 717 | F95 718 | S4 719 | F1 720 | E2 721 | N5 722 | L270 723 | F59 724 | W2 725 | R90 726 | S1 727 | F42 728 | E2 729 | N5 730 | E4 731 | L90 732 | W1 733 | F17 734 | E2 735 | F91 736 | R90 737 | E3 738 | F30 739 | S1 740 | R90 741 | W5 742 | F74 743 | W5 744 | N1 745 | N4 746 | R180 747 | S5 748 | R180 749 | F92 750 | N4 751 | N3 752 | E3 753 | R90 754 | F60 755 | S4 756 | L90 757 | E4 758 | R270 759 | F3 760 | E1 761 | L180 762 | E1 763 | S5 764 | F84 765 | E3 766 | L90 767 | N3 768 | F27 769 | W1 770 | F58 771 | N1 772 | F93 773 | N2 774 | F30 775 | L90 776 | N2 777 | L90 778 | N2 779 | F99 780 | W4 781 | S2 782 | F13 783 | L90 784 | N3 785 | W4 786 | F100 -------------------------------------------------------------------------------- /day12b/src/main.rs: -------------------------------------------------------------------------------- 1 | #[rustfmt::skip] 2 | pub fn main() { 3 | let (x, y, _, _) = include_bytes!("../input.txt") 4 | .split(|b| b == &b'\n') 5 | .fold((0, 0, 10, -1), |(x, y, wx, wy), i| { 6 | let v: isize = atoi::atoi(&i[1..]).unwrap(); 7 | match i[0] { 8 | b'N' => (x, y, wx, wy - v), 9 | b'S' => (x, y, wx, wy + v), 10 | b'E' => (x, y, wx + v, wy), 11 | b'W' => (x, y, wx - v, wy), 12 | b if b == b'L' && v == 90 || b == b'R' && v == 270 => (x, y, wy, -wx), 13 | b if (b == b'L' || b == b'R') && v == 180 => (x, y, -wx, -wy), 14 | b if b == b'L' && v == 270 || b == b'R' && v == 90 => (x, y, -wy, wx), 15 | b'F' => (x + wx * v, y + wy * v, wx, wy), 16 | _ => unreachable!(), 17 | } 18 | }); 19 | println!("{}", x.abs() + y.abs()); 20 | } 21 | -------------------------------------------------------------------------------- /day13a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day13a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day13a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day13a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day13a/input.txt: -------------------------------------------------------------------------------- 1 | 1011416 2 | 41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,911,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,x,x,x,x,23,x,x,x,x,x,29,x,827,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19 -------------------------------------------------------------------------------- /day13a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | pub fn main() { 4 | let (est, lines) = include_str!("../input.txt").split_once('\n').unwrap(); 5 | let est = est.parse::().unwrap(); 6 | let (line, wait) = lines 7 | .split(',') 8 | .filter_map(|l| l.parse::().ok()) 9 | .map(|l| (l, l - (est % l))) 10 | .min_by_key(|l| l.1) 11 | .unwrap(); 12 | println!("{}", line * wait); 13 | } 14 | -------------------------------------------------------------------------------- /day13b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.0.1" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 10 | 11 | [[package]] 12 | name = "day13b" 13 | version = "0.1.0" 14 | dependencies = [ 15 | "ring-algorithm", 16 | ] 17 | 18 | [[package]] 19 | name = "num-bigint" 20 | version = "0.3.3" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3" 23 | dependencies = [ 24 | "autocfg", 25 | "num-integer", 26 | "num-traits", 27 | ] 28 | 29 | [[package]] 30 | name = "num-integer" 31 | version = "0.1.44" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" 34 | dependencies = [ 35 | "autocfg", 36 | "num-traits", 37 | ] 38 | 39 | [[package]] 40 | name = "num-traits" 41 | version = "0.2.14" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" 44 | dependencies = [ 45 | "autocfg", 46 | ] 47 | 48 | [[package]] 49 | name = "ring-algorithm" 50 | version = "0.2.3" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "ef97b6162a505432db23759895f5c16b9464167d88c895f151fdcdc3ad816d89" 53 | dependencies = [ 54 | "num-bigint", 55 | "num-traits", 56 | "take_mut", 57 | ] 58 | 59 | [[package]] 60 | name = "take_mut" 61 | version = "0.2.2" 62 | source = "registry+https://github.com/rust-lang/crates.io-index" 63 | checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" 64 | -------------------------------------------------------------------------------- /day13b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day13b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | ring-algorithm = "0.2" 12 | -------------------------------------------------------------------------------- /day13b/input.txt: -------------------------------------------------------------------------------- 1 | 1011416 2 | 41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,911,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,x,x,x,x,23,x,x,x,x,x,29,x,827,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19 3 | -------------------------------------------------------------------------------- /day13b/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let lines: Vec<(i64, i64)> = include_str!("../input.txt") 3 | .lines() 4 | .nth(1) 5 | .unwrap() 6 | .split(',') 7 | .enumerate() 8 | .filter_map(|(i, l)| l.parse().ok().map(|l| (l - i as i64, l))) 9 | .collect(); 10 | println!("{}", crt(&lines)); 11 | } 12 | 13 | #[inline(always)] 14 | fn egcd(a: i64, b: i64) -> (i64, i64, i64) { 15 | if a == 0 { 16 | (b, 0, 1) 17 | } else { 18 | let (g, x, y) = egcd(b % a, a); 19 | (g, y - (b / a) * x, x) 20 | } 21 | } 22 | 23 | fn crt(remo: &[(i64, i64)]) -> i64 { 24 | let prod = remo.iter().map(|n| n.1).product::(); 25 | remo.iter() 26 | .map(|(re, mo)| { 27 | let p = prod / mo; 28 | re * ((egcd(p, *mo).1 % mo + mo) % mo) * p 29 | }) 30 | .sum::() 31 | % prod 32 | } 33 | -------------------------------------------------------------------------------- /day14a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day14a" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "regex", 19 | ] 20 | 21 | [[package]] 22 | name = "memchr" 23 | version = "2.4.1" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 26 | 27 | [[package]] 28 | name = "regex" 29 | version = "1.5.4" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 32 | dependencies = [ 33 | "aho-corasick", 34 | "memchr", 35 | "regex-syntax", 36 | ] 37 | 38 | [[package]] 39 | name = "regex-syntax" 40 | version = "0.6.25" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 43 | -------------------------------------------------------------------------------- /day14a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day14a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | regex = "1.4" 12 | -------------------------------------------------------------------------------- /day14a/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use std::collections::HashMap; 3 | 4 | pub fn main() { 5 | let re = Regex::new(r#"^mem\[(\d+)\] = (\d+)$"#).unwrap(); 6 | let mut mem: HashMap = HashMap::new(); 7 | let mut and_or = (0, 0); 8 | 9 | for line in include_str!("../input.txt").lines() { 10 | if line.starts_with("ma") { 11 | and_or = line 12 | .split(" = ") 13 | .skip(1) 14 | .next() 15 | .unwrap() 16 | .bytes() 17 | .rev() 18 | .enumerate() 19 | .fold((usize::MAX, 0), |(and, or), (i, b)| match b { 20 | b'0' => (and & !(1 << i), or), 21 | b'1' => (and, or | 1 << i), 22 | _ => (and, or), 23 | }); 24 | } else { 25 | let captures = re.captures(&line).unwrap(); 26 | mem.insert( 27 | captures[1].parse().unwrap(), 28 | captures[2].parse::().unwrap() & and_or.0 | and_or.1, 29 | ); 30 | } 31 | } 32 | 33 | println!("{}", mem.values().sum::()); 34 | } 35 | -------------------------------------------------------------------------------- /day14b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day14b" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "regex", 19 | ] 20 | 21 | [[package]] 22 | name = "memchr" 23 | version = "2.4.1" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 26 | 27 | [[package]] 28 | name = "regex" 29 | version = "1.5.4" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 32 | dependencies = [ 33 | "aho-corasick", 34 | "memchr", 35 | "regex-syntax", 36 | ] 37 | 38 | [[package]] 39 | name = "regex-syntax" 40 | version = "0.6.25" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 43 | -------------------------------------------------------------------------------- /day14b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day14b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | regex = "1.4" 12 | -------------------------------------------------------------------------------- /day14b/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use std::collections::HashMap; 3 | 4 | #[rustfmt::skip] 5 | pub fn main() { 6 | let re = Regex::new(r#"^mem\[(\d+)\] = (\d+)$"#).unwrap(); 7 | let mut mem = HashMap::new(); 8 | let mut float_addrs = vec![]; 9 | let mut whitelist = 0; 10 | 11 | for line in include_str!("../input.txt").lines() { 12 | if line.starts_with("me") { 13 | let cap = re.captures(&line).unwrap(); 14 | let addr = cap[1].parse::().unwrap() & whitelist; 15 | let val = cap[2].parse().unwrap(); 16 | for float_addr in &float_addrs { 17 | mem.insert(addr | float_addr, val); 18 | } 19 | } else { 20 | let mut float_base = 0; 21 | let mut float_bits = vec![]; 22 | float_addrs.clear(); 23 | whitelist = 0; 24 | 25 | line.split(" = ") 26 | .nth(1) 27 | .unwrap() 28 | .bytes() 29 | .rev() 30 | .enumerate() 31 | .for_each(|(i, b)| match b { 32 | b'0' => whitelist |= 1 << i, 33 | b'1' => float_base |= 1 << i, 34 | b'X' => float_bits.push(i), 35 | _ => unreachable!(), 36 | }); 37 | 38 | float_addrs = (0..2usize.pow(float_bits.len() as u32)) 39 | .map(|template| float_bits 40 | .iter() 41 | .enumerate() 42 | .fold(float_base, |addr, (i, fb)| addr | (template & 1 << i) << fb - i) 43 | ) 44 | .collect(); 45 | } 46 | } 47 | 48 | println!("{}", mem.values().sum::()); 49 | } 50 | -------------------------------------------------------------------------------- /day15a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day15a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day15a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day15a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day15a/input.txt: -------------------------------------------------------------------------------- 1 | 6,4,12,1,20,0,16 2 | -------------------------------------------------------------------------------- /day15a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut nums = include_str!("../input.txt") 3 | .trim_end() 4 | .split(',') 5 | .map(|n| n.parse().unwrap()) 6 | .collect::>(); 7 | nums.reserve(2020); 8 | 9 | while nums.len() < 2020 { 10 | let last = nums.last().unwrap(); 11 | match nums 12 | .iter() 13 | .rev() 14 | .enumerate() 15 | .skip(1) 16 | .find(|(_, n)| n == &last) 17 | { 18 | Some((i, _)) => nums.push(i), 19 | None => nums.push(0), 20 | } 21 | } 22 | 23 | println!("{}", nums.last().unwrap()); 24 | } 25 | -------------------------------------------------------------------------------- /day15b/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = "fat" 7 | debug = false 8 | debug-assertions = false 9 | panic = "abort" 10 | incremental = false 11 | codegen-units = 1 12 | -------------------------------------------------------------------------------- /day15b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day15b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day15b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day15b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day15b/input.txt: -------------------------------------------------------------------------------- 1 | 6,4,12,1,20,0,16 2 | -------------------------------------------------------------------------------- /day15b/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::collections::{hash_map::Entry, HashMap}; 2 | 3 | const COUNT: u32 = 30_000_000; 4 | const BOUNDRY: u32 = COUNT / 10; 5 | 6 | pub fn main() { 7 | let mut input: Vec<_> = include_str!("../input.txt") 8 | .trim_end() 9 | .split(',') 10 | .map(|n| n.parse().unwrap()) 11 | .collect(); 12 | 13 | let mut cur = input.remove(input.len() - 1); 14 | let mut high: HashMap = HashMap::with_capacity(1024 * 256); 15 | let mut low: [u32; BOUNDRY as usize] = [0; BOUNDRY as usize]; 16 | input 17 | .iter() 18 | .enumerate() 19 | .for_each(|(i, &n)| low[n as usize] = i as u32 + 1); 20 | 21 | for i in input.len() as u32 + 1..COUNT { 22 | if cur < BOUNDRY { 23 | let lownum = &mut low[cur as usize]; 24 | cur = if *lownum == 0 { 0 } else { i - *lownum }; 25 | // cur = (-*lownum >> 31 & 1) * (i - *lownum); 26 | *lownum = i; 27 | } else { 28 | match high.entry(cur) { 29 | Entry::Occupied(mut occup) => cur = i - occup.insert(i), 30 | Entry::Vacant(vacant) => { 31 | vacant.insert(i); 32 | cur = 0; 33 | } 34 | } 35 | } 36 | } 37 | 38 | println!("{}", cur); 39 | } 40 | -------------------------------------------------------------------------------- /day16a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day16a" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "regex", 19 | ] 20 | 21 | [[package]] 22 | name = "memchr" 23 | version = "2.4.1" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 26 | 27 | [[package]] 28 | name = "regex" 29 | version = "1.5.4" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 32 | dependencies = [ 33 | "aho-corasick", 34 | "memchr", 35 | "regex-syntax", 36 | ] 37 | 38 | [[package]] 39 | name = "regex-syntax" 40 | version = "0.6.25" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 43 | -------------------------------------------------------------------------------- /day16a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day16a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | regex = "1.4" 12 | -------------------------------------------------------------------------------- /day16a/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | 3 | pub fn main() { 4 | let re = Regex::new(r#"^.*: (\d+)-(\d+) or (\d+)-(\d+)$"#).unwrap(); 5 | let data = include_str!("../input.txt"); 6 | 7 | let rules: Vec<(usize, usize, usize, usize)> = data 8 | .lines() 9 | .take_while(|l| !l.is_empty()) 10 | .map(|rule| { 11 | let cap = re.captures(rule).unwrap(); 12 | ( 13 | cap[1].parse::().unwrap(), 14 | cap[2].parse::().unwrap(), 15 | cap[3].parse::().unwrap(), 16 | cap[4].parse::().unwrap(), 17 | ) 18 | }) 19 | .collect(); 20 | 21 | println!( 22 | "{}", 23 | data.lines() 24 | .skip(rules.len() + 1) 25 | .skip_while(|&t| t != "nearby tickets:") 26 | .skip(1) 27 | .flat_map(|ticket| { 28 | ticket 29 | .split(',') 30 | .map(|field| field.parse::().unwrap()) 31 | }) 32 | .filter(|&f| { 33 | !rules 34 | .iter() 35 | .any(|r| (f >= r.0 && f <= r.1) || (f >= r.2 && f <= r.3)) 36 | }) 37 | .sum::(), 38 | ); 39 | } 40 | -------------------------------------------------------------------------------- /day16b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "day16b" 16 | version = "0.1.0" 17 | dependencies = [ 18 | "regex", 19 | ] 20 | 21 | [[package]] 22 | name = "memchr" 23 | version = "2.4.1" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 26 | 27 | [[package]] 28 | name = "regex" 29 | version = "1.5.4" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 32 | dependencies = [ 33 | "aho-corasick", 34 | "memchr", 35 | "regex-syntax", 36 | ] 37 | 38 | [[package]] 39 | name = "regex-syntax" 40 | version = "0.6.25" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 43 | -------------------------------------------------------------------------------- /day16b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day16b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | regex = "1.4" 12 | -------------------------------------------------------------------------------- /day16b/src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use std::collections::{HashMap, HashSet}; 3 | 4 | pub fn main() { 5 | let re = Regex::new(r#"^(.*): (\d+)-(\d+) or (\d+)-(\d+)$"#).unwrap(); 6 | let input = include_str!("../input.txt"); 7 | 8 | let rules: Vec<(usize, usize, usize, usize, &str)> = input 9 | .lines() 10 | .take_while(|l| !l.is_empty()) 11 | .map(|rule| { 12 | let cap = re.captures(rule).unwrap(); 13 | ( 14 | cap[2].parse::().unwrap(), 15 | cap[3].parse::().unwrap(), 16 | cap[4].parse::().unwrap(), 17 | cap[5].parse::().unwrap(), 18 | cap.get(1).unwrap().as_str(), 19 | ) 20 | }) 21 | .collect(); 22 | 23 | let my_ticket = tickets("your ticket:", &input).next().unwrap(); 24 | let tickets: Vec<_> = tickets("nearby tickets:", &input) 25 | .filter(|t| { 26 | t.iter().all(|&f| { 27 | rules 28 | .iter() 29 | .any(|r| (f >= r.0 && f <= r.1) || (f >= r.2 && f <= r.3)) 30 | }) 31 | }) 32 | .collect(); 33 | 34 | #[rustfmt::skip] 35 | let mut rule_fields: HashMap<_, HashSet<_>> = rules 36 | .iter() 37 | .map(|f| ( 38 | f.4, (0..tickets[0].len()) 39 | .filter(|i| { 40 | tickets 41 | .iter() 42 | .map(|t| t[*i]) 43 | .filter(|&v| (v >= f.0 && v <= f.1) || (v >= f.2 && v <= f.3)) 44 | .count() 45 | == tickets.len() 46 | }) 47 | .collect(), 48 | )) 49 | .collect(); 50 | 51 | let mut map = vec![""; rules.len()]; 52 | for _ in 0..map.len() { 53 | let (&name, p) = rule_fields 54 | .iter() 55 | .filter(|(_, p)| p.len() == 1) 56 | .next() 57 | .unwrap(); 58 | let pos = p.iter().next().unwrap().clone(); 59 | map[pos] = name; 60 | rule_fields.values_mut().for_each(|p| { 61 | p.remove(&pos); 62 | }); 63 | rule_fields.remove(&name); 64 | } 65 | 66 | println!( 67 | "{}", 68 | my_ticket 69 | .into_iter() 70 | .enumerate() 71 | .map(|(i, v)| (map[i], v)) 72 | .filter(|(f, _)| f.starts_with("dep")) 73 | .map(|(_, v)| v) 74 | .product::() 75 | ); 76 | } 77 | 78 | fn tickets(header: &'static str, input: &'static str) -> impl Iterator> { 79 | input 80 | .lines() 81 | .skip_while(move |&t| t != header) 82 | .skip(1) 83 | .take_while(|t| !t.is_empty()) 84 | .map(|t| t.split(',').map(|f| f.parse().unwrap()).collect()) 85 | } 86 | -------------------------------------------------------------------------------- /day17a/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = "fat" 7 | debug = false 8 | debug-assertions = false 9 | panic = "abort" 10 | incremental = false 11 | codegen-units = 1 12 | -------------------------------------------------------------------------------- /day17a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day17a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day17a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day17a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day17a/input.txt: -------------------------------------------------------------------------------- 1 | ....#... 2 | .#..###. 3 | .#.#.### 4 | .#....#. 5 | ...#.#.# 6 | #....... 7 | ##....#. 8 | .##..#.# -------------------------------------------------------------------------------- /day17a/src/main.rs: -------------------------------------------------------------------------------- 1 | const CYCLES: usize = 6; 2 | 3 | #[rustfmt::skip] 4 | pub fn main() { 5 | let slice: Vec> = include_bytes!("../input.txt") 6 | .split(|&b| b == b'\n') 7 | .map(|row| row.into_iter().map(|&b| b == b'#').collect()) 8 | .collect(); 9 | 10 | let base_size = slice.len(); 11 | let max = CYCLES * 2 + base_size; 12 | let origin = max / 2; 13 | let neighbors: Vec<(isize, isize, isize)> = (0..3 * 3 * 3) 14 | .filter(|&i| i != 3 * 3 * 3 / 2) 15 | .map(|i| (i % 3 - 1, i / 3 % 3 - 1, i / 9 - 1)) 16 | .collect(); 17 | 18 | let mut cur = vec![vec![vec![false; max + 1]; max + 1]; max / 2 + 1]; 19 | let mut prev = cur.clone(); 20 | for x in 0..base_size { 21 | for y in 0..base_size { 22 | cur[0][origin - base_size / 2 + x][origin - base_size / 2 + y] = slice[x][y]; 23 | } 24 | } 25 | 26 | for cycle in 0..CYCLES { 27 | std::mem::swap(&mut cur, &mut prev); 28 | 29 | let size = base_size + cycle * 2; 30 | for x in 0..=cycle + 1 { 31 | for y in 0..=size { 32 | for z in 0..=size { 33 | let (y, z) = (origin - size / 2 + y, origin - size / 2 + z); 34 | let o = neighbors 35 | .iter() 36 | .map(|&r| ( 37 | (x as isize + r.0).abs() as usize, 38 | (y as isize + r.1) as usize, 39 | (z as isize + r.2) as usize, 40 | )) 41 | .filter(|(x, y, z)| prev[*x][*y][*z]) 42 | .count(); 43 | cur[x][y][z] = if prev[x][y][z] { o == 2 || o == 3 } else { o == 3 }; 44 | } 45 | } 46 | } 47 | } 48 | 49 | println!( 50 | "{}", 51 | 2 * cur 52 | .iter() 53 | .flat_map(|y| y.iter().flat_map(|z| z.iter().filter(|&c| *c))) 54 | .count() 55 | - cur[0].iter().flat_map(|z| z.iter().filter(|&c| *c)).count() 56 | ); 57 | } 58 | -------------------------------------------------------------------------------- /day17b/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = "fat" 7 | debug = false 8 | debug-assertions = false 9 | panic = "abort" 10 | incremental = false 11 | codegen-units = 1 12 | -------------------------------------------------------------------------------- /day17b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day17b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day17b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day17b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day17b/input.txt: -------------------------------------------------------------------------------- 1 | ....#... 2 | .#..###. 3 | .#.#.### 4 | .#....#. 5 | ...#.#.# 6 | #....... 7 | ##....#. 8 | .##..#.# -------------------------------------------------------------------------------- /day17b/src/main.rs: -------------------------------------------------------------------------------- 1 | const CYCLES: usize = 6; 2 | 3 | #[rustfmt::skip] 4 | pub fn main() { 5 | let slice: Vec> = include_bytes!("../input.txt") 6 | .split(|&b| b == b'\n') 7 | .map(|row| row.into_iter().map(|&b| b == b'#').collect()) 8 | .collect(); 9 | 10 | let base_size = slice.len(); 11 | let max = CYCLES * 2 + base_size; 12 | let origin = max / 2; 13 | let neighbors: Vec<(isize, isize, isize, isize)> = (0..3 * 3 * 3 * 3) 14 | .filter(|&i| i != 3 * 3 * 3 * 3 / 2) 15 | .map(|i| (i % 3 - 1, i / 3 % 3 - 1, i / 9 % 3 - 1, i / 27 - 1)) 16 | .collect(); 17 | 18 | let mut cur = vec![vec![vec![vec![false; max + 1]; max + 1]; max / 2 + 1]; max / 2 + 1]; 19 | let mut prev = cur.clone(); 20 | for x in 0..base_size { 21 | for y in 0..base_size { 22 | cur[0][0][origin - base_size / 2 + x][origin - base_size / 2 + y] = slice[x][y]; 23 | } 24 | } 25 | 26 | for cycle in 0..CYCLES { 27 | std::mem::swap(&mut cur, &mut prev); 28 | 29 | let size = base_size + cycle * 2; 30 | for x in 0..=cycle + 1 { 31 | for y in 0..=cycle + 1 { 32 | for z in 0..=size { 33 | for w in 0..size { 34 | let (z, w) = (origin - size / 2 + z, origin - size / 2 + w); 35 | let o = neighbors 36 | .iter() 37 | .map(|&r| ( 38 | (x as isize + r.0).abs() as usize, 39 | (y as isize + r.1).abs() as usize, 40 | (z as isize + r.2) as usize, 41 | (w as isize + r.3) as usize, 42 | )) 43 | .filter(|(x, y, z, w)| prev[*x][*y][*z][*w]) 44 | .count(); 45 | cur[x][y][z][w] = if prev[x][y][z][w] { o == 2 || o == 3 } else { o == 3 }; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | println!( 53 | "{}", 54 | 4 * (cur.iter() 55 | .map(|y| { 56 | y.iter() 57 | .flat_map(|z| z.iter().flat_map(|w| w.iter().filter(|&c| *c))) 58 | .count() 59 | }) 60 | .sum::() 61 | - cur 62 | .iter() 63 | .map(|x| x[0].iter().flat_map(|z| z.iter().filter(|&c| *c)).count()) 64 | .sum::()) 65 | ); 66 | } 67 | -------------------------------------------------------------------------------- /day18a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "arrayvec" 7 | version = "0.5.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" 10 | 11 | [[package]] 12 | name = "bitflags" 13 | version = "1.3.2" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 16 | 17 | [[package]] 18 | name = "bitvec" 19 | version = "0.19.6" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33" 22 | dependencies = [ 23 | "funty", 24 | "radium", 25 | "tap", 26 | "wyz", 27 | ] 28 | 29 | [[package]] 30 | name = "cfg-if" 31 | version = "1.0.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 34 | 35 | [[package]] 36 | name = "day18a" 37 | version = "0.1.0" 38 | dependencies = [ 39 | "nom", 40 | ] 41 | 42 | [[package]] 43 | name = "funty" 44 | version = "1.1.0" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" 47 | 48 | [[package]] 49 | name = "lexical-core" 50 | version = "0.7.6" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" 53 | dependencies = [ 54 | "arrayvec", 55 | "bitflags", 56 | "cfg-if", 57 | "ryu", 58 | "static_assertions", 59 | ] 60 | 61 | [[package]] 62 | name = "memchr" 63 | version = "2.3.4" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" 66 | 67 | [[package]] 68 | name = "nom" 69 | version = "6.2.1" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "9c5c51b9083a3c620fa67a2a635d1ce7d95b897e957d6b28ff9a5da960a103a6" 72 | dependencies = [ 73 | "bitvec", 74 | "funty", 75 | "lexical-core", 76 | "memchr", 77 | "version_check", 78 | ] 79 | 80 | [[package]] 81 | name = "radium" 82 | version = "0.5.3" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" 85 | 86 | [[package]] 87 | name = "ryu" 88 | version = "1.0.6" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" 91 | 92 | [[package]] 93 | name = "static_assertions" 94 | version = "1.1.0" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" 97 | 98 | [[package]] 99 | name = "tap" 100 | version = "1.0.1" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" 103 | 104 | [[package]] 105 | name = "version_check" 106 | version = "0.9.3" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" 109 | 110 | [[package]] 111 | name = "wyz" 112 | version = "0.2.0" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" 115 | -------------------------------------------------------------------------------- /day18a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day18a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | nom = "6.0" 12 | -------------------------------------------------------------------------------- /day18a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(custom_inner_attributes)]#![rustfmt::skip] 2 | 3 | use nom::{character::is_digit, *}; 4 | 5 | pub fn main() { 6 | println!( 7 | "{}", 8 | include_bytes!("../input.txt") 9 | .split(|&b| b == b'\n') 10 | .map(|e| expr(e).unwrap().1) 11 | .sum::() 12 | ); 13 | } 14 | 15 | named!(digit, map!(take_while_m_n!(1, 1, is_digit), |d| (d[0] - b'0') as usize)); 16 | named!(unit, alt!(delimited!(tag!("("), expr, tag!(")")) | digit)); 17 | 18 | named!( 19 | expr, 20 | do_parse!( 21 | first: unit 22 | >> sum: fold_many1!( 23 | complete!(pair!(delimited!(tag!(" "), one_of!("+*"), tag!(" ")), unit)), 24 | first, 25 | |acc, (op, num)| match op { 26 | '+' => acc + num, 27 | '*' => acc * num, 28 | _ => unreachable!(), 29 | }) 30 | >> (sum) 31 | ) 32 | ); 33 | -------------------------------------------------------------------------------- /day18b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "arrayvec" 7 | version = "0.5.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" 10 | 11 | [[package]] 12 | name = "bitflags" 13 | version = "1.3.2" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 16 | 17 | [[package]] 18 | name = "bitvec" 19 | version = "0.19.6" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33" 22 | dependencies = [ 23 | "funty", 24 | "radium", 25 | "tap", 26 | "wyz", 27 | ] 28 | 29 | [[package]] 30 | name = "cfg-if" 31 | version = "1.0.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 34 | 35 | [[package]] 36 | name = "day18b" 37 | version = "0.1.0" 38 | dependencies = [ 39 | "nom", 40 | ] 41 | 42 | [[package]] 43 | name = "funty" 44 | version = "1.1.0" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" 47 | 48 | [[package]] 49 | name = "lexical-core" 50 | version = "0.7.6" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" 53 | dependencies = [ 54 | "arrayvec", 55 | "bitflags", 56 | "cfg-if", 57 | "ryu", 58 | "static_assertions", 59 | ] 60 | 61 | [[package]] 62 | name = "memchr" 63 | version = "2.3.4" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" 66 | 67 | [[package]] 68 | name = "nom" 69 | version = "6.2.1" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "9c5c51b9083a3c620fa67a2a635d1ce7d95b897e957d6b28ff9a5da960a103a6" 72 | dependencies = [ 73 | "bitvec", 74 | "funty", 75 | "lexical-core", 76 | "memchr", 77 | "version_check", 78 | ] 79 | 80 | [[package]] 81 | name = "radium" 82 | version = "0.5.3" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" 85 | 86 | [[package]] 87 | name = "ryu" 88 | version = "1.0.6" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" 91 | 92 | [[package]] 93 | name = "static_assertions" 94 | version = "1.1.0" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" 97 | 98 | [[package]] 99 | name = "tap" 100 | version = "1.0.1" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" 103 | 104 | [[package]] 105 | name = "version_check" 106 | version = "0.9.3" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" 109 | 110 | [[package]] 111 | name = "wyz" 112 | version = "0.2.0" 113 | source = "registry+https://github.com/rust-lang/crates.io-index" 114 | checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" 115 | -------------------------------------------------------------------------------- /day18b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day18b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | nom = "6.0" 12 | -------------------------------------------------------------------------------- /day18b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(custom_inner_attributes)]#![rustfmt::skip] 2 | 3 | use nom::{character::is_digit, *}; 4 | 5 | pub fn main() { 6 | println!( 7 | "{}", 8 | include_bytes!("../input.txt") 9 | .split(|&b| b == b'\n') 10 | .map(|e| expr(e).unwrap().1) 11 | .sum::() 12 | ); 13 | } 14 | 15 | named!(digit, map!(take_while_m_n!(1, 1, is_digit), |d| (d[0] - b'0') as usize)); 16 | named!(unit, alt!(delimited!(tag!("("), expr, tag!(")")) | digit)); 17 | 18 | named!( 19 | expr, 20 | do_parse!( 21 | first: expr_plus 22 | >> sum: fold_many0!( 23 | complete!(preceded!(tag!(" * "), expr_plus)), 24 | first, 25 | |acc, num| acc * num) 26 | >> (sum) 27 | ) 28 | ); 29 | named!( 30 | expr_plus, 31 | do_parse!( 32 | first: unit 33 | >> sum: fold_many0!( 34 | complete!(preceded!(tag!(" + "), unit)), 35 | first, 36 | |acc, num| acc + num) 37 | >> (sum) 38 | ) 39 | ); 40 | -------------------------------------------------------------------------------- /day19a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day19a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day19a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day19a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day19a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | pub fn main() { 4 | let input = include_str!("../input.txt"); 5 | let mut rules: Vec<(usize, Rule)> = input 6 | .lines() 7 | .take_while(|l| !l.trim().is_empty()) 8 | .map(|l| { 9 | let (n, rule) = l.split_once(": ").unwrap(); 10 | ( 11 | n.parse().unwrap(), 12 | if rule.starts_with('"') { 13 | Rule::Lit(rule.chars().nth(1).unwrap() as u8) 14 | } else { 15 | let parts: Vec<_> = rule.splitn(2, '|').collect(); 16 | let first = parts[0] 17 | .split_terminator(' ') 18 | .filter(|n| !n.is_empty()) 19 | .map(|n| n.parse().unwrap()) 20 | .collect(); 21 | 22 | if parts.len() == 1 { 23 | Rule::Seq(first) 24 | } else { 25 | let second = parts[1] 26 | .split_terminator(' ') 27 | .filter(|n| !n.is_empty()) 28 | .map(|n| n.parse().unwrap()) 29 | .collect(); 30 | Rule::SeqOr(first, second) 31 | } 32 | }, 33 | ) 34 | }) 35 | .collect(); 36 | rules.sort_unstable_by_key(|r| r.0); 37 | let rules: Vec = rules.into_iter().map(|r| r.1).collect(); 38 | 39 | let msgs: Vec<&str> = input.lines().skip_while(|l| !l.trim().is_empty()).collect(); 40 | 41 | println!( 42 | "{}", 43 | msgs.into_iter() 44 | .filter(|m| matches(m.as_bytes(), &rules, 0) 45 | .map(|n| n == m.len()) 46 | .unwrap_or(false)) 47 | .count(), 48 | ); 49 | } 50 | 51 | fn matches(msg: &[u8], rules: &[Rule], rule: usize) -> Option { 52 | if msg.is_empty() { 53 | return None; 54 | } 55 | 56 | match &rules[rule] { 57 | Rule::Lit(c) if &msg[0] == c => Some(1), 58 | Rule::Lit(_) => None, 59 | Rule::Seq(r) => r 60 | .into_iter() 61 | .try_fold(0, |c, r| matches(&msg[c..], rules, *r).map(|n| n + c)), 62 | Rule::SeqOr(r, s) => r 63 | .into_iter() 64 | .try_fold(0, |c, r| matches(&msg[c..], rules, *r).map(|n| n + c)) 65 | .or_else(|| { 66 | s.into_iter() 67 | .try_fold(0, |c, r| matches(&msg[c..], rules, *r).map(|n| n + c)) 68 | }), 69 | } 70 | } 71 | 72 | #[derive(Debug)] 73 | enum Rule { 74 | Lit(u8), 75 | Seq(Vec), 76 | SeqOr(Vec, Vec), 77 | } 78 | -------------------------------------------------------------------------------- /day19b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day19b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day19b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day19b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day19b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | pub fn main() { 4 | let (rules, msgs) = include_str!("../input.txt").split_once("\n\n").unwrap(); 5 | let mut rules: Vec<(usize, Rule)> = rules 6 | .lines() 7 | .map(|l| { 8 | let (n, rule) = l.split_once(": ").unwrap(); 9 | ( 10 | n.parse().unwrap(), 11 | if rule.starts_with('"') { 12 | Rule::Lit(rule.chars().nth(1).unwrap() as u8) 13 | } else { 14 | let parts: Vec<_> = rule.splitn(2, '|').collect(); 15 | let a = parts[0] 16 | .split_terminator(' ') 17 | .filter(|n| !n.is_empty()) 18 | .map(|n| n.parse().unwrap()) 19 | .collect(); 20 | 21 | if parts.len() == 1 { 22 | Rule::Seq(a) 23 | } else { 24 | let b = parts[1] 25 | .split_terminator(' ') 26 | .filter(|n| !n.is_empty()) 27 | .map(|n| n.parse().unwrap()) 28 | .collect(); 29 | Rule::SeqOr(a, b) 30 | } 31 | }, 32 | ) 33 | }) 34 | .collect(); 35 | rules.sort_unstable_by_key(|r| r.0); 36 | let rules: Vec<_> = rules.into_iter().map(|r| r.1).collect(); 37 | 38 | // rules[8] = Rule::SeqOr(vec![42], vec![42, 8]); 39 | // rules[11] = Rule::SeqOr(vec![42, 31], vec![42, 11, 31]); 40 | 41 | println!( 42 | "{}", 43 | msgs.lines() 44 | .filter(|msg| matches_42(msg.as_bytes(), &rules)) 45 | .count(), 46 | ); 47 | } 48 | 49 | enum Rule { 50 | Lit(u8), 51 | Seq(Vec), 52 | SeqOr(Vec, Vec), 53 | } 54 | 55 | fn matches_42(msg: &[u8], rules: &[Rule]) -> bool { 56 | (0..) 57 | .try_fold(msg, |msg, depth| match matches(msg, 42, rules) { 58 | Some(msg) if matches_31(depth, msg, rules) => Err(true), 59 | Some(msg) => Ok(msg), 60 | None => Err(false), 61 | }) 62 | .err() 63 | .unwrap() 64 | } 65 | 66 | fn matches_31(depth: usize, msg: &[u8], rules: &[Rule]) -> bool { 67 | (0..depth) 68 | .try_fold(msg, |msg, _| match matches(msg, 31, rules) { 69 | Some(msg) if msg.is_empty() => Err(true), 70 | Some(msg) => Ok(msg), 71 | None => Err(false), 72 | }) 73 | .err() 74 | .unwrap_or(false) 75 | } 76 | 77 | fn matches<'a>(msg: &'a [u8], rule: usize, rules: &[Rule]) -> Option<&'a [u8]> { 78 | match &rules[rule] { 79 | Rule::Lit(_) if msg.is_empty() => None, 80 | Rule::Lit(c) if &msg[0] == c => Some(&msg[1..]), 81 | Rule::Lit(_) => None, 82 | Rule::Seq(a) => a.into_iter().try_fold(msg, |m, &r| matches(m, r, rules)), 83 | Rule::SeqOr(a, b) => a 84 | .into_iter() 85 | .try_fold(msg, |m, &r| matches(m, r, rules)) 86 | .or_else(|| b.into_iter().try_fold(msg, |m, &r| matches(m, r, rules))), 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /day20a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day20a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day20a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day20a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day20a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | use std::collections::HashMap; 4 | 5 | const SIZE: usize = 10; 6 | 7 | #[rustfmt::skip] 8 | pub fn main() { 9 | let tiles = include_str!("../input.txt") 10 | .split("\n\n") 11 | .map(|data| { 12 | let (id, cells) = data.split_once('\n').unwrap(); 13 | let id = id[5..].trim_end_matches(":").parse().unwrap(); 14 | let cells: Vec = cells.bytes().filter(|&b| b != b'\n').collect(); 15 | (id, [ 16 | cells[0..SIZE].into_iter() 17 | .fold(0u16, |e, &c| e << 1 | (c == b'#') as u16), 18 | (0..SIZE).map(|i| cells[SIZE - 1 + i * SIZE]) 19 | .fold(0u16, |e, c| e << 1 | (c == b'#') as u16), 20 | cells[SIZE * (SIZE - 1)..SIZE * SIZE].into_iter() 21 | .fold(0u16, |e, &c| e << 1 | (c == b'#') as u16), 22 | (0..SIZE).map(|i| cells[i * SIZE]) 23 | .fold(0u16, |e, c| e << 1 | (c == b'#') as u16), 24 | ]) 25 | }) 26 | .collect::>(); 27 | 28 | let edges: HashMap = tiles 29 | .iter() 30 | .flat_map(|t| t.1.iter().map(|&e| [e, e.reverse_bits() >> 6])) 31 | .fold(HashMap::new(), |mut map, [a, b]| { 32 | *map.entry(a).or_default() += 1; 33 | *map.entry(b).or_default() += 1; 34 | map 35 | }); 36 | 37 | println!( 38 | "{}", 39 | tiles 40 | .iter() 41 | .filter(|t| t.1.iter().filter(|e| edges[e] > 1).take(3).count() < 3) 42 | .map(|t| t.0) 43 | .product::() 44 | ); 45 | } 46 | -------------------------------------------------------------------------------- /day20b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day20b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day20b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day20b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day20b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(array_windows, custom_inner_attributes, drain_filter, iterator_fold_self, str_split_once)] 2 | #![rustfmt::skip] 3 | 4 | use std::cell::RefCell; 5 | use std::collections::{HashMap, HashSet}; 6 | 7 | const SIZE: usize = 10; 8 | const FIELD_SIZE: usize = 12; 9 | const NEIGHBORS: [Pos; 4] = [(0, usize::MAX), (1, 0), (0, 1), (usize::MAX, 0)]; 10 | const MONSTER: [Pos; 15] = [(0, 1), (1, 2), (4, 2), (5, 1), (6, 1), (7, 2), (10, 2), (11, 1), (12, 1), (13, 2), (16, 2), (17, 1), (18, 0), (18, 1), (19, 1)]; 11 | 12 | type Pos = (usize, usize); 13 | type Field = [[Option; FIELD_SIZE]; FIELD_SIZE]; 14 | 15 | pub fn main() { 16 | let mut tiles = include_str!("../input.txt") 17 | .split("\n\n") 18 | .map(|d| Tile::from(d)) 19 | .collect::>(); 20 | 21 | // Count edge occurences 22 | // TODO: do not include reverse here, check reverse on lookup? 23 | let edges: HashMap> = tiles 24 | .iter() 25 | .fold(HashMap::new(), |mut map, tile| { 26 | for edge in &tile.edges { 27 | (*map.entry(*edge).or_default()).push(tile.id); 28 | (*map.entry(edge.reverse_bits() >> 6).or_default()).push(tile.id); 29 | } 30 | map 31 | }); 32 | 33 | // Find a corner, rotate unique edgest to top left 34 | let i = tiles 35 | .iter() 36 | .position(|t| t.edges.iter().filter(|e| edges[*e].len() > 1).take(3).count() < 3) 37 | .unwrap(); 38 | let mut tile = tiles.remove(i); 39 | let side = tile 40 | .edges 41 | .iter() 42 | .cycle() 43 | .map(|e| edges[e].len() < 2) 44 | .take(5) 45 | .collect::>() 46 | .array_windows() 47 | .position(|[a, b]| *a && *b) 48 | .unwrap(); 49 | tile.rot(3 - side); 50 | tile.orient(3 - side); 51 | 52 | // Create field, place corner, then place the rest 53 | let mut field: Field = Default::default(); 54 | let mut empties = HashSet::new(); 55 | place(&mut field, tile, (0, 0), &mut empties); 56 | while !empties.is_empty() { 57 | fit(&mut field, *empties.iter().next().unwrap(), &mut tiles, &mut empties, &edges); 58 | } 59 | 60 | // Build waters from tiles 61 | let mut waters: Vec> = field 62 | .iter() 63 | .flat_map(|tile_row| { 64 | (0..SIZE - 2).into_iter().map(move |y| { 65 | tile_row.iter().flat_map(|t| t.as_ref().unwrap().body[y].to_vec()).collect() 66 | }) 67 | }) 68 | .collect(); 69 | 70 | // Count waves, subtract monster bits 71 | println!( 72 | "{}", 73 | waters 74 | .iter() 75 | .map(|r| r.iter().filter(|&&b| b).count()) 76 | .sum::() 77 | - (0..8) 78 | .map(|orient| { 79 | waters.reverse(); 80 | if orient % 2 == 1 { 81 | rot(&mut waters, 1); 82 | } 83 | monsters(&mut waters) 84 | }) 85 | .filter(|&n| n > 0) 86 | .next() 87 | .unwrap() 88 | * MONSTER.len() 89 | ); 90 | } 91 | 92 | #[derive(Clone)] 93 | struct Tile { 94 | pub id: u16, 95 | pub edges: [u16; 4], 96 | pub body: Vec>, 97 | } 98 | 99 | impl Tile { 100 | fn from(data: &str) -> Self { 101 | let (id, cells) = data.split_once('\n').unwrap(); 102 | let id = id[5..].trim_end_matches(":").parse().unwrap(); 103 | let cells: Vec<_> = cells.bytes().filter(|&b| b != b'\n').map(|b| b == b'#').collect(); 104 | Self { 105 | id, 106 | edges: [ 107 | cells[0..SIZE].into_iter().fold(0u16, |e, &c| e << 1 | c as u16), 108 | (0..SIZE).map(|i| cells[SIZE - 1 + i * SIZE]).fold(0u16, |e, c| e << 1 | c as u16), 109 | cells[SIZE * (SIZE - 1)..SIZE * SIZE].into_iter().fold(0u16, |e, &c| e << 1 | c as u16), 110 | (0..SIZE).map(|i| cells[i * SIZE]).fold(0u16, |e, c| e << 1 | c as u16), 111 | ], 112 | body: cells[SIZE..SIZE * (SIZE - 1)] 113 | .chunks(SIZE) 114 | .map(|r| r[1..SIZE - 1].to_vec()) 115 | .collect(), 116 | } 117 | } 118 | 119 | fn flip(&mut self) { 120 | self.edges[1] = self.edges[1].reverse_bits() >> 6; 121 | self.edges[3] = self.edges[3].reverse_bits() >> 6; 122 | let (a, b) = self.edges.split_at_mut(2); 123 | std::mem::swap(&mut a[0], &mut b[0]); 124 | } 125 | 126 | fn rot(&mut self, rot: usize) { 127 | if rot == 1 || rot == 2 { 128 | self.edges[1] = self.edges[1].reverse_bits() >> 6; 129 | self.edges[3] = self.edges[3].reverse_bits() >> 6; 130 | } 131 | if rot == 2 || rot == 3 { 132 | self.edges[0] = self.edges[0].reverse_bits() >> 6; 133 | self.edges[2] = self.edges[2].reverse_bits() >> 6; 134 | } 135 | self.edges.rotate_right(rot); 136 | } 137 | 138 | fn orient(&mut self, orient: usize) { 139 | if orient >= 4 { 140 | self.body.reverse(); 141 | } 142 | rot(&mut self.body, orient % 4); 143 | } 144 | } 145 | 146 | fn place(field: &mut Field, tile: Tile, at: Pos, empties: &mut HashSet) { 147 | field[at.1][at.0] = Some(tile); 148 | empties.remove(&at); 149 | empties.extend( 150 | [(at.0 + 1, at.1), (at.0, at.1 + 1)] 151 | .iter() 152 | .filter(|p| p.0 < FIELD_SIZE && p.1 < FIELD_SIZE && field[p.1][p.0].is_none()) 153 | ); 154 | } 155 | 156 | fn fit( 157 | field: &mut Field, 158 | pos: Pos, 159 | tiles: &mut Vec, 160 | empties: &mut HashSet, 161 | edges: &HashMap>, 162 | ) { 163 | let match_edges: Vec<_> = NEIGHBORS 164 | .iter() 165 | .map(|p| (p.0.wrapping_add(pos.0), p.1.wrapping_add(pos.1))) 166 | .enumerate() 167 | .filter(|(_, p)| p.0 < FIELD_SIZE && p.1 < FIELD_SIZE) 168 | .filter_map(|(i, p)| field[p.1][p.0].as_ref().map(|p| (i, p))) 169 | .map(|(i, t)| (i, t.edges[(i + 2) % 4])) 170 | .collect(); 171 | 172 | let (side, edge) = match_edges[0]; 173 | let neighbor_id = field[pos.1 + NEIGHBORS[side].1][pos.0 + NEIGHBORS[side].0].as_ref().unwrap().id; 174 | let tile_id = *edges[&edge].iter().filter(|&id| id != &neighbor_id).next().unwrap(); 175 | 176 | let (i, tile) = tiles 177 | .iter() 178 | .enumerate() 179 | .filter(|(_, tile)| tile.id == tile_id) 180 | .filter_map(|(i, tile)| { 181 | (0..8) 182 | .filter_map(|orient| { 183 | let mut tile: Tile = tile.clone(); 184 | if orient >= 4 { 185 | tile.flip(); 186 | } 187 | tile.rot(orient % 4); 188 | 189 | if match_edges.iter().all(|(s, e)| &tile.edges[*s] == e) { 190 | tile.orient(orient); 191 | Some((i, tile)) 192 | } else { 193 | None 194 | } 195 | }) 196 | .next() 197 | }) 198 | .next() 199 | .unwrap(); 200 | 201 | tiles.remove(i); 202 | place(field, tile, pos, empties); 203 | } 204 | 205 | fn rot(arr: &mut Vec>, rot: usize) { 206 | if rot & 2 > 0 { 207 | arr.reverse(); 208 | arr.iter_mut().for_each(|r| r.reverse()); 209 | } 210 | if rot & 1 > 0 { 211 | let (template, len) = (arr.clone(), arr[0].len()); 212 | for x in 0..len { 213 | for y in 0..len { 214 | arr[y][x] = template[len - x - 1][y]; 215 | } 216 | } 217 | } 218 | } 219 | 220 | fn monsters(map: &mut Vec>) -> usize { 221 | let count = RefCell::new(0); 222 | map.iter() 223 | .skip(1) 224 | .enumerate() 225 | .take(map.len() - 2) 226 | .take_while(|(i, _)| *i < 3 || *count.borrow() > 0) 227 | .map(|(y, row)| { 228 | row.array_windows() 229 | .skip(17) 230 | .enumerate() 231 | .filter(|(x, [a, b, c])| { 232 | *a && *b && *c && MONSTER.iter().all(|(a, b)| map[b + y][a + x]) 233 | }) 234 | .count() 235 | }) 236 | .for_each(|n| *count.borrow_mut() += n); 237 | count.into_inner() 238 | } 239 | -------------------------------------------------------------------------------- /day21a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day21a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day21a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day21a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day21a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once, map_into_keys_values)] 2 | 3 | use std::cell::RefCell; 4 | use std::collections::{HashMap, HashSet}; 5 | 6 | pub fn main() { 7 | let food: Vec<_> = include_str!("../input.txt") 8 | .lines() 9 | .map(|food| { 10 | let (ingr, aller) = food.split_once(" (contains ").unwrap(); 11 | ( 12 | ingr.split(' ').collect(), 13 | aller.trim_end_matches(')').split(", ").collect(), 14 | ) 15 | }) 16 | .collect(); 17 | 18 | let good_ingr = take_good_ingr(&food, take_bad_ingr(&food).into_keys().collect()); 19 | println!( 20 | "{}", 21 | food.iter() 22 | .map(|f| f.0.iter().filter(|i| good_ingr.contains(*i)).count()) 23 | .sum::() 24 | ); 25 | } 26 | 27 | fn take_bad_ingr<'a>(food: &[(HashSet<&'a str>, HashSet<&'a str>)]) -> HashMap<&'a str, &'a str> { 28 | let mut rev: HashMap<&str, HashSet<_>> = food 29 | .into_iter() 30 | .flat_map(|food| food.1.iter().map(move |aller| (food, aller))) 31 | .fold(HashMap::new(), |mut map, (food, aller)| { 32 | map.entry(aller) 33 | .and_modify(|ingr| *ingr = ingr.intersection(&food.0).copied().collect()) 34 | .or_insert_with(|| food.0.clone()); 35 | map 36 | }); 37 | 38 | // Keep eliminating allergens with 1 ingredient from list 39 | let covered = RefCell::new(HashSet::new()); 40 | loop { 41 | let covering: HashSet<_> = rev 42 | .iter() 43 | .filter(|(aller, ings)| ings.len() == 1 && !covered.borrow().contains(*aller)) 44 | .map(|(aller, ings)| { 45 | covered.borrow_mut().insert(*aller); 46 | *ings.iter().next().unwrap() 47 | }) 48 | .collect(); 49 | match covering.is_empty() { 50 | false => rev 51 | .values_mut() 52 | .filter(|ings| ings.len() != 1) 53 | .for_each(|ings| *ings = ings.difference(&covering).copied().collect()), 54 | true => break, 55 | } 56 | } 57 | 58 | rev.into_iter() 59 | .map(|(aller, ingr)| (ingr.into_iter().next().unwrap(), aller)) 60 | .collect() 61 | } 62 | 63 | fn take_good_ingr<'a>( 64 | food: &[(HashSet<&'a str>, HashSet<&'a str>)], 65 | bad_ingr: Vec<&str>, 66 | ) -> HashSet<&'a str> { 67 | food.into_iter() 68 | .flat_map(|food| food.0.iter().copied()) 69 | .filter(|ingr| !bad_ingr.contains(ingr)) 70 | .collect() 71 | } 72 | -------------------------------------------------------------------------------- /day21b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day21b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day21b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day21b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day21b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once, map_into_keys_values)] 2 | 3 | use std::cell::RefCell; 4 | use std::collections::{HashMap, HashSet}; 5 | 6 | pub fn main() { 7 | let food: Vec<(HashSet<&str>, HashSet<&str>)> = include_str!("../input.txt") 8 | .lines() 9 | .map(|food| { 10 | let (ingr, aller) = food.split_once(" (contains ").unwrap(); 11 | ( 12 | ingr.split(' ').collect(), 13 | aller.trim_end_matches(')').split(", ").collect(), 14 | ) 15 | }) 16 | .collect(); 17 | 18 | let bad_ingr = take_bad_ingr(&food); 19 | let mut bad_ingr = bad_ingr.iter().collect::>(); 20 | bad_ingr.sort_by_key(|p| p.1); 21 | println!( 22 | "{}", 23 | bad_ingr 24 | .into_iter() 25 | .map(|(i, _)| *i) 26 | .collect::>() 27 | .join(",") 28 | ); 29 | } 30 | 31 | fn take_bad_ingr<'a>(food: &[(HashSet<&'a str>, HashSet<&'a str>)]) -> HashMap<&'a str, &'a str> { 32 | let mut rev: HashMap<&str, HashSet<_>> = food 33 | .into_iter() 34 | .flat_map(|food| food.1.iter().map(move |aller| (food, aller))) 35 | .fold(HashMap::new(), |mut map, (food, aller)| { 36 | map.entry(aller) 37 | .and_modify(|ingr| *ingr = ingr.intersection(&food.0).copied().collect()) 38 | .or_insert_with(|| food.0.clone()); 39 | map 40 | }); 41 | 42 | // Keep eliminating allergens with 1 ingredient from list 43 | let covered = RefCell::new(HashSet::new()); 44 | loop { 45 | let covering: HashSet<_> = rev 46 | .iter() 47 | .filter(|(aller, ings)| ings.len() == 1 && !covered.borrow().contains(*aller)) 48 | .map(|(aller, ings)| { 49 | covered.borrow_mut().insert(*aller); 50 | *ings.iter().next().unwrap() 51 | }) 52 | .collect(); 53 | match covering.is_empty() { 54 | false => rev 55 | .values_mut() 56 | .filter(|ings| ings.len() != 1) 57 | .for_each(|ings| *ings = ings.difference(&covering).copied().collect()), 58 | true => break, 59 | } 60 | } 61 | 62 | rev.into_iter() 63 | .map(|(aller, ingr)| (ingr.into_iter().next().unwrap(), aller)) 64 | .collect() 65 | } 66 | -------------------------------------------------------------------------------- /day22a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day22a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day22a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day22a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day22a/input.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 12 3 | 48 4 | 26 5 | 22 6 | 44 7 | 16 8 | 31 9 | 19 10 | 30 11 | 10 12 | 40 13 | 47 14 | 21 15 | 27 16 | 2 17 | 46 18 | 9 19 | 15 20 | 23 21 | 6 22 | 50 23 | 28 24 | 5 25 | 42 26 | 34 27 | 28 | Player 2: 29 | 14 30 | 45 31 | 4 32 | 24 33 | 1 34 | 7 35 | 36 36 | 29 37 | 38 38 | 33 39 | 3 40 | 13 41 | 11 42 | 17 43 | 39 44 | 43 45 | 8 46 | 41 47 | 32 48 | 37 49 | 35 50 | 49 51 | 20 52 | 18 53 | 25 54 | -------------------------------------------------------------------------------- /day22a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | use std::collections::VecDeque; 4 | 5 | pub fn main() { 6 | let (p1, p2) = include_str!("../input.txt").split_once("\n\n").unwrap(); 7 | let mut p1: VecDeque = p1.lines().skip(1).map(|n| n.parse().unwrap()).collect(); 8 | let mut p2: VecDeque = p2.lines().skip(1).map(|n| n.parse().unwrap()).collect(); 9 | 10 | while !p2.is_empty() && !p2.is_empty() { 11 | let (c1, c2) = p1.pop_front().zip(p2.pop_front()).unwrap(); 12 | if c1 > c2 { 13 | p1.push_back(c1); 14 | p1.push_back(c2); 15 | } else { 16 | p2.push_back(c2); 17 | p2.push_back(c1); 18 | } 19 | } 20 | 21 | println!( 22 | "{}", 23 | p1.iter() 24 | .chain(p2.iter()) 25 | .rev() 26 | .enumerate() 27 | .map(|(i, c)| (i + 1) * c) 28 | .sum::() 29 | ); 30 | } 31 | -------------------------------------------------------------------------------- /day22b/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = "fat" 7 | debug = false 8 | debug-assertions = false 9 | panic = "abort" 10 | incremental = false 11 | codegen-units = 1 12 | -------------------------------------------------------------------------------- /day22b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day22b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day22b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day22b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day22b/input.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 12 3 | 48 4 | 26 5 | 22 6 | 44 7 | 16 8 | 31 9 | 19 10 | 30 11 | 10 12 | 40 13 | 47 14 | 21 15 | 27 16 | 2 17 | 46 18 | 9 19 | 15 20 | 23 21 | 6 22 | 50 23 | 28 24 | 5 25 | 42 26 | 34 27 | 28 | Player 2: 29 | 14 30 | 45 31 | 4 32 | 24 33 | 1 34 | 7 35 | 36 36 | 29 37 | 38 38 | 33 39 | 3 40 | 13 41 | 11 42 | 17 43 | 39 44 | 43 45 | 8 46 | 41 47 | 32 48 | 37 49 | 35 50 | 49 51 | 20 52 | 18 53 | 25 54 | -------------------------------------------------------------------------------- /day22b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | use std::collections::HashSet; 4 | 5 | const ALLOC_SIZE: usize = 3400; 6 | 7 | #[rustfmt::skip] 8 | pub fn main() { 9 | let (p1, p2) = include_str!("../input.txt").split_once("\n\n").unwrap(); 10 | let p1 = Deck::from(&p1.lines().skip(1).map(|n| n.parse().unwrap()).collect::>()); 11 | let p2 = Deck::from(&p2.lines().skip(1).map(|n| n.parse().unwrap()).collect::>()); 12 | 13 | println!( 14 | "{}", 15 | if play(&p1, &p2) { p1 } else { p2 } 16 | .as_slice() 17 | .iter() 18 | .rev() 19 | .enumerate() 20 | .map(|(i, c)| (i + 1) * c) 21 | .sum::() 22 | ); 23 | } 24 | 25 | fn play(p1: &Deck, p2: &Deck) -> bool { 26 | let mut turns = HashSet::new(); 27 | while turns.insert((p1.as_slice(), p2.as_slice())) { 28 | if p1.len() == 0 || p2.len() == 0 { 29 | return p1.len() > 0; 30 | } 31 | 32 | let (c1, c2) = (p1.pop(), p2.pop()); 33 | if if c1 <= p1.len() && c2 <= p2.len() { 34 | let sp1 = Deck::from(&p1.as_slice()[..c1]); 35 | let sp2 = Deck::from(&p2.as_slice()[..c2]); 36 | sp1.as_slice().iter().max() >= sp2.as_slice().iter().max() || play(&sp1, &sp2) 37 | } else { 38 | c1 > c2 39 | } { 40 | p1.push(c1); 41 | p1.push(c2); 42 | } else { 43 | p2.push(c2); 44 | p2.push(c1); 45 | } 46 | } 47 | 48 | true 49 | } 50 | 51 | struct Deck(usize, usize, [usize; ALLOC_SIZE]); 52 | 53 | impl Deck { 54 | fn from(slice: &[usize]) -> Self { 55 | let mut cards = [0; ALLOC_SIZE]; 56 | cards[..slice.len()].copy_from_slice(&slice); 57 | Deck(0, slice.len(), cards) 58 | } 59 | 60 | fn as_slice(&self) -> &[usize] { 61 | &self.2[self.0..self.1] 62 | } 63 | 64 | fn len(&self) -> usize { 65 | self.1 - self.0 66 | } 67 | 68 | fn push(&self, card: usize) { 69 | unsafe { 70 | (*(&self.2 as *const _ as *mut [_; ALLOC_SIZE]))[self.1] = card; 71 | *(&self.1 as *const _ as *mut usize) += 1; 72 | } 73 | } 74 | 75 | fn pop(&self) -> usize { 76 | unsafe { 77 | *(&self.0 as *const _ as *mut usize) += 1; 78 | } 79 | self.2[self.0 - 1] 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /day23a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day23a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day23a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day23a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day23a/input.txt: -------------------------------------------------------------------------------- 1 | 418976235 2 | -------------------------------------------------------------------------------- /day23a/src/main.rs: -------------------------------------------------------------------------------- 1 | pub fn main() { 2 | let mut cups: Vec = include_bytes!("../input.txt") 3 | .into_iter() 4 | .filter(|&b| b != &b'\n') 5 | .map(|b| b - b'0') 6 | .collect(); 7 | let highest = *cups.iter().max().unwrap(); 8 | 9 | for _ in 0..100 { 10 | let cur = cups[0]; 11 | let removed = [cups.remove(1), cups.remove(1), cups.remove(1)]; 12 | for target in (1..cur).rev().chain((cur..=highest).rev()) { 13 | if let Some(pos) = cups.iter().position(|&c| c == target) { 14 | for removed in removed.iter().rev() { 15 | cups.insert(pos + 1, *removed); 16 | } 17 | break; 18 | } 19 | } 20 | 21 | cups.rotate_left(1); 22 | } 23 | 24 | let offset = cups.iter().position(|&c| c == 1).unwrap(); 25 | cups.rotate_left(offset); 26 | println!( 27 | "{}", 28 | cups.iter() 29 | .skip(1) 30 | .map(|c| (c + b'0') as char) 31 | .collect::() 32 | ); 33 | } 34 | -------------------------------------------------------------------------------- /day23b/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | rustflags = ["-C", "target-cpu=native"] 3 | 4 | [profile.release] 5 | opt-level = 3 6 | lto = "fat" 7 | debug = false 8 | debug-assertions = false 9 | panic = "abort" 10 | incremental = false 11 | codegen-units = 1 12 | -------------------------------------------------------------------------------- /day23b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day23b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day23b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day23b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day23b/input.txt: -------------------------------------------------------------------------------- 1 | 418976235 2 | -------------------------------------------------------------------------------- /day23b/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::mem::MaybeUninit; 2 | 3 | pub fn main() { 4 | let cups: Vec = include_bytes!("../input.txt") 5 | .into_iter() 6 | .filter(|&b| b != &b'\n') 7 | .map(|b| (b - b'0') as u32) 8 | .collect(); 9 | 10 | // Build cup index linked list up to 1000000 11 | let mut indices: [u32; 1_000_001] = unsafe { MaybeUninit::uninit().assume_init() }; 12 | let last = cups[1..].iter().fold(cups[0], |prev, cup| { 13 | indices[prev as usize] = *cup; 14 | *cup 15 | }); 16 | (cups.len() + 1..1_000_000).for_each(|c| indices[c] = c as u32 + 1); 17 | indices[last as usize] = cups.len() as u32 + 1; 18 | indices[1_000_000] = cups[0]; 19 | 20 | (0..10_000_000).fold(cups[0], |cur, _| { 21 | let a = indices[cur as usize]; 22 | let b = indices[a as usize]; 23 | let c = indices[b as usize]; 24 | 25 | let dest = (1..cur) 26 | .rev() 27 | .filter(|&n| n != a && n != b && n != c) 28 | .next() 29 | .unwrap_or_else(|| { 30 | (cur + 1..=1_000_000) 31 | .rev() 32 | .filter(|&n| n != a && n != b && n != c) 33 | .next() 34 | .unwrap() 35 | }); 36 | 37 | indices[cur as usize] = indices[c as usize]; 38 | indices[c as usize] = indices[dest as usize]; 39 | indices[dest as usize] = a; 40 | indices[cur as usize] 41 | }); 42 | 43 | let a = indices[1] as u64; 44 | let b = indices[a as usize] as u64; 45 | println!("{}", a * b); 46 | } 47 | -------------------------------------------------------------------------------- /day24a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day24a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day24a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day24a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day24a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(array_windows, split_inclusive)] 2 | 3 | use std::collections::HashSet; 4 | 5 | pub fn main() { 6 | println!( 7 | "{}", 8 | include_bytes!("../input.txt") 9 | .split_inclusive(|&b| b == b'\n') 10 | .map(|line| { 11 | line.array_windows().fold((0, 0), |(x, y), v| match v { 12 | [b'e', _] => (x, y + 1), 13 | [b'w', _] => (x, y - 1), 14 | [b's', b'e'] => (x + 1, y), 15 | [b's', b'w'] => (x + 1, y + 1), 16 | [b'n', b'e'] => (x - 1, y - 1), 17 | [b'n', b'w'] => (x - 1, y), 18 | _ => unreachable!(), 19 | }) 20 | }) 21 | .fold(HashSet::new(), |mut map, coord| { 22 | if !map.remove(&coord) { 23 | map.insert(coord); 24 | } 25 | map 26 | }) 27 | .len() 28 | ); 29 | } 30 | -------------------------------------------------------------------------------- /day24b/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day24b" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day24b/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day24b" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day24b/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(array_windows, split_inclusive)] 2 | 3 | use std::collections::{HashMap, HashSet}; 4 | 5 | const NEIGHBORS: [(i16, i16); 6] = [(0, 1), (0, -1), (1, 1), (1, 0), (-1, 0), (-1, -1)]; 6 | 7 | pub fn main() { 8 | let mut prev: HashSet<(i16, i16)> = include_bytes!("../input.txt") 9 | .split_inclusive(|&b| b == b'\n') 10 | .map(|line| { 11 | line.array_windows().fold((0, 0), |(x, y), v| match v { 12 | [b'e', _] => (x, y + 1), 13 | [b'w', _] => (x, y - 1), 14 | [b's', b'e'] => (x + 1, y), 15 | [b's', b'w'] => (x + 1, y + 1), 16 | [b'n', b'e'] => (x - 1, y - 1), 17 | [b'n', b'w'] => (x - 1, y), 18 | _ => unreachable!(), 19 | }) 20 | }) 21 | .fold(HashSet::new(), |mut map, coord| { 22 | if !map.remove(&coord) { 23 | map.insert(coord); 24 | } 25 | map 26 | }); 27 | let mut cur: HashSet<(i16, i16)> = HashSet::new(); 28 | let mut neighbors: HashMap<(i16, i16), usize> = HashMap::new(); 29 | 30 | for _ in 0..100 { 31 | for (x, y) in &prev { 32 | for (xx, yy) in &NEIGHBORS { 33 | *neighbors.entry((x + xx, y + yy)).or_default() += 1; 34 | } 35 | } 36 | 37 | cur.clear(); 38 | neighbors.drain().for_each(|(coord, count)| { 39 | let black = prev.contains(&coord); 40 | if black && count <= 2 || !black && count == 2 { 41 | cur.insert(coord); 42 | } 43 | }); 44 | std::mem::swap(&mut prev, &mut cur); 45 | } 46 | 47 | println!("{}", prev.len()); 48 | } 49 | -------------------------------------------------------------------------------- /day25a/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "day25a" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /day25a/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day25a" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [lib] 8 | path = "src/main.rs" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /day25a/input.txt: -------------------------------------------------------------------------------- 1 | 14082811 2 | 5249543 3 | -------------------------------------------------------------------------------- /day25a/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(str_split_once)] 2 | 3 | #[rustfmt::skip] 4 | pub fn main() { 5 | let (d, c) = include_str!("../input.txt").trim().split_once('\n').unwrap(); 6 | let (d, c): (u64, u64) = (d.parse().unwrap(), c.parse().unwrap()); 7 | 8 | let (mut pk1, mut pk2, mut ek1, mut ek2) = (1, 1, 1, 1); 9 | loop { 10 | pk1 = pk1 * 7 % 20201227; 11 | pk2 = pk2 * 7 % 20201227; 12 | ek1 = ek1 * d % 20201227; 13 | ek2 = ek2 * c % 20201227; 14 | if pk1 == c { 15 | println!("{}", ek1); 16 | return; 17 | } else if pk2 == d { 18 | println!("{}", ek2); 19 | return; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /res/bench.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timvisee/advent-of-code-2020/7b2026d6d7a2a132543ca661b569e6e667b1a204/res/bench.ods -------------------------------------------------------------------------------- /res/bench_ms.log: -------------------------------------------------------------------------------- 1 | 0.16991 2 | 0.00726 3 | 0.55271 4 | 0.06852 5 | 0.00905 6 | 0.01298 7 | 0.15666 8 | 0.18453 9 | 0.00351 10 | 0.01078 11 | 0.03100 12 | 0.05605 13 | 2.25 14 | 1.71 15 | 0.02159 16 | 0.13093 17 | 0.04275 18 | 0.02501 19 | 0.00442 20 | 0.00484 21 | 6.88 22 | 6.56 23 | 0.01144 24 | 0.01104 25 | 0.00216 26 | 0.00351 27 | 0.27603 28 | 6.10 29 | 0.22727 30 | 410.94 31 | 0.20918 32 | 0.52627 33 | 0.35715 34 | 7.98 35 | 0.24608 36 | 0.22806 37 | 0.36369 38 | 0.52348 39 | 0.11111 40 | 0.46028 41 | 0.46420 42 | 0.29310 43 | 0.00266 44 | 3.42 45 | 0.00503 46 | 191.97 47 | 0.10485 48 | 43.21 49 | 26.88 50 | 713.81909 51 | 510.89 52 | -------------------------------------------------------------------------------- /res/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timvisee/advent-of-code-2020/7b2026d6d7a2a132543ca661b569e6e667b1a204/res/graph.png -------------------------------------------------------------------------------- /runner/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "runner" 3 | version = "0.1.0" 4 | authors = ["timvisee "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | rayon = "1.5" 9 | took = "0.1" 10 | 11 | # Days 12 | day01a = { path = "../day01a" } 13 | day01b = { path = "../day01b" } 14 | day02a = { path = "../day02a" } 15 | day02b = { path = "../day02b" } 16 | day03a = { path = "../day03a" } 17 | day03b = { path = "../day03b" } 18 | day04a = { path = "../day04a" } 19 | day04b = { path = "../day04b" } 20 | day05a = { path = "../day05a" } 21 | day05b = { path = "../day05b" } 22 | day06a = { path = "../day06a" } 23 | day06b = { path = "../day06b" } 24 | day07a = { path = "../day07a" } 25 | day07b = { path = "../day07b" } 26 | day08a = { path = "../day08a" } 27 | day08b = { path = "../day08b" } 28 | day09a = { path = "../day09a" } 29 | day09b = { path = "../day09b" } 30 | day10a = { path = "../day10a" } 31 | day10b = { path = "../day10b" } 32 | day11a = { path = "../day11a" } 33 | day11b = { path = "../day11b" } 34 | day12a = { path = "../day12a" } 35 | day12b = { path = "../day12b" } 36 | day13a = { path = "../day13a" } 37 | day13b = { path = "../day13b" } 38 | day14a = { path = "../day14a" } 39 | day14b = { path = "../day14b" } 40 | day15a = { path = "../day15a" } 41 | day15b = { path = "../day15b" } 42 | day16a = { path = "../day16a" } 43 | day16b = { path = "../day16b" } 44 | day17a = { path = "../day17a" } 45 | day17b = { path = "../day17b" } 46 | day18a = { path = "../day18a" } 47 | day18b = { path = "../day18b" } 48 | day19a = { path = "../day19a" } 49 | day19b = { path = "../day19b" } 50 | day20a = { path = "../day20a" } 51 | day20b = { path = "../day20b" } 52 | day21a = { path = "../day21a" } 53 | day21b = { path = "../day21b" } 54 | day22a = { path = "../day22a" } 55 | day22b = { path = "../day22b" } 56 | day23a = { path = "../day23a" } 57 | day23b = { path = "../day23b" } 58 | day24a = { path = "../day24a" } 59 | day24b = { path = "../day24b" } 60 | day25a = { path = "../day25a" } 61 | -------------------------------------------------------------------------------- /runner/src/bin/bench.rs: -------------------------------------------------------------------------------- 1 | use took::{Timer, Took}; 2 | 3 | const RUNS: usize = 100; 4 | 5 | fn main() { 6 | println!("Benchmarking all days with {} runs...", RUNS); 7 | 8 | let times: Vec<_> = runner::jobs() 9 | .into_iter() 10 | .map(|j| { 11 | ( 12 | j.1, 13 | (0..RUNS) 14 | .map(|_| { 15 | let took = Timer::new(); 16 | j.0(); 17 | took.took().into_std() 18 | }) 19 | .min() 20 | .unwrap(), 21 | ) 22 | }) 23 | .collect(); 24 | 25 | times.iter().for_each(|t| Took::from_std(t.1).describe(t.0)); 26 | Took::from_std(times.into_iter().map(|(_, t)| t).sum()).describe("everything"); 27 | } 28 | -------------------------------------------------------------------------------- /runner/src/bin/runner-par.rs: -------------------------------------------------------------------------------- 1 | use rayon::prelude::*; 2 | use took::Timer; 3 | 4 | fn main() { 5 | // Build threadpool with larger stack size 6 | rayon::ThreadPoolBuilder::new() 7 | .num_threads(4) 8 | .stack_size(12_800_000) 9 | .build_global() 10 | .unwrap(); 11 | 12 | let jobs = runner::jobs(); 13 | let timer = Timer::new(); 14 | (0..jobs.len()).into_par_iter().for_each(|i| jobs[i].0()); 15 | timer.took().describe("everything"); 16 | } 17 | -------------------------------------------------------------------------------- /runner/src/bin/runner.rs: -------------------------------------------------------------------------------- 1 | use took::Timer; 2 | 3 | fn main() { 4 | let timer = Timer::new(); 5 | runner::jobs().into_iter().for_each(|j| j.0()); 6 | timer.took().describe("everything"); 7 | } 8 | -------------------------------------------------------------------------------- /runner/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub fn jobs() -> &'static [(fn(), &'static str)] { 2 | &[ 3 | (day01a::main, "day01a"), 4 | (day01b::main, "day01b"), 5 | (day02a::main, "day02a"), 6 | (day02b::main, "day02b"), 7 | (day03a::main, "day03a"), 8 | (day03b::main, "day03b"), 9 | (day04a::main, "day04a"), 10 | (day04b::main, "day04b"), 11 | (day05a::main, "day05a"), 12 | (day05b::main, "day05b"), 13 | (day06a::main, "day06a"), 14 | (day06b::main, "day06b"), 15 | (day07a::main, "day07a"), 16 | (day07b::main, "day07b"), 17 | (day08a::main, "day08a"), 18 | (day08b::main, "day08b"), 19 | (day09a::main, "day09a"), 20 | (day09b::main, "day09b"), 21 | (day10a::main, "day10a"), 22 | (day10b::main, "day10b"), 23 | (day11a::main, "day11a"), 24 | (day11b::main, "day11b"), 25 | (day12a::main, "day12a"), 26 | (day12b::main, "day12b"), 27 | (day13a::main, "day13a"), 28 | (day13b::main, "day13b"), 29 | (day14a::main, "day14a"), 30 | (day14b::main, "day14b"), 31 | (day15a::main, "day15a"), 32 | (day15b::main, "day15b"), 33 | (day16a::main, "day16a"), 34 | (day16b::main, "day16b"), 35 | (day17a::main, "day17a"), 36 | (day17b::main, "day17b"), 37 | (day18a::main, "day18a"), 38 | (day18b::main, "day18b"), 39 | (day19a::main, "day19a"), 40 | (day19b::main, "day19b"), 41 | (day20a::main, "day20a"), 42 | (day20b::main, "day20b"), 43 | (day21a::main, "day21a"), 44 | (day21b::main, "day21b"), 45 | (day22a::main, "day22a"), 46 | (day22b::main, "day22b"), 47 | (day23a::main, "day23a"), 48 | (day23b::main, "day23b"), 49 | (day24a::main, "day24a"), 50 | (day24b::main, "day24b"), 51 | (day25a::main, "day25a"), 52 | ] 53 | } 54 | --------------------------------------------------------------------------------