Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68716b3

Browse filesBrowse files
2017, day 17
1 parent 34f6e6a commit 68716b3
Copy full SHA for 68716b3

File tree

Expand file treeCollapse file tree

5 files changed

+42
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+42
-2
lines changed

‎src/main.rs

Copy file name to clipboardExpand all lines: src/main.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn main() {
8383
(2017, 14) => year_2017::day14::run("wenycdww"),
8484
(2017, 15) => year_2017::day15::run(591, 393),
8585
(2017, 16) => year_2017::day16::run(&inputs::read_first_line(year, day)),
86+
(2017, 17) => year_2017::day17::run(354),
8687

8788
// Other
8889
(_, _) => panic!("Not implemented :("),

‎src/year_2017/day14.rs

Copy file name to clipboardExpand all lines: src/year_2017/day14.rs
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ pub fn run(input: &str) {
1919
fn solve_part1(input: &str) -> usize {
2020
// convert the input into a grid
2121
let grid = build_grid(input);
22-
let mut used = 0;
23-
2422
return grid.len();
2523
}
2624

‎src/year_2017/day16.rs

Copy file name to clipboardExpand all lines: src/year_2017/day16.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn solve_part2(input: &[u8], iter: usize) -> Vec<u8> {
4040
return b;
4141
}
4242

43+
#[allow(non_snake_case)]
4344
fn transform(moves: String, state: &[u8]) -> Vec<u8> {
4445
let L = state.len();
4546
let mut r = Vec::new();

‎src/year_2017/day17.rs

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub fn run(step: usize) {
2+
let part1 = solve_part1(step);
3+
println!("part 1: {}", part1);
4+
assert_eq!(part1, 2000);
5+
6+
let part2 = solve_part2(step);
7+
println!("part 2: {}", part2);
8+
assert_eq!(part2, 10242889);
9+
}
10+
11+
fn solve_part1(step: usize) -> usize {
12+
// I wasn't able to find a way to re-use code between part1 and part2.
13+
// For part1, we use a buffer.
14+
let mut buf = Vec::new();
15+
buf.push(0);
16+
let mut pos = 0;
17+
for i in 1..2018 {
18+
pos = (pos + step) % i;
19+
buf.insert(pos, i);
20+
pos += 1;
21+
}
22+
pos = pos % 2017;
23+
return buf[pos];
24+
}
25+
26+
fn solve_part2(step: usize) -> usize {
27+
// 0 is at offset 0. It makes the search easy. We no longer need a buffer, just keep track
28+
// of what was the last element to get added right after 0.
29+
let mut pos = 0;
30+
let mut last = None;
31+
for i in 1..(50_000_000 + 1) {
32+
pos = (pos + step) % i;
33+
if pos == 0 {
34+
last = Some(i);
35+
}
36+
pos += 1;
37+
}
38+
return last.unwrap();
39+
}

‎src/year_2017/mod.rs

Copy file name to clipboardExpand all lines: src/year_2017/mod.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pub mod day13;
1414
pub mod day14;
1515
pub mod day15;
1616
pub mod day16;
17+
pub mod day17;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.