-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
251 lines (209 loc) · 6.37 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
-------Part 1-------- -------Part 2--------
Day Time Rank Score Time Rank Score
11 00:50:32 1393 0 00:57:12 1215 0
BENCHMARK RESULTS
*/
// allow bench feature when using unstable flag
// use: $ cargo +nightly bench --features unstable
#![cfg_attr(feature = "unstable", feature(test))]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
mod intcode;
use intcode::*;
use aoc_import_magic::{import_magic, PuzzleOptions};
use regex::Regex;
use std::{
collections::{HashMap, VecDeque},
io,
};
const DAY: i32 = 11;
type InputTypeSingle = IntcodeNumber;
type InputType = Vec<InputTypeSingle>;
type OutputType1 = usize;
type OutputType2 = OutputType1;
type TodaysPuzzleOptions = PuzzleOptions<InputType>;
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
struct Robot {
xx: i128,
yy: i128,
dir: Dir,
}
impl Robot {
fn pos(&self) -> (i128, i128) {
(self.xx, self.yy)
}
fn turn(&mut self, dir: i128) {
self.dir.turn(dir)
}
fn step(&mut self) {
match &self.dir {
Dir::Up => self.xx += 1,
Dir::Down => self.xx -= 1,
Dir::Left => self.yy -= 1,
Dir::Right => self.yy += 1,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
enum Dir {
Up,
Right,
Down,
Left,
}
impl Dir {
fn turn(&mut self, dir: i128) {
*self = match *self {
Dir::Up if dir == 0 => Dir::Left,
Dir::Up if dir == 1 => Dir::Right,
Dir::Right if dir == 0 => Dir::Up,
Dir::Right if dir == 1 => Dir::Down,
Dir::Down if dir == 0 => Dir::Right,
Dir::Down if dir == 1 => Dir::Left,
Dir::Left if dir == 0 => Dir::Down,
Dir::Left if dir == 1 => Dir::Up,
_ => panic!("Invalid turn spec! {:?}, {}", self, dir),
}
}
}
enum Color {
White,
Black,
}
fn main() -> Result<(), io::Error> {
println!("AoC 2019 | Day {}", DAY);
// This function is pure magic (see ../../aoc_import_magic/lib.rs) because it
// 1. parses command line arguments
// 2. reads the input file for the correct day
// 3. uses `parse_input` as a parsing function
// 4. returns a nice usable struct which contains everything which we need for the actual puzzle
let puzzle = import_magic(DAY, parse_input)?;
let res1 = if puzzle.skip_p1 {
None
} else {
let res1 = part1(&puzzle);
println!("Part 1 result: {}", res1);
Some(res1)
};
let res2 = part2(&puzzle, res1);
println!("Part 2 result: {}", res2);
Ok(())
}
fn parse_input(input: Vec<String>, config: &HashMap<String, String>, verbose: bool) -> InputType {
input[0]
.split(",")
.map(|xx| xx.parse::<InputTypeSingle>().unwrap())
.collect()
}
fn run_robot(program: &Vec<IntcodeNumber>, start_color: i128) -> HashMap<(i128, i128), i128> {
let mut panel: HashMap<(i128, i128), i128> = HashMap::new();
let mut robot_cpu = IntcodeProcessor::new(program);
let mut robot = Robot {
xx: 0,
yy: 0,
dir: Dir::Up,
};
panel.insert( robot.pos(), start_color);
let mut outputs = VecDeque::new();
loop {
let current_color = match panel.get(&robot.pos()) {
Some(color) => *color,
None => 0,
};
if let Some(_finished) = robot_cpu.run(current_color, &mut outputs) {
break;
};
let color = outputs.pop_front().unwrap();
let dir = outputs.pop_front().unwrap();
panel.insert( robot.pos(), color );
robot.turn(dir);
robot.step();
}
panel
}
fn part1(po: &TodaysPuzzleOptions) -> OutputType1 {
let program = po.data.as_ref().unwrap();
run_robot(program, 0).len()
}
fn part2(po: &TodaysPuzzleOptions, res1: Option<OutputType1>) -> OutputType2 {
let program = po.data.as_ref().unwrap();
let panel = run_robot(program, 1);
let mut xx_min = std::i128::MAX;
let mut xx_max = std::i128::MIN;
let mut yy_min = std::i128::MAX;
let mut yy_max = std::i128::MIN;
for (xx, yy) in panel.keys() {
xx_min = i128::min(xx_min, *xx);
xx_max = i128::max(xx_max, *xx);
yy_min = i128::min(yy_min, *yy);
yy_max = i128::max(yy_max, *yy);
}
for xx in xx_min ..= xx_max {
for yy in yy_min ..= yy_max {
let cc = match panel.get( &(xx, yy) ) {
Some(0) => '.',
Some(1) => '#',
_ => '.',
};
print!("{}", cc);
}
println!("");
}
0
}
#[cfg(test)]
mod tests {
use super::*;
use aoc_import_magic::{import_magic_with_params, PuzzleOptions};
pub(super) fn import_helper(inputname: &str) -> PuzzleOptions<InputType> {
let params = ["appname", "--input", inputname];
import_magic_with_params(DAY, parse_input, ¶ms).unwrap()
}
fn test_case_helper(inputname: &str, sol1: OutputType1, sol2: OutputType2) {
let po = import_helper(inputname);
let res1 = part1(&po);
assert_eq!(sol1, res1, "part1");
let res2 = part2(&po, Some(res1));
assert_eq!(sol2, res2, "part2");
}
#[test]
fn example_1() {
test_case_helper("example1", 8, 8)
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use super::*;
use aoc_import_magic::test_helper_import_config;
use std::{
fs::File,
io::{BufRead, BufReader},
};
use test::Bencher;
fn helper_read_file(fname: &str) -> Vec<String> {
BufReader::new(File::open(fname).unwrap())
.lines()
.map(|line| line.unwrap())
.collect()
}
#[bench]
fn bench_parsing(bb: &mut Bencher) {
let input = helper_read_file(&format!("../../_inputs/day{:02}/real1.input", DAY));
let config = test_helper_import_config(DAY, "real1");
bb.iter(|| test::black_box(parse_input(input.to_owned(), &config, false)));
}
#[bench]
fn bench_part1(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part1(&puzzle_options)));
}
#[bench]
fn bench_part2(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| test::black_box(part2(&puzzle_options, None)));
}
}