Skip to content

Commit eb62792

Browse files
committed
Year 2017 Day 2
1 parent 806e2d8 commit eb62792

File tree

8 files changed

+83
-1
lines changed

8 files changed

+83
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ pie
242242
| Day | Problem | Solution | Benchmark (μs) |
243243
| --- | --- | --- | --: |
244244
| 1 | [Inverse Captcha](https://adventofcode.com/2017/day/1) | [Source](src/year2017/day01.rs) | 1 |
245+
| 2 | [Corruption Checksum](https://adventofcode.com/2017/day/2) | [Source](src/year2017/day02.rs) | 3 |
245246

246247
## 2016
247248

benches/benchmark.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ mod year2016 {
9393

9494
mod year2017 {
9595
benchmark!(year2017, day01);
96+
benchmark!(year2017, day02);
9697
}
9798

9899
mod year2019 {

input/year2017/day02.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
1208 412 743 57 1097 53 71 1029 719 133 258 69 1104 373 367 365
2+
4011 4316 1755 4992 228 240 3333 208 247 3319 4555 717 1483 4608 1387 3542
3+
675 134 106 115 204 437 1035 1142 195 1115 569 140 1133 190 701 1016
4+
4455 2184 5109 221 3794 246 5214 4572 3571 3395 2054 5050 216 878 237 3880
5+
4185 5959 292 2293 118 5603 2167 5436 3079 167 243 256 5382 207 5258 4234
6+
94 402 126 1293 801 1604 1481 1292 1428 1051 345 1510 1417 684 133 119
7+
120 1921 115 3188 82 334 366 3467 103 863 3060 2123 3429 1974 557 3090
8+
53 446 994 71 872 898 89 982 957 789 1040 100 133 82 84 791
9+
2297 733 575 2896 1470 169 2925 1901 195 2757 1627 1216 148 3037 392 221
10+
1343 483 67 1655 57 71 1562 447 58 1561 889 1741 1338 88 1363 560
11+
2387 3991 3394 6300 2281 6976 234 204 6244 854 1564 210 195 7007 3773 3623
12+
1523 77 1236 1277 112 171 70 1198 86 1664 1767 75 315 143 1450 1610
13+
168 2683 1480 200 1666 1999 3418 2177 156 430 2959 3264 2989 136 110 3526
14+
8702 6973 203 4401 8135 7752 1704 8890 182 9315 255 229 6539 647 6431 6178
15+
2290 157 2759 3771 4112 2063 153 3538 3740 130 3474 1013 180 2164 170 189
16+
525 1263 146 954 188 232 1019 918 268 172 1196 1091 1128 234 650 420

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub mod year2016 {
221221
/// # A technical support callout from the Elves escalates rapidly.
222222
pub mod year2017 {
223223
pub mod day01;
224+
pub mod day02;
224225
}
225226

226227
/// # Rescue Santa from deep space with a solar system adventure.

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn year2016() -> Vec<Solution> {
141141
}
142142

143143
fn year2017() -> Vec<Solution> {
144-
vec![solution!(year2017, day01)]
144+
vec![solution!(year2017, day01), solution!(year2017, day02)]
145145
}
146146

147147
fn year2019() -> Vec<Solution> {

src/year2017/day02.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! # Corruption Checksum
2+
//!
3+
//! Part two is `O(n²)` complexity but at least we can reduce by a factor of two by sorting
4+
//! each line first, allowing us to only compare each number against those that are greater.
5+
//! As a minor benefit this makes part one faster too.
6+
use crate::util::parse::*;
7+
8+
type Input = Vec<Vec<u32>>;
9+
10+
pub fn parse(input: &str) -> Input {
11+
input
12+
.lines()
13+
.map(|line| {
14+
let mut values: Vec<_> = line.iter_unsigned().collect();
15+
values.sort_unstable();
16+
values
17+
})
18+
.collect()
19+
}
20+
21+
pub fn part1(input: &Input) -> u32 {
22+
input.iter().map(|values| values[values.len() - 1] - values[0]).sum()
23+
}
24+
25+
pub fn part2(input: &Input) -> u32 {
26+
input
27+
.iter()
28+
.map(|values| {
29+
for i in 0..values.len() {
30+
for j in i + 1..values.len() {
31+
if values[j] % values[i] == 0 {
32+
return values[j] / values[i];
33+
}
34+
}
35+
}
36+
unreachable!()
37+
})
38+
.sum()
39+
}

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ mod year2016 {
8686

8787
mod year2017 {
8888
mod day01_test;
89+
mod day02_test;
8990
}
9091

9192
mod year2019 {

tests/year2017/day02_test.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use aoc::year2017::day02::*;
2+
3+
const FIRST_EXAMPLE: &str = "\
4+
5 1 9 5
5+
7 5 3
6+
2 4 6 8";
7+
8+
const SECOND_EXAMPLE: &str = "\
9+
5 9 2 8
10+
9 4 7 3
11+
3 8 6 5";
12+
13+
#[test]
14+
fn part1_test() {
15+
let input = parse(FIRST_EXAMPLE);
16+
assert_eq!(part1(&input), 18);
17+
}
18+
19+
#[test]
20+
fn part2_test() {
21+
let input = parse(SECOND_EXAMPLE);
22+
assert_eq!(part2(&input), 9);
23+
}

0 commit comments

Comments
 (0)