Rust Day 2 - Handle crlf line endings

This commit is contained in:
Tobias Berger 2022-12-02 15:25:16 +01:00
parent 747e69fbfb
commit 8ad6df2ab6
Signed by: toby
GPG key ID: 2D05EFAB764D6A88

View file

@ -125,11 +125,12 @@ impl FromStr for GameResult {
fn parse_input_part_1() -> Vec<[Shape; 2]> { fn parse_input_part_1() -> Vec<[Shape; 2]> {
INPUT INPUT
.split('\n') .lines()
.map(|round_string| { .map(|round_string| {
let round = round_string let round = round_string
.split(' ') .split(' ')
.map(|play| { .map(|play| {
assert_eq!(play.len(), 1, "play: {play}");
Shape::from_str(play).unwrap_or_else(|_| panic!("Found invalid play {play}")) Shape::from_str(play).unwrap_or_else(|_| panic!("Found invalid play {play}"))
}) })
.take(2) .take(2)
@ -141,10 +142,12 @@ fn parse_input_part_1() -> Vec<[Shape; 2]> {
fn parse_input_part_2() -> Vec<(Shape, GameResult)> { fn parse_input_part_2() -> Vec<(Shape, GameResult)> {
INPUT INPUT
.split('\n') .lines()
.map(|round_string| { .map(|round_string| {
let round = round_string.split(' ').take(2).collect::<Vec<_>>(); let round = round_string.split(' ').take(2).collect::<Vec<_>>();
assert_eq!(round.len(), 2); assert_eq!(round.len(), 2);
assert_eq!(round[0].len(), 1);
assert_eq!(round[1].len(), 1);
let enemy_shape = Shape::from_str(round[0]) let enemy_shape = Shape::from_str(round[0])
.unwrap_or_else(|_| panic!("Found invalid play {}", round[0])); .unwrap_or_else(|_| panic!("Found invalid play {}", round[0]));
let desired_result = GameResult::from_str(round[1]) let desired_result = GameResult::from_str(round[1])