-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
171 lines (141 loc) · 4.5 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
/*
-------Part 1-------- -------Part 2--------
Day Time Rank Score Time Rank Score
1 00:01:52 205 0 00:19:51 1241 0
BENCHMARK RESULTS
test bench::bench_parsing ... bench: 3,955 ns/iter (+/- 324)
test bench::bench_part1 ... bench: 57 ns/iter (+/- 13)
test bench::bench_part2 ... bench: 1,910 ns/iter (+/- 97)
*/
// allow bench feature when using unstable flag
// use: $ cargo +nightly bench --features unstable
#![cfg_attr(feature = "unstable", feature(test))]
use aoc_import_magic::{import_magic, PuzzleOptions};
use std::{
collections::HashMap,
convert::TryFrom,
iter::successors,
io,
};
const DAY: i32 = 1;
type InputTypeSingle = i32;
type InputType = Vec<InputTypeSingle>;
type OutputType1 = i32;
type OutputType2 = u32;
type TodaysPuzzleOptions = PuzzleOptions<InputType>;
// runtime in release mode with real input:
// real 0.002673526s
// original version:
// real 0.003070799s
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 {
// PARSE input
input
.into_iter()
.map(|line| {
line.parse::<InputTypeSingle>().unwrap_or_default()
})
.collect()
}
fn part1(po: &TodaysPuzzleOptions) -> OutputType1 {
po.data.as_ref().unwrap().into_iter().map(|mass| mass / 3 - 2).sum()
}
fn part2(po: &TodaysPuzzleOptions, _res1: Option<OutputType1>) -> OutputType2 {
po.data.as_ref().unwrap().into_iter().map(|mass| {
// Thanks to /u/bsullio
successors(u32::try_from(*mass).ok(), |mass| (mass / 3).checked_sub(2))
.skip(1) // don't include the initial mass
.sum::<u32>()
/* // original code:
let mut fuel_total = 0;
let mut fuel = mass / 3 - 2;
while fuel > 0 {
fuel_total += fuel;
fuel = fuel / 3 - 2;
}
fuel_total
*/
}).sum()
}
#[cfg(test)]
mod tests {
use super::*;
use aoc_import_magic::{import_magic_with_params, PuzzleOptions};
pub(in 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_12() {
test_case_helper("example_12", 2, 2)
}
#[test]
fn example_14() {
test_case_helper("example_14", 2, 2)
}
#[test]
fn example_1969() {
test_case_helper("example_1969", 654, 966)
}
#[test]
fn example_100756() {
test_case_helper("example_100756", 33583, 50346)
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use super::*;
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));
bb.iter(|| parse_input(input.to_owned(), &HashMap::new(), false));
}
#[bench]
fn bench_part1(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| part1(&puzzle_options));
}
#[bench]
fn bench_part2(bb: &mut Bencher) {
let puzzle_options = tests::import_helper("real1");
bb.iter(|| part2(&puzzle_options, None));
}
}