-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_04.rs
190 lines (156 loc) Β· 4.78 KB
/
day_04.rs
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
use common::{solution, Answer};
solution!("Giant Squid", 4);
fn part_a(input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let winning = bingo.solve();
winning.0[winning.1].final_out(winning.2).into()
}
fn part_b(input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let losing = bingo.losing_solve();
losing.0[losing.1].final_out(losing.2).into()
}
#[derive(Debug, Clone)]
struct Bingo {
numbers: Vec<u32>,
boards: Vec<Board>,
take: u32,
}
#[derive(Debug, Clone)]
struct Board {
data: Vec<Vec<u32>>,
checked: Vec<Vec<bool>>,
}
impl Bingo {
fn parse_input(inp: &str) -> Self {
let mut lines = inp.lines();
let numbers = lines
.next()
.unwrap()
.split(',')
.map(|x| x.parse::<u32>().unwrap())
.collect();
let boards = Board::parse(inp);
Bingo {
numbers,
boards,
take: 5,
}
}
fn solve(self) -> (Vec<Board>, usize, u32) {
let mut nums = self.numbers.clone();
let mut tick = self.boards;
let mut take = self.take;
loop {
for _ in 1..take {
let num = nums.remove(0);
tick = Board::tick(tick, num);
if let Some(i) = Board::check(tick.clone()).first() {
return (tick, *i, num);
};
}
take += 1;
}
}
fn losing_solve(self) -> (Vec<Board>, usize, u32) {
let mut nums = self.numbers.clone();
let mut tick = self.boards;
let mut take = self.take;
let mut ret = Vec::new();
loop {
for _ in 1..take {
let num = nums.remove(0);
tick = Board::tick(tick, num);
let check = Board::check(tick.clone());
for i in check.clone() {
if !ret.contains(&i) {
ret.push(i);
}
}
if ret.len() == tick.len() {
return (tick, *ret.last().unwrap(), num);
}
}
take += 1;
}
}
}
impl Board {
fn parse(inp: &str) -> Vec<Self> {
let mut boards = Vec::new();
let inp = inp.replace('\r', "");
let raw_boards = inp.split("\n\n").skip(1);
for i in raw_boards {
let mut data = Vec::new();
let mut checked = Vec::new();
for line in i.lines() {
let nums = line
.split(' ')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect::<Vec<u32>>();
data.push(nums.clone());
checked.push(vec![false; nums.len()]);
}
boards.push(Board { data, checked });
}
boards
}
fn tick(data: Vec<Board>, num: u32) -> Vec<Board> {
let mut data = data;
for (board_i, board) in data.clone().iter().enumerate() {
for (row_i, row) in board.data.iter().enumerate() {
for (col_i, col) in row.iter().enumerate() {
if *col == num {
data[board_i].checked[row_i][col_i] = true;
}
}
}
}
data
}
fn check(data: Vec<Board>) -> Vec<usize> {
let mut out = Vec::new();
for (i, board) in data.iter().enumerate() {
let base_row_count = board.data[0].len();
let base_col_count = board.data.len();
// Check rows
for row in board.checked.iter() {
let mut row_count = 0;
for i in row.iter().take(base_col_count) {
if *i {
row_count += 1;
}
}
if row_count == base_row_count {
out.push(i);
}
}
// Check columns
for col_i in 0..base_col_count {
let mut col_count = 0;
for row_i in 0..base_row_count {
if board.checked[row_i][col_i] {
col_count += 1;
}
}
if col_count == base_row_count {
out.push(i);
}
}
}
out
}
// Get sum of all unchecked numbers
fn final_out(&self, winning: u32) -> usize {
let mut sum = 0;
for (row_i, row) in self.data.iter().enumerate() {
for (col_i, col) in row.iter().enumerate() {
if !self.checked[row_i][col_i] {
sum += *col;
}
}
}
sum as usize * winning as usize
}
}