File tree Expand file tree Collapse file tree 5 files changed +42
-2
lines changed
Filter options
Expand file tree Collapse file tree 5 files changed +42
-2
lines changed
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ fn main() {
83
83
( 2017 , 14 ) => year_2017:: day14:: run ( "wenycdww" ) ,
84
84
( 2017 , 15 ) => year_2017:: day15:: run ( 591 , 393 ) ,
85
85
( 2017 , 16 ) => year_2017:: day16:: run ( & inputs:: read_first_line ( year, day) ) ,
86
+ ( 2017 , 17 ) => year_2017:: day17:: run ( 354 ) ,
86
87
87
88
// Other
88
89
( _, _) => panic ! ( "Not implemented :(" ) ,
Original file line number Diff line number Diff line change @@ -19,8 +19,6 @@ pub fn run(input: &str) {
19
19
fn solve_part1 ( input : & str ) -> usize {
20
20
// convert the input into a grid
21
21
let grid = build_grid ( input) ;
22
- let mut used = 0 ;
23
-
24
22
return grid. len ( ) ;
25
23
}
26
24
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ fn solve_part2(input: &[u8], iter: usize) -> Vec<u8> {
40
40
return b;
41
41
}
42
42
43
+ #[ allow( non_snake_case) ]
43
44
fn transform ( moves : String , state : & [ u8 ] ) -> Vec < u8 > {
44
45
let L = state. len ( ) ;
45
46
let mut r = Vec :: new ( ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -14,3 +14,4 @@ pub mod day13;
14
14
pub mod day14;
15
15
pub mod day15;
16
16
pub mod day16;
17
+ pub mod day17;
You can’t perform that action at this time.
0 commit comments