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;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
part_one();
|
|
||||||
part_two();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part_one() {
|
|
||||||
let contents = fs::read_to_string("input")
|
let contents = fs::read_to_string("input")
|
||||||
.expect("Something went wrong reading the file");
|
.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
|
let mut boards: Vec<Vec<Vec<i32>>> = groups.iter().map(|group| group
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(|x| x
|
.map(|x| x
|
||||||
.split(' ')
|
.split_whitespace()
|
||||||
.filter(|x| *x != "")
|
|
||||||
.map(|x| x
|
.map(|x| x
|
||||||
.parse::<i32>()
|
.parse::<i32>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -40,73 +34,6 @@ fn part_one() {
|
|||||||
.collect()
|
.collect()
|
||||||
).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![];
|
let mut last_winning_boards: Vec<(usize, Vec<Vec<i32>>)> = vec![];
|
||||||
for num in bingo_nums {
|
for num in bingo_nums {
|
||||||
boards = boards
|
boards = boards
|
||||||
@ -122,21 +49,67 @@ fn part_two() {
|
|||||||
)
|
)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let winning_boards: Vec<(usize, Vec<Vec<i32>>)> = boards.iter().cloned().enumerate()
|
let winning_boards: Vec<(usize, Vec<Vec<i32>>)> = boards
|
||||||
.filter(|(i, board)|
|
.iter()
|
||||||
board.iter().filter(|row|
|
.cloned()
|
||||||
row[0] == -1 && row[1] == -1 && row[2] == -1 && row[3] == -1 && row[4] == -1
|
.enumerate()
|
||||||
).collect::<Vec<&Vec<i32>>>().len() > 0 ||
|
.filter(|(_, board)|
|
||||||
[0, 1, 2, 3, 4].into_iter().filter(|column|
|
board
|
||||||
board[0][*column] == -1 && board[1][*column] == -1 && board[2][*column] == -1 && board[3][*column] == -1 && board[4][*column] == -1
|
.iter()
|
||||||
).collect::<Vec<usize>>().len() > 0
|
.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();
|
).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() {
|
if winning_boards.len() == boards.len() {
|
||||||
let new_nums: HashSet<usize> = winning_boards.iter().map(|(i, x)| i).cloned().collect();
|
let new_nums: HashSet<usize> = winning_boards
|
||||||
let old_nums: HashSet<usize> = last_winning_boards.iter().map(|(i, x)| i).cloned().collect();
|
.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 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);
|
println!("Part 2, final score: {:?}", sum * num);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -144,3 +117,5 @@ fn part_two() {
|
|||||||
last_winning_boards = winning_boards;
|
last_winning_boards = winning_boards;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user