Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit6fd5566

Browse files
author
Lukasz
committed
AoC 2021
1 parent8a38063 commit6fd5566

File tree

1 file changed

+46
-12
lines changed
  • advent-of-code/2021/day02/src

1 file changed

+46
-12
lines changed

‎advent-of-code/2021/day02/src/main.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Usage: cd 2021/day02 && cargo run
22

3-
use itertools::Itertools;
3+
use std::io::{self,Error,ErrorKind,Read,Write};
4+
use std::str::FromStr;
45

56

6-
// Command
7+
// Command
78
enumCommandType{
89
Forward(u8),
910
Down(u8),
@@ -28,25 +29,58 @@ impl Submarine {
2829

2930
}
3031

31-
fnprocess(&self,cmds:&[CommandType]){
32-
32+
fnprocess(&mutself,cmds:&[CommandType]){
33+
for cmdin cmds{
34+
match cmd{
35+
CommandType::Forward(v) =>{
36+
self.h_pos +=*vasu32;
37+
}
38+
CommandType::Down(v) =>{
39+
self.depth +=*vasu32;
40+
}
41+
CommandType::Up(v) =>{
42+
self.depth -=*vasu32;
43+
}
44+
}
45+
}
3346
}
47+
}
3448

35-
49+
use std::num::ParseIntError;
50+
51+
implFromStrforCommandType{
52+
53+
fnfrom_str(line:&str) ->Result<Self,ParseIntError>{
54+
letmut words = line.split_ascii_whitespace();
55+
let first = words
56+
.next()
57+
.ok_or_else(||Err("Invalid input"));
58+
let second = words
59+
.next()
60+
.ok_or_else(||Err("Invalid input"))?
61+
.parse::<u8>()?;
62+
match first{
63+
"forward" =>Ok(CommandType::Forward(second)),
64+
"down" =>Ok(CommandType::Down(second)),
65+
"up" =>Ok(CommandType::Up(second)),
66+
_ =>Err("Invalid input"),
67+
}
68+
}
3669
}
3770

38-
fnparse_input(input:&str) ->Vec<i32>{
39-
input.lines()
40-
.filter_map(|n| n.parse::<i32>().ok())
41-
.collect()
71+
fnparse_input(input:&str) ->Vec<CommandType>{
72+
input
73+
.lines()
74+
.map(|line|CommandType::from_str(line))
75+
.collect::<std::result::Result<Vec<_>,_>>()?;
4276
}
4377

44-
fnpart_1(input:&[i32]){
78+
fnpart_1(input:&[CommandType]){
4579

46-
println!("part_1={}", sum);
80+
//println!("part_1={}", sum);
4781
}
4882

49-
fnpart_2(input:Vec<i32>){
83+
fnpart_2(input:&[CommandType]){
5084

5185
}
5286

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp