-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
171 lines (147 loc) · 4.74 KB
/
main.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
use serde::{Deserialize, Serialize};
use std::{error::Error, fs::File, io::BufReader, time::Instant};
const NUM_COLUMNS: usize = 12;
const NUM_DIALS: usize = 5;
const NUM_LEVELS: usize = 4;
type Puzzle = [Dial; NUM_DIALS];
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
struct Dial {
zero: Option<Level>,
one: Option<Level>,
two: Option<Level>,
three: Option<Level>,
}
type Level = [Option<u32>; NUM_COLUMNS];
fn main() {
println!("Grecian Puzzle!");
let mut puzzle = read_puzzle().unwrap();
let start = Instant::now();
let solved_puzzle = solve(&mut puzzle).unwrap();
let elapsed = start.elapsed();
println!("Solved in {}ms", elapsed.as_millis());
let table = table(&solved_puzzle);
table.into_iter().for_each(|row| {
row.into_iter().for_each(|col| print!("{:3} ", col));
println!();
});
write_puzzle(&solved_puzzle).unwrap();
}
/// Solve the puzzle with brute force
fn solve(puzzle: &mut Puzzle) -> Result<Puzzle, Box<dyn Error>> {
// Loop over all Dials except the bottom one
for _ in 0..NUM_COLUMNS {
for _ in 0..NUM_COLUMNS {
for _ in 0..NUM_COLUMNS {
for _ in 0..NUM_COLUMNS {
if validate(&puzzle) {
return Ok(*puzzle);
}
// rotate level
rotate(&mut puzzle[0]);
}
rotate(&mut puzzle[1]);
}
rotate(&mut puzzle[2]);
}
rotate(&mut puzzle[3]);
}
return Err("No solution found".into());
fn rotate(dial: &mut Dial) {
rotate(&mut dial.zero);
rotate(&mut dial.one);
rotate(&mut dial.two);
rotate(&mut dial.three);
fn rotate(level: &mut Option<Level>) {
if let Some(level) = level {
level.rotate_right(1); // yo! that's a pretty handy method :)
}
}
}
}
/// Check if the puzzle is solved
fn validate(puzzle: &Puzzle) -> bool {
for col in 0..NUM_COLUMNS {
let mut sum = 0;
for lvl in 0..NUM_LEVELS {
for dial in puzzle.iter() {
let level = match lvl {
0 => &dial.zero,
1 => &dial.one,
2 => &dial.two,
3 => &dial.three,
_ => panic!("Invalid level"),
};
let level = match level {
Some(level) => level,
None => continue,
};
let value = level[col];
if let Some(value) = value {
sum += value;
break;
}
}
}
if sum != 42 {
return false;
}
}
return true;
}
/// Read in the puzzle from a file
fn read_puzzle() -> Result<Puzzle, Box<dyn Error>> {
let file = File::open("puzzle.json")?;
let reader = BufReader::new(file);
let puzzle: Puzzle = serde_json::from_reader(reader)?;
verify(&puzzle);
Ok(puzzle)
}
/// Just a sanity check to make sure the puzzle is valid
fn verify(puzzle: &Puzzle) {
// assert 5 dials
assert_eq!(puzzle.len(), NUM_DIALS);
for dial in puzzle.iter() {
// Put level in array and remove none
let levels = [dial.zero, dial.one, dial.two, dial.three];
let levels = levels.iter().filter(|x| x.is_some());
// Assert each level has 12 elements
for level in levels {
assert_eq!(level.unwrap().len(), NUM_COLUMNS);
}
}
}
/// Serialize the solved puzzle to a file
fn write_puzzle(puzzle: &Puzzle) -> Result<(), Box<dyn Error>> {
let file = File::create("solved_puzzle.json")?;
serde_json::to_writer_pretty(file, puzzle)?;
Ok(())
}
/// Convert a puzzle into a 2D array (table) representing the visible state of
/// the puzzle. If all columns of this table are equal to 42, the puzzle is
/// solved.
fn table(puzzle: &Puzzle) -> [[u32; NUM_COLUMNS]; NUM_LEVELS] {
let mut table = [[0; NUM_COLUMNS]; NUM_LEVELS];
for col in 0..NUM_COLUMNS {
for lvl in 0..NUM_LEVELS {
for dial in puzzle.iter() {
let level = match lvl {
0 => &dial.zero,
1 => &dial.one,
2 => &dial.two,
3 => &dial.three,
_ => panic!("Invalid level"),
};
let level = match level {
Some(level) => level,
None => continue,
};
let value = level[col];
if let Some(value) = value {
table[lvl][col] = value;
break;
}
}
}
}
return table;
}