Johann made me clean up my code a bit (rust)
This commit is contained in:
parent
dc1725a103
commit
dbc35b17ad
@ -2,11 +2,6 @@ use std::fs;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn main() {
|
||||
part_one();
|
||||
part_two();
|
||||
}
|
||||
|
||||
fn part_one() {
|
||||
let contents = fs::read_to_string("input")
|
||||
.expect("Something went wrong reading the file");
|
||||
|
||||
@ -29,8 +24,7 @@ fn part_one() {
|
||||
let mut boards: Vec<Vec<Vec<i32>>> = groups.iter().map(|group| group
|
||||
.split('\n')
|
||||
.map(|x| x
|
||||
.split(' ')
|
||||
.filter(|x| *x != "")
|
||||
.split_whitespace()
|
||||
.map(|x| x
|
||||
.parse::<i32>()
|
||||
.unwrap()
|
||||
@ -40,73 +34,6 @@ fn part_one() {
|
||||
.collect()
|
||||
).collect();
|
||||
|
||||
for num in bingo_nums {
|
||||
boards = boards
|
||||
.into_iter()
|
||||
.map(|board| board
|
||||
.into_iter()
|
||||
.map(|row| row
|
||||
.into_iter()
|
||||
.map(|x| if x == num { -1 } else { x })
|
||||
.collect()
|
||||
)
|
||||
.collect()
|
||||
)
|
||||
.collect();
|
||||
|
||||
let winning_boards: Vec<&Vec<Vec<i32>>> = boards.iter()
|
||||
.filter(|board|
|
||||
board.iter().filter(|row|
|
||||
row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1
|
||||
).collect::<Vec<&Vec<i32>>>().len() > 0 ||
|
||||
[0, 1, 2, 3, 4].into_iter().filter(|column|
|
||||
board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1
|
||||
).collect::<Vec<usize>>().len() > 0
|
||||
).collect();
|
||||
|
||||
if winning_boards.len() > 0 {
|
||||
let sum: i32 = winning_boards.into_iter().take(1).flatten().flatten().filter(|x| **x != -1).sum();
|
||||
println!("Part 1, final score: {}", sum * num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part_two() {
|
||||
let contents = fs::read_to_string("input")
|
||||
.expect("Something went wrong reading the file");
|
||||
|
||||
let mut groups: Vec<String> = contents
|
||||
.split("\n\n")
|
||||
.map(|x| x
|
||||
.to_string()
|
||||
)
|
||||
.collect();
|
||||
|
||||
let bingo_nums: Vec<i32> = groups
|
||||
.remove(0)
|
||||
.split(',')
|
||||
.map(|x| x
|
||||
.parse::<i32>()
|
||||
.unwrap()
|
||||
)
|
||||
.collect();
|
||||
|
||||
let mut boards: Vec<Vec<Vec<i32>>> = groups.iter().map(|group| group
|
||||
.split('\n')
|
||||
.map(|x| x
|
||||
.split(' ')
|
||||
.filter(|x| *x != "")
|
||||
.map(|x| x
|
||||
.parse::<i32>()
|
||||
.unwrap()
|
||||
)
|
||||
.collect()
|
||||
)
|
||||
.collect()
|
||||
).collect();
|
||||
|
||||
// Don't even ask okay, it works. I am a C programmer not a fp one :p
|
||||
let mut last_winning_boards: Vec<(usize, Vec<Vec<i32>>)> = vec![];
|
||||
for num in bingo_nums {
|
||||
boards = boards
|
||||
@ -122,21 +49,67 @@ fn part_two() {
|
||||
)
|
||||
.collect();
|
||||
|
||||
let winning_boards: Vec<(usize, Vec<Vec<i32>>)> = boards.iter().cloned().enumerate()
|
||||
.filter(|(i, board)|
|
||||
board.iter().filter(|row|
|
||||
row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1
|
||||
).collect::<Vec<&Vec<i32>>>().len() > 0 ||
|
||||
[0, 1, 2, 3, 4].into_iter().filter(|column|
|
||||
board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1
|
||||
).collect::<Vec<usize>>().len() > 0
|
||||
let winning_boards: Vec<(usize, Vec<Vec<i32>>)> = boards
|
||||
.iter()
|
||||
.cloned()
|
||||
.enumerate()
|
||||
.filter(|(_, board)|
|
||||
board
|
||||
.iter()
|
||||
.filter(|row|
|
||||
row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1
|
||||
)
|
||||
.collect::<Vec<&Vec<i32>>>()
|
||||
.len() > 0
|
||||
||
|
||||
(0..5)
|
||||
.filter(|column|
|
||||
board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1
|
||||
).collect::<Vec<usize>>()
|
||||
.len() > 0
|
||||
).collect();
|
||||
|
||||
if winning_boards.len() == 1 {
|
||||
let sum: i32 = winning_boards
|
||||
.iter()
|
||||
.map(|(_, x)| x)
|
||||
.take(1)
|
||||
.flatten().flatten()
|
||||
.filter(|x| **x != -1)
|
||||
.sum();
|
||||
|
||||
println!("Part 1, final score: {}", sum * num);
|
||||
}
|
||||
|
||||
if winning_boards.len() == boards.len() {
|
||||
let new_nums: HashSet<usize> = winning_boards.iter().map(|(i, x)| i).cloned().collect();
|
||||
let old_nums: HashSet<usize> = last_winning_boards.iter().map(|(i, x)| i).cloned().collect();
|
||||
let new_nums: HashSet<usize> = winning_boards
|
||||
.iter()
|
||||
.map(|(i, _)| i)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let old_nums: HashSet<usize> = last_winning_boards
|
||||
.iter()
|
||||
.map(|(i, _)| i)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let that_one_fucker: HashSet<usize> = &new_nums - &old_nums;
|
||||
let sum: i32 = winning_boards.get(that_one_fucker.iter().next().unwrap().clone()).unwrap().clone().1.into_iter().flatten().filter(|x| *x != -1).sum();
|
||||
|
||||
let sum: i32 = winning_boards.get(that_one_fucker
|
||||
.iter()
|
||||
.next()
|
||||
.unwrap()
|
||||
.clone()
|
||||
)
|
||||
.unwrap()
|
||||
.clone()
|
||||
.1
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|x| *x != -1)
|
||||
.sum();
|
||||
|
||||
println!("Part 2, final score: {:?}", sum * num);
|
||||
break;
|
||||
}
|
||||
@ -144,3 +117,5 @@ fn part_two() {
|
||||
last_winning_boards = winning_boards;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user