-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rs
135 lines (117 loc) · 3.69 KB
/
day4.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
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::collections::HashMap;
use std::path::PathBuf;
/*
A board is represented as a HashMap of number -> (x,y) position
*/
pub struct BoardState {
rows: HashMap<u32, u32>,
cols: HashMap<u32, u32>,
remaining_sum: u32,
has_won: bool,
}
pub fn load_input_file(input: PathBuf) -> (Vec<u32>, Vec<HashMap<u32, (u32, u32)>>) {
// Open the file and get a reader
let file = File::open(input).expect("Unable to open file.");
let mut reader = BufReader::new(file);
// Pull out the first line and parse into numbers
let mut draw_numbers: Vec<u32> = Vec::new();
let mut first_line = String::new();
reader.read_line(&mut first_line).unwrap();
let draw_split: Vec<&str> = first_line.trim().split(",").collect();
for token in draw_split {
draw_numbers.push(token.parse().unwrap());
}
// Boards
let mut boards: Vec<HashMap<u32, (u32, u32)>> = Vec::new();
let mut board: HashMap<u32, (u32, u32)> = HashMap::new();
let mut y_pos: u32 = 0;
for (_file_idx, read_line) in reader.lines().enumerate() {
let line = read_line.expect("Problem reading line!");
if line.chars().count() == 0 {
// If we've filled out a board then add it to `boards`
// and start a new `board`
if board.keys().len() > 0 {
boards.push(board);
board = HashMap::new();
y_pos = 0;
}
}
else {
// Add these numbers to the board
let board_split: Vec<&str> = line.split_whitespace().collect();
let mut x_pos = 0;
for token in board_split {
board.insert(token.parse().unwrap(), (x_pos, y_pos));
x_pos += 1;
}
y_pos += 1
}
}
// Add the last board to the boards
if board.keys().len() > 0 {
boards.push(board);
}
(draw_numbers, boards)
}
#[allow(mutable_borrow_reservation_conflict)]
pub fn run(input: Option<PathBuf>) {
// Load file
let (draw_numbers, boards) = load_input_file(input.unwrap());
// Initialize board states
let mut board_states: Vec<BoardState> = Vec::new();
for board in &boards {
let board_state = BoardState {
rows: HashMap::new(),
cols: HashMap::new(),
remaining_sum: board.keys().sum(),
has_won: false,
};
board_states.push(board_state)
}
// Play game
for draw_number in &draw_numbers {
// Update all boards
let mut board_num = 0;
for board in &boards {
let board_state = &mut board_states[board_num];
if board_state.has_won == false {
match board.get(&draw_number) {
None => (),
Some((x_pos, y_pos)) => {
// Update board state
board_state.remaining_sum -= *draw_number;
match board_state.rows.get(x_pos) {
None => {
board_state.rows.insert(*x_pos, 1);
},
Some(i) => {
if *i == 4 {
let score = board_state.remaining_sum * *draw_number;
board_state.has_won = true;
println!("Winner on board {}! [score: {}]", board_num, score);
}
board_state.rows.insert(*x_pos, i+1);
}
};
match board_state.cols.get(y_pos) {
None => {
board_state.cols.insert(*y_pos, 1);
},
Some(i) => {
if *i == 4 {
let score = board_state.remaining_sum * *draw_number;
board_state.has_won = true;
println!("Winner on board {}! [score: {}]", board_num, score);
}
board_state.cols.insert(*y_pos, i+1);
}
}
}
}
}
board_num += 1;
}
}
}