24SGYRW3AVJEZHHLFHIE7LQRG76IA2RUV6WDQRX4WWLJO4OWDSRQC # This file is automatically @generated by Cargo.# It is not intended for manual editing.version = 3[[package]]name = "advent"version = "1.0.0"dependencies = ["arrayvec","clap","itertools",][[package]]name = "ansi_term"version = "0.12.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"dependencies = ["winapi",][[package]]name = "arrayvec"version = "0.7.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"[[package]]name = "atty"version = "0.2.14"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"dependencies = ["hermit-abi","libc","winapi",][[package]]name = "bitflags"version = "1.3.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"[[package]]name = "clap"version = "2.34.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"dependencies = ["ansi_term","atty","bitflags","strsim","textwrap","unicode-width","vec_map",][[package]]name = "either"version = "1.6.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"[[package]]name = "hermit-abi"version = "0.1.19"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"dependencies = ["libc",][[package]]name = "itertools"version = "0.10.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"dependencies = ["either",][[package]]name = "libc"version = "0.2.108"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"[[package]]name = "strsim"version = "0.8.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"[[package]]name = "textwrap"version = "0.11.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"dependencies = ["unicode-width",][[package]]name = "unicode-width"version = "0.1.9"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"[[package]]name = "vec_map"version = "0.8.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"[[package]]name = "winapi"version = "0.3.9"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"dependencies = ["winapi-i686-pc-windows-gnu","winapi-x86_64-pc-windows-gnu",][[package]]name = "winapi-i686-pc-windows-gnu"version = "0.4.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"[[package]]name = "winapi-x86_64-pc-windows-gnu"version = "0.4.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]name = "advent"version = "1.0.0"edition = "2021"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]arrayvec = "0.7.2"clap = "2.34.0"itertools = "0.10.3"
17,2,33,86,38,41,4,34,91,61,11,81,3,59,29,71,26,44,54,89,46,9,85,62,23,76,45,24,78,14,58,48,57,40,21,49,7,99,8,56,50,19,53,55,10,94,75,68,6,83,84,88,52,80,73,74,79,36,70,28,37,0,42,98,96,92,27,90,47,20,5,77,69,93,31,30,95,25,63,65,51,72,60,16,12,64,18,13,1,35,15,66,67,43,22,87,97,32,39,8210 27 53 91 8615 94 47 38 6132 68 8 88 935 84 3 7 8762 78 90 66 6430 51 26 16 5766 88 47 75 2361 77 64 9 7344 32 28 80 813 99 67 49 7868 92 82 74 8312 99 80 72 356 96 36 28 432 7 14 24 963 76 40 37 7388 66 96 86 794 21 70 25 4628 16 12 69 859 43 89 30 5545 52 0 83 6721 42 92 30 8115 98 26 79 4890 99 5 88 532 67 74 55 3354 20 69 39 7553 12 4 86 4662 7 98 6 2317 68 39 63 2029 25 84 87 2454 5 42 8 4514 63 36 84 2772 96 95 99 4028 68 78 8 4641 45 33 15 8265 66 64 49 722 35 72 75 4753 59 17 95 5525 91 57 10 9639 3 18 90 6434 26 71 52 6972 8 67 92 8387 89 25 39 7886 53 55 22 4321 63 40 9 7429 56 44 30 8033 87 52 80 8370 91 74 63 3648 49 29 42 654 47 96 4 1953 35 30 43 6182 7 38 86 7953 87 21 45 4410 18 46 30 3612 1 50 2 5994 3 39 62 3268 74 24 97 9945 75 41 62 343 28 49 1 6610 91 95 58 3861 79 50 27 7169 59 96 5 2667 16 2 72 2845 58 55 18 5374 76 98 38 4282 22 79 89 873 33 73 66 5269 29 78 75 341 64 15 17 6827 32 46 54 1855 74 60 28 409 54 84 1 4215 91 77 74 1055 64 60 22 8618 58 73 0 2311 61 2 68 4375 62 34 89 5339 10 84 56 2186 98 87 90 8317 79 1 19 1542 67 55 6 7736 3 60 1 7063 40 7 88 6165 96 18 73 3042 35 44 45 8177 95 39 24 581 24 39 53 8999 11 27 22 865 8 36 97 2892 58 38 34 6232 4 1 74 6897 20 54 99 6763 78 61 57 2128 24 4 98 1964 77 14 81 3016 36 89 79 2673 90 0 28 511 27 56 96 129 87 12 69 863 95 72 86 6448 46 50 37 5722 3 7 87 1490 11 67 76 1358 49 16 56 5945 46 19 41 2375 66 61 51 544 6 84 59 8618 16 40 79 8538 98 95 89 582 21 76 36 1371 0 17 47 2973 41 26 87 9562 99 58 9 2045 10 71 28 3989 17 29 46 8149 35 24 74 3262 22 95 86 02 39 9 41 2559 42 94 74 1372 69 75 97 216 71 90 4 1962 75 92 98 1080 12 57 82 253 65 67 81 151 69 43 14 4593 53 36 66 472 12 47 40 7868 43 24 28 995 98 70 25 598 10 58 46 736 56 37 84 3237 2 68 52 2366 80 18 98 8497 77 96 3 2612 14 40 42 9929 9 30 11 4424 82 7 51 1696 0 10 92 4334 80 5 59 5730 18 72 37 3831 28 81 87 9440 93 85 27 6970 6 41 14 1758 95 79 24 6562 48 11 78 4330 21 19 16 9790 14 51 98 3945 56 69 24 3873 29 88 9 6272 84 27 18 8122 7 23 91 6855 19 29 40 1863 51 26 93 1211 50 60 88 659 35 22 97 2361 69 82 32 2837 17 81 94 119 6 0 49 840 25 34 98 6359 15 53 23 6466 52 69 84 6883 86 19 87 9385 92 24 50 331 41 40 96 2699 59 9 98 345 75 60 52 9041 40 36 70 5764 63 72 16 9950 84 69 89 4312 55 54 67 5359 13 42 78 9198 19 96 21 3928 48 83 50 9757 7 12 6 6338 32 52 66 102 18 42 75 9475 31 77 20 9035 14 28 54 9596 24 86 11 587 50 97 76 6327 51 34 21 8360 89 11 38 8857 36 77 55 1842 27 67 32 9412 9 24 10 1469 35 79 97 5046 82 60 45 684 88 0 7 5137 52 64 25 7431 8 75 53 7211 47 34 40 5070 96 35 20 2673 62 54 72 429 27 8 46 4831 0 90 81 1682 44 88 22 3273 95 77 66 3730 68 12 85 1134 5 57 15 3822 89 78 7 4071 1 54 90 3913 80 22 73 3049 36 98 75 3332 95 74 54 5621 55 68 34 6160 50 3 38 1121 80 17 8 467 88 18 22 2041 73 72 0 3466 75 45 47 3044 10 93 28 5832 50 78 90 2928 71 77 2 6979 66 30 40 3714 11 63 10 6084 88 65 8 5486 89 64 69 7653 82 24 16 5167 75 3 33 2123 63 99 13 434 39 7 73 873 38 22 72 8056 48 1 50 6049 98 67 53 3079 61 66 9 4596 24 23 43 7862 10 16 52 9364 81 45 21 2390 39 98 70 2857 42 37 47 8799 48 94 75 969 91 72 58 6713 16 52 86 6817 40 23 15 8380 37 85 82 6022 76 3 89 3579 61 4 0 8947 6 10 12 8313 24 31 50 9054 99 45 42 9821 73 39 15 1625 67 43 16 9315 98 5 54 5787 60 64 36 765 73 41 44 438 52 47 19 3022 20 1 92 9452 73 90 14 1654 59 29 9 4465 83 89 75 4572 33 77 15 6984 46 85 11 4113 95 28 38 696 74 19 32 1537 70 29 83 1448 62 92 8 6426 92 89 37 2339 97 2 40 4246 85 52 47 4577 36 67 10 278 28 24 53 8652 21 54 91 7296 53 17 89 5123 58 5 18 213 68 32 47 7550 97 30 84 8691 21 13 3 7433 1 4 95 3129 52 62 14 1023 11 56 51 3547 93 8 70 581 83 91 43 758 18 66 47 3967 62 89 41 3532 50 96 56 4911 21 12 80 8623 3 63 99 4298 97 66 86 6073 32 96 52 758 31 59 84 1993 48 35 0 929 55 36 31 7824 81 3 10 8088 42 91 14 876 59 44 30 1271 68 58 1 5785 36 3 58 1116 44 69 60 3951 31 65 95 8782 63 8 14 4967 7 64 91 5952 65 60 39 221 77 81 91 4619 18 87 31 8823 11 32 10 794 50 8 59 6854 60 99 68 4240 20 88 5 6914 27 73 80 3047 62 33 86 3572 74 12 8 1537 32 15 90 2114 61 52 82 7644 27 58 51 5549 2 10 17 7929 48 71 86 3036 63 48 89 9238 71 1 46 413 83 79 14 3451 11 96 69 3561 74 99 22 9525 3 2 88 137 98 22 89 4030 47 42 43 3155 65 75 99 2423 64 29 90 1057 85 31 17 9870 3 81 51 3443 90 23 50 3713 75 89 25 8812 99 46 62 3697 48 96 15 5345 87 35 0 7746 72 89 55 5498 81 69 92 4295 47 19 33 6365 58 47 51 1761 60 43 10 94 2 53 3 2537 93 18 59 7542 96 11 32 3510 96 37 83 172 87 64 18 9981 73 1 0 6678 80 42 72 5659 97 53 9 1259 97 49 11 586 99 83 27 1221 67 79 16 5796 9 39 69 8118 43 42 45 9510 37 77 48 8515 19 71 92 4457 94 39 28 152 46 79 60 3811 55 65 74 9310 92 67 91 28 28 47 80 9848 33 1 21 3741 15 44 73 1731 96 5 68 6587 55 85 48 710 53 42 80 8481 91 68 54 2732 45 67 76 3430 62 31 72 1215 13 94 65 742 83 84 55 856 78 38 54 8797 37 67 10 293 96 2 30 1496 20 38 1 4151 29 98 21 3687 32 85 13 6615 94 61 0 835 43 73 10 3974 19 4 13 5331 92 66 40 3942 3 21 33 9514 34 23 45 6016 82 89 44 764 7 12 85 3278 23 26 39 3442 97 41 54 5983 4 86 57 9887 72 0 55 9632 65 88 4 5715 79 17 58 708 64 89 14 8210 40 18 94 7584 85 92 63 5619 95 11 31 3815 0 82 75 1325 67 78 59 1899 69 57 21 8114 63 12 85 3541 82 78 99 9015 10 3 87 6554 2 6 32 2239 89 4 14 885 75 76 25 7469 43 56 78 2641 11 40 8 7364 28 55 52 4413 33 18 77 8850 16 60 79 8334 24 48 22 1174 60 61 42 2637 89 84 53 738 41 43 31 6917 64 88 52 1440 69 43 12 2939 79 82 0 4817 87 73 31 7174 35 34 85 376 47 13 80 206 21 8 58 8610 84 38 5 7419 62 88 49 148 44 59 56 460 63 61 16 7340 99 77 5 1163 30 68 94 3936 66 13 47 8970 22 18 53 9624 56 87 4 939 29 90 57 6031 97 52 16 2236 99 50 87 1364 84 72 0 7143 45 68 5 764 38 78 3 8997 25 48 65 5739 93 77 54 649 10 19 53 4784 69 76 11 8688 86 29 33 7214 93 40 36 5919 71 47 17 9192 16 67 27 5551 15 2 5 8455 2 36 73 6149 25 96 56 2742 4 89 39 8313 14 9 52 5171 20 92 3 581 59 60 45 2598 94 86 89 857 78 51 73 5314 15 61 71 4779 0 92 5 5567 45 73 55 5327 88 35 85 6071 24 6 23 2182 76 3 9 2286 78 8 44 4731 89 58 12 7130 92 81 61 144 39 60 44 9462 85 65 98 388 25 40 56 4775 85 40 89 1945 86 81 74 9262 33 78 37 180 2 39 76 6891 5 79 0 5456 45 33 86 4763 73 96 15 9569 85 22 80 2051 43 64 0 583 6 29 52 7474 33 86 65 1691 80 17 53 8823 61 90 62 792 95 82 26 4915 47 77 9 4627 49 21 51 538 26 97 74 3438 48 81 98 4614 80 18 11 936 82 66 85 8644 7 89 5 4529 48 93 37 4177 67 21 68 8196 28 38 49 5819 80 1 0 5095 10 63 75 7677 43 62 46 1891 79 57 74 8593 81 35 61 9886 67 32 80 8478 41 61 20 4026 34 69 7 1349 60 92 22 5635 99 24 82 290 85 53 1 7519 18 55 70 8428 68 71 20 627 90 86 52 244 43 15 48 3914 37 63 83 7573 61 41 96 6889 40 53 12 9129 37 59 10 1969 98 88 82 2465 72 25 42 462 50 34 16 875 88 84 33 292 64 31 41 8694 45 76 70 339 89 66 4 24
#![feature(array_methods, array_chunks)]extern crate arrayvec;extern crate clap;extern crate itertools;use clap::{Arg, App};use itertools::Itertools;use std::fs::File;use std::io::{self, BufRead, BufReader, Lines, Error};use std::path::Path;use types::TestCase;mod types;fn read_testcase<P>(filename: P) -> io::Result<TestCase>where P: AsRef<Path>, {let file = File::open(filename)?;parse_testcase(BufReader::new(file).lines())}fn parse_testcase(mut content: Lines<BufReader<File>>) -> Result<TestCase, Error> {let numbers: Vec<u8> = content.next().unwrap().unwrap().split(",").map(|n| n.parse::<u8>()).filter(Result::is_ok).map(Result::unwrap).collect();let boards: Vec<Vec<u8>> = content.map(Result::unwrap).flat_map(|line| line.split(" ").map(String::from).collect::<Vec<String>>()).map(|n: String| n.parse::<u8>().unwrap()).collect::<Vec<u8>>().into_iter().chunks(25).into_iter().map(|chunk| chunk.collect()).collect();Ok(TestCase::new(boards, numbers))}fn solve(mut test_case: TestCase) -> Result<u32, Error> {Ok(test_case.solve())}fn print_solution(result: Result<u32, Error>) {match result {Ok(solution) => print!("{}\n", solution),Err(_) => print!("Error solving for provided test case")};}fn main() {let matches = App::new("Advent of Code").version("1.0").author("Salvador P. <sisekeom@protonmail.com>").about("Solution for advent of code 2021 1.1").arg(Arg::with_name("filepath").help("path to the test case file").required(true).index(1)).get_matches();let filepath = matches.value_of("filepath").unwrap();read_testcase(filepath).map(solve).map(print_solution).unwrap();}
use arrayvec::ArrayVec;#[derive(Default, Copy, Clone)]struct Cell {number: u8,marked: bool}impl Cell {pub fn new(number: u8) -> Cell {Cell {number: number,marked: false}}pub fn is_marked(&self) -> bool {self.marked}pub fn get_number(&self) -> u8 {self.number}pub fn mark(&mut self, ball: &u8) {if self.number == *ball {self.marked = true}}}#[derive(Copy, Clone)]pub struct Board {numbers: [Cell;25]}impl Board {pub fn new(numbers: Vec<u8>) -> Board {let board: ArrayVec<Cell, 25> =IntoIterator::into_iter(numbers).map(|number| Cell::new(number)).collect();Board {numbers: board.into_inner().unwrap_or_else(|_| panic!("Array not completely filled"))}}pub fn mark(&mut self, ball: &u8) {self.numbers.iter_mut().map(|cell| cell.mark(ball)).for_each(drop);}pub fn did_already_win(&self) -> bool {self.check_rows_for_win() || self.check_columns_for_win()}pub fn calculate_points(&self, last_ball: u32) -> u32 {self.numbers.iter().filter(|cell| !cell.is_marked()).fold(0 as u32, |acc, cell| acc + cell.get_number() as u32) * last_ball}fn get_rows(&self) -> Vec<[Cell; 5]> {(0..5).map(|row| [self.numbers[row * 5], self.numbers[row * 5 + 1], self.numbers[row * 5 + 2], self.numbers[row * 5 + 3], self.numbers[row * 5 + 4]]).collect()}fn get_columns(&self) -> Vec<[Cell; 5]> {(0..5).map(|col| [self.numbers[col], self.numbers[col + 5], self.numbers[col + 10], self.numbers[col + 15], self.numbers[col + 20]]).collect()}fn check_rows_for_win(&self) -> bool {self.get_rows().iter().any(|row| row.iter().all(|cell| cell.is_marked()))}fn check_columns_for_win(&self) -> bool {self.get_columns().iter().any(|column| column.iter().all(|cell| cell.is_marked()))}}pub struct TestCase {pub boards: Vec<Board>,pub balls: Vec<u8>}impl TestCase {pub fn new(numbers: Vec<Vec<u8>>, balls: Vec<u8>) -> TestCase{TestCase {boards: numbers.into_iter().map(Board::new).collect(),balls: balls}}pub fn solve(&mut self) -> u32 {let mut last_ball: u8 = 0;self.balls.clone().into_iter().take_while(|x| {self.boards.iter_mut().for_each(|board| board.mark(x));self.boards.iter().filter(|board| !board.did_already_win()).count() > 1}).for_each(drop);let last_board: &mut Board = self.boards.iter_mut().find(|board| !board.did_already_win()).unwrap();self.balls.clone().into_iter().take_while(|x| {last_board.mark(x);last_ball = *x;!last_board.did_already_win()}).for_each(drop);last_board.calculate_points(last_ball as u32)}}