File tree Expand file tree Collapse file tree 29 files changed +136
-139
lines changed
Expand file tree Collapse file tree 29 files changed +136
-139
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ struct Puzzle {
77}
88
99impl Puzzle {
10- fn new ( ) -> Puzzle {
11- Puzzle {
10+ const fn new ( ) -> Self {
11+ Self {
1212 data : String :: new ( ) ,
1313 }
1414 }
Original file line number Diff line number Diff line change @@ -11,8 +11,8 @@ struct Puzzle {
1111}
1212
1313impl Puzzle {
14- fn new ( ) -> Puzzle {
15- Puzzle {
14+ const fn new ( ) -> Self {
15+ Self {
1616 pos : vec ! [ ] ,
1717 vel : vec ! [ ] ,
1818 message : String :: new ( ) ,
@@ -113,7 +113,7 @@ impl Puzzle {
113113 }
114114
115115 /// Solve part two.
116- fn part2 ( & self ) -> u32 {
116+ const fn part2 ( & self ) -> u32 {
117117 self . seconds
118118 }
119119}
Original file line number Diff line number Diff line change @@ -5,8 +5,8 @@ struct Puzzle {
55}
66
77impl Puzzle {
8- fn new ( ) -> Puzzle {
9- Puzzle { serial_number : 0 }
8+ const fn new ( ) -> Self {
9+ Self { serial_number : 0 }
1010 }
1111
1212 /// Get the puzzle input.
Original file line number Diff line number Diff line change @@ -8,8 +8,8 @@ struct Puzzle {
88}
99
1010impl Puzzle {
11- fn new ( ) -> Puzzle {
12- Puzzle {
11+ fn new ( ) -> Self {
12+ Self {
1313 state : String :: new ( ) ,
1414 rules : HashSet :: new ( ) ,
1515 }
@@ -23,14 +23,14 @@ impl Puzzle {
2323 if let Some ( state) = line. strip_prefix ( "initial state: " ) {
2424 self . state = state. to_string ( ) ;
2525 } else if let Some ( from) = line. strip_suffix ( " => #" ) {
26- self . rules . insert ( Vec :: from_iter ( from. chars ( ) ) ) ;
26+ self . rules . insert ( from. chars ( ) . collect :: < Vec < _ > > ( ) ) ;
2727 }
2828 }
2929 }
3030
3131 /// Solve part one.
3232 fn part1 ( & self ) -> i32 {
33- let mut state = Vec :: from_iter ( self . state . chars ( ) ) ;
33+ let mut state = self . state . chars ( ) . collect :: < Vec < _ > > ( ) ;
3434
3535 let mut pots = 0 ;
3636
@@ -76,7 +76,7 @@ impl Puzzle {
7676
7777 /// Solve part two.
7878 fn part2 ( & self ) -> i64 {
79- let mut state = Vec :: from_iter ( self . state . chars ( ) ) ;
79+ let mut state = self . state . chars ( ) . collect :: < Vec < _ > > ( ) ;
8080
8181 let mut score = 0 ;
8282 let mut pots = 0 ;
Original file line number Diff line number Diff line change @@ -32,8 +32,8 @@ struct Puzzle {
3232}
3333
3434impl Puzzle {
35- fn new ( ) -> Puzzle {
36- Puzzle {
35+ fn new ( ) -> Self {
36+ Self {
3737 verbose : false ,
3838 grid : Grid :: new ( ) ,
3939 carts : vec ! [ ] ,
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ fn appear(recipes: &str) -> usize {
4848
4949 let score: Vec < u8 > = recipes
5050 . chars ( )
51- . map ( |c| c. to_digit ( 10 ) . unwrap ( ) as u8 )
51+ . map ( |c| u8 :: try_from ( c. to_digit ( 10 ) . unwrap ( ) ) . unwrap ( ) )
5252 . collect ( ) ;
5353
5454 let score = score. as_slice ( ) ;
@@ -85,8 +85,8 @@ struct Puzzle {
8585}
8686
8787impl Puzzle {
88- fn new ( ) -> Puzzle {
89- Puzzle {
88+ const fn new ( ) -> Self {
89+ Self {
9090 recipes : String :: new ( ) ,
9191 }
9292 }
@@ -95,7 +95,7 @@ impl Puzzle {
9595 fn configure ( & mut self , path : & str ) {
9696 let data = std:: fs:: read_to_string ( path) . unwrap ( ) ;
9797
98- self . recipes = data. trim ( ) . to_owned ( ) ;
98+ data. trim ( ) . clone_into ( & mut self . recipes ) ;
9999 }
100100
101101 /// Solve part one.
Original file line number Diff line number Diff line change @@ -100,9 +100,7 @@ fn next_pos(
100100 if target_adj. contains ( & ( x, y) ) {
101101 min_path = min_path. min ( steps) ;
102102 pos. push ( e) ;
103- } else if !visited. contains ( & ( x, y) ) {
104- visited. insert ( ( x, y) ) ;
105-
103+ } else if visited. insert ( ( x, y) ) {
106104 q. extend (
107105 adjacent ( x, y)
108106 . iter ( )
@@ -138,8 +136,8 @@ fn has_elves_and_goblins(units: &[Unit]) -> bool {
138136}
139137
140138impl Puzzle {
141- fn new ( ) -> Puzzle {
142- Puzzle {
139+ fn new ( ) -> Self {
140+ Self {
143141 wall : HashSet :: new ( ) ,
144142 units : vec ! [ ] ,
145143 }
Original file line number Diff line number Diff line change @@ -40,8 +40,8 @@ struct Puzzle {
4040}
4141
4242impl Puzzle {
43- fn new ( ) -> Puzzle {
44- Puzzle {
43+ const fn new ( ) -> Self {
44+ Self {
4545 result1 : 0 ,
4646 result2 : 0 ,
4747 }
@@ -142,12 +142,12 @@ impl Puzzle {
142142 }
143143
144144 /// Solve part one.
145- fn part1 ( & self ) -> u32 {
145+ const fn part1 ( & self ) -> u32 {
146146 self . result1
147147 }
148148
149149 /// Solve part two.
150- fn part2 ( & self ) -> u32 {
150+ const fn part2 ( & self ) -> u32 {
151151 self . result2
152152 }
153153}
Original file line number Diff line number Diff line change @@ -21,17 +21,17 @@ struct Point {
2121}
2222
2323impl Point {
24- fn go ( & self , dir : & Dir ) -> Self {
24+ const fn go ( & self , dir : & Dir ) -> Self {
2525 match dir {
26- Dir :: Right => Point {
26+ Dir :: Right => Self {
2727 x : self . x + 1 ,
2828 y : self . y ,
2929 } ,
30- Dir :: Left => Point {
30+ Dir :: Left => Self {
3131 x : self . x - 1 ,
3232 y : self . y ,
3333 } ,
34- Dir :: Down => Point {
34+ Dir :: Down => Self {
3535 x : self . x ,
3636 y : self . y + 1 ,
3737 } ,
@@ -66,8 +66,8 @@ struct Puzzle {
6666}
6767
6868impl Puzzle {
69- fn new ( ) -> Puzzle {
70- Puzzle {
69+ fn new ( ) -> Self {
70+ Self {
7171 grid : HashMap :: new ( ) ,
7272 ymin : 0 ,
7373 ymax : 0 ,
@@ -108,15 +108,15 @@ impl Puzzle {
108108
109109 for y in 0 ..=self . ymax {
110110 for x in xmin..=xmax {
111- let c = match self . grid . get ( & Point { x , y } ) {
112- Some ( t ) => match * t {
113- CLAY => " \x1b [38;5;166m# \x1b [0m" ,
114- SETTLE => "?" ,
115- FLOW => "\x1b [94m| \x1b [0m" ,
116- _ => "\x1b [96m~ \x1b [0m " ,
117- } ,
118- _ => "\x1b [38;5;231m. \x1b [0m" ,
119- } ;
111+ let c =
112+ self . grid
113+ . get ( & Point { x , y } )
114+ . map_or ( " \x1b [38;5;231m. \x1b [0m" , |t| match * t {
115+ CLAY => "\x1b [38;5;166m# \x1b [0m" ,
116+ SETTLE => "? " ,
117+ FLOW => " \x1b [94m| \x1b [0m" ,
118+ _ => "\x1b [96m~ \x1b [0m" ,
119+ } ) ;
120120 print ! ( "{c}" ) ;
121121 }
122122 println ! ( ) ;
Original file line number Diff line number Diff line change @@ -88,8 +88,8 @@ struct Puzzle {
8888}
8989
9090impl Puzzle {
91- fn new ( ) -> Puzzle {
92- Puzzle {
91+ fn new ( ) -> Self {
92+ Self {
9393 area : grid ! [ ] ,
9494 n : 0 ,
9595 }
You can’t perform that action at this time.
0 commit comments