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,82
10 27 53 91 86
15 94 47 38 61
32 68 8 88 9
35 84 3 7 87
62 78 90 66 64
30 51 26 16 57
66 88 47 75 23
61 77 64 9 73
44 32 28 80 81
3 99 67 49 78
68 92 82 74 83
12 99 80 72 3
56 96 36 28 43
2 7 14 24 9
63 76 40 37 73
88 66 96 86 7
94 21 70 25 46
28 16 12 69 8
59 43 89 30 55
45 52 0 83 67
21 42 92 30 81
15 98 26 79 48
90 99 5 88 53
2 67 74 55 33
54 20 69 39 75
53 12 4 86 46
62 7 98 6 23
17 68 39 63 20
29 25 84 87 24
54 5 42 8 45
14 63 36 84 27
72 96 95 99 40
28 68 78 8 46
41 45 33 15 82
65 66 64 49 7
22 35 72 75 47
53 59 17 95 55
25 91 57 10 96
39 3 18 90 64
34 26 71 52 69
72 8 67 92 83
87 89 25 39 78
86 53 55 22 43
21 63 40 9 74
29 56 44 30 80
33 87 52 80 83
70 91 74 63 36
48 49 29 42 6
54 47 96 4 19
53 35 30 43 61
82 7 38 86 79
53 87 21 45 44
10 18 46 30 36
12 1 50 2 59
94 3 39 62 32
68 74 24 97 99
45 75 41 62 34
3 28 49 1 66
10 91 95 58 38
61 79 50 27 71
69 59 96 5 26
67 16 2 72 28
45 58 55 18 53
74 76 98 38 42
82 22 79 89 87
3 33 73 66 52
69 29 78 75 34
1 64 15 17 68
27 32 46 54 18
55 74 60 28 40
9 54 84 1 42
15 91 77 74 10
55 64 60 22 86
18 58 73 0 23
11 61 2 68 43
75 62 34 89 53
39 10 84 56 21
86 98 87 90 83
17 79 1 19 15
42 67 55 6 77
36 3 60 1 70
63 40 7 88 61
65 96 18 73 30
42 35 44 45 81
77 95 39 24 5
81 24 39 53 89
99 11 27 22 86
5 8 36 97 28
92 58 38 34 62
32 4 1 74 68
97 20 54 99 67
63 78 61 57 21
28 24 4 98 19
64 77 14 81 30
16 36 89 79 26
73 90 0 28 5
11 27 56 96 1
29 87 12 69 8
63 95 72 86 64
48 46 50 37 57
22 3 7 87 14
90 11 67 76 13
58 49 16 56 59
45 46 19 41 23
75 66 61 51 54
4 6 84 59 86
18 16 40 79 85
38 98 95 89 5
82 21 76 36 13
71 0 17 47 29
73 41 26 87 95
62 99 58 9 20
45 10 71 28 39
89 17 29 46 81
49 35 24 74 32
62 22 95 86 0
2 39 9 41 25
59 42 94 74 13
72 69 75 97 21
6 71 90 4 19
62 75 92 98 10
80 12 57 82 25
3 65 67 81 15
1 69 43 14 45
93 53 36 66 4
72 12 47 40 78
68 43 24 28 99
5 98 70 25 59
8 10 58 46 7
36 56 37 84 32
37 2 68 52 23
66 80 18 98 84
97 77 96 3 26
12 14 40 42 99
29 9 30 11 44
24 82 7 51 16
96 0 10 92 43
34 80 5 59 57
30 18 72 37 38
31 28 81 87 94
40 93 85 27 69
70 6 41 14 17
58 95 79 24 65
62 48 11 78 43
30 21 19 16 97
90 14 51 98 39
45 56 69 24 38
73 29 88 9 62
72 84 27 18 81
22 7 23 91 68
55 19 29 40 18
63 51 26 93 12
11 50 60 88 65
9 35 22 97 23
61 69 82 32 28
37 17 81 94 1
19 6 0 49 8
40 25 34 98 63
59 15 53 23 64
66 52 69 84 68
83 86 19 87 93
85 92 24 50 33
1 41 40 96 26
99 59 9 98 3
45 75 60 52 90
41 40 36 70 57
64 63 72 16 99
50 84 69 89 43
12 55 54 67 53
59 13 42 78 91
98 19 96 21 39
28 48 83 50 97
57 7 12 6 63
38 32 52 66 10
2 18 42 75 94
75 31 77 20 90
35 14 28 54 95
96 24 86 11 58
7 50 97 76 63
27 51 34 21 83
60 89 11 38 88
57 36 77 55 18
42 27 67 32 94
12 9 24 10 14
69 35 79 97 50
46 82 60 45 6
84 88 0 7 51
37 52 64 25 74
31 8 75 53 72
11 47 34 40 50
70 96 35 20 26
73 62 54 72 4
29 27 8 46 48
31 0 90 81 16
82 44 88 22 32
73 95 77 66 37
30 68 12 85 11
34 5 57 15 38
22 89 78 7 40
71 1 54 90 39
13 80 22 73 30
49 36 98 75 33
32 95 74 54 56
21 55 68 34 61
60 50 3 38 11
21 80 17 8 46
7 88 18 22 20
41 73 72 0 34
66 75 45 47 30
44 10 93 28 58
32 50 78 90 29
28 71 77 2 69
79 66 30 40 37
14 11 63 10 60
84 88 65 8 54
86 89 64 69 76
53 82 24 16 51
67 75 3 33 21
23 63 99 13 43
4 39 7 73 87
3 38 22 72 80
56 48 1 50 60
49 98 67 53 30
79 61 66 9 45
96 24 23 43 78
62 10 16 52 93
64 81 45 21 23
90 39 98 70 28
57 42 37 47 87
99 48 94 75 9
69 91 72 58 67
13 16 52 86 68
17 40 23 15 83
80 37 85 82 60
22 76 3 89 35
79 61 4 0 89
47 6 10 12 83
13 24 31 50 90
54 99 45 42 98
21 73 39 15 16
25 67 43 16 93
15 98 5 54 57
87 60 64 36 7
65 73 41 44 4
38 52 47 19 30
22 20 1 92 94
52 73 90 14 16
54 59 29 9 44
65 83 89 75 45
72 33 77 15 69
84 46 85 11 41
13 95 28 38 6
96 74 19 32 15
37 70 29 83 14
48 62 92 8 64
26 92 89 37 23
39 97 2 40 42
46 85 52 47 45
77 36 67 10 27
8 28 24 53 86
52 21 54 91 72
96 53 17 89 51
23 58 5 18 2
13 68 32 47 75
50 97 30 84 86
91 21 13 3 74
33 1 4 95 31
29 52 62 14 10
23 11 56 51 35
47 93 8 70 58
1 83 91 43 7
58 18 66 47 39
67 62 89 41 35
32 50 96 56 49
11 21 12 80 86
23 3 63 99 42
98 97 66 86 60
73 32 96 52 75
8 31 59 84 19
93 48 35 0 92
9 55 36 31 78
24 81 3 10 80
88 42 91 14 87
6 59 44 30 12
71 68 58 1 57
85 36 3 58 11
16 44 69 60 39
51 31 65 95 87
82 63 8 14 49
67 7 64 91 59
52 65 60 39 22
1 77 81 91 46
19 18 87 31 88
23 11 32 10 79
4 50 8 59 68
54 60 99 68 42
40 20 88 5 69
14 27 73 80 30
47 62 33 86 35
72 74 12 8 15
37 32 15 90 21
14 61 52 82 76
44 27 58 51 55
49 2 10 17 79
29 48 71 86 30
36 63 48 89 92
38 71 1 46 41
3 83 79 14 34
51 11 96 69 35
61 74 99 22 95
25 3 2 88 13
7 98 22 89 40
30 47 42 43 31
55 65 75 99 24
23 64 29 90 10
57 85 31 17 98
70 3 81 51 34
43 90 23 50 37
13 75 89 25 88
12 99 46 62 36
97 48 96 15 53
45 87 35 0 77
46 72 89 55 54
98 81 69 92 42
95 47 19 33 63
65 58 47 51 17
61 60 43 10 9
4 2 53 3 25
37 93 18 59 75
42 96 11 32 35
10 96 37 83 17
2 87 64 18 99
81 73 1 0 66
78 80 42 72 56
59 97 53 9 12
59 97 49 11 58
6 99 83 27 12
21 67 79 16 57
96 9 39 69 81
18 43 42 45 95
10 37 77 48 85
15 19 71 92 44
57 94 39 28 1
52 46 79 60 38
11 55 65 74 93
10 92 67 91 2
8 28 47 80 98
48 33 1 21 37
41 15 44 73 17
31 96 5 68 65
87 55 85 48 7
10 53 42 80 84
81 91 68 54 27
32 45 67 76 34
30 62 31 72 12
15 13 94 65 7
42 83 84 55 8
56 78 38 54 87
97 37 67 10 29
3 96 2 30 14
96 20 38 1 41
51 29 98 21 36
87 32 85 13 66
15 94 61 0 83
5 43 73 10 39
74 19 4 13 53
31 92 66 40 39
42 3 21 33 95
14 34 23 45 60
16 82 89 44 7
64 7 12 85 32
78 23 26 39 34
42 97 41 54 59
83 4 86 57 98
87 72 0 55 96
32 65 88 4 57
15 79 17 58 70
8 64 89 14 82
10 40 18 94 75
84 85 92 63 56
19 95 11 31 38
15 0 82 75 13
25 67 78 59 18
99 69 57 21 81
14 63 12 85 35
41 82 78 99 90
15 10 3 87 65
54 2 6 32 22
39 89 4 14 8
85 75 76 25 74
69 43 56 78 26
41 11 40 8 73
64 28 55 52 44
13 33 18 77 88
50 16 60 79 83
34 24 48 22 11
74 60 61 42 26
37 89 84 53 7
38 41 43 31 69
17 64 88 52 14
40 69 43 12 29
39 79 82 0 48
17 87 73 31 71
74 35 34 85 3
76 47 13 80 20
6 21 8 58 86
10 84 38 5 74
19 62 88 49 1
48 44 59 56 4
60 63 61 16 73
40 99 77 5 11
63 30 68 94 39
36 66 13 47 89
70 22 18 53 96
24 56 87 4 93
9 29 90 57 60
31 97 52 16 22
36 99 50 87 13
64 84 72 0 71
43 45 68 5 7
64 38 78 3 89
97 25 48 65 57
39 93 77 54 6
49 10 19 53 47
84 69 76 11 86
88 86 29 33 72
14 93 40 36 59
19 71 47 17 91
92 16 67 27 55
51 15 2 5 84
55 2 36 73 61
49 25 96 56 27
42 4 89 39 83
13 14 9 52 51
71 20 92 3 5
81 59 60 45 25
98 94 86 89 8
57 78 51 73 53
14 15 61 71 47
79 0 92 5 55
67 45 73 55 53
27 88 35 85 60
71 24 6 23 21
82 76 3 9 22
86 78 8 44 47
31 89 58 12 71
30 92 81 61 14
4 39 60 44 94
62 85 65 98 3
88 25 40 56 47
75 85 40 89 19
45 86 81 74 92
62 33 78 37 1
80 2 39 76 68
91 5 79 0 54
56 45 33 86 47
63 73 96 15 95
69 85 22 80 20
51 43 64 0 58
3 6 29 52 74
74 33 86 65 16
91 80 17 53 88
23 61 90 62 79
2 95 82 26 49
15 47 77 9 46
27 49 21 51 53
8 26 97 74 34
38 48 81 98 46
14 80 18 11 9
36 82 66 85 86
44 7 89 5 45
29 48 93 37 41
77 67 21 68 81
96 28 38 49 58
19 80 1 0 50
95 10 63 75 76
77 43 62 46 18
91 79 57 74 85
93 81 35 61 98
86 67 32 80 84
78 41 61 20 40
26 34 69 7 13
49 60 92 22 56
35 99 24 82 29
0 85 53 1 75
19 18 55 70 84
28 68 71 20 6
27 90 86 52 2
44 43 15 48 39
14 37 63 83 75
73 61 41 96 68
89 40 53 12 91
29 37 59 10 19
69 98 88 82 24
65 72 25 42 4
62 50 34 16 8
75 88 84 33 29
2 64 31 41 86
94 45 76 70 3
39 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)
}
}