• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Vest / aoc-rust / 4005027531

pending completion
4005027531

Pull #19

github

GitHub
Merge 74f46dcbf into 0e0e3157b
Pull Request #19: Updated versions

2556 of 3992 branches covered (64.03%)

Branch coverage included in aggregate %.

1560 of 1560 new or added lines in 44 files covered. (100.0%)

5981 of 6247 relevant lines covered (95.74%)

624811.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.69
/adv2020/src/day12.rs
1
pub fn find_answer1(input: &str) -> i32 {
2✔
2
    let mut ship = Ship {
2✔
3
        position: (0, 0),
2✔
4
        direction: 0,
5
    };
6

7
    input.lines().for_each(|line| {
7✔
8
        let mut chars = line.chars();
5✔
9
        let direction = chars.next().unwrap();
5✔
10
        let value_str = chars.collect::<String>();
5✔
11
        let value = value_str.parse::<i32>().unwrap();
5✔
12

13
        match direction {
5!
14
            'N' => ship.position.1 += value,
1✔
15
            'S' => ship.position.1 -= value,
×
16
            'E' => ship.position.0 += value,
×
17
            'W' => ship.position.0 -= value,
×
18
            'L' => ship.direction += value,
×
19
            'R' => ship.direction -= value,
1✔
20
            'F' if ship.direction == 0 => ship.position.0 += value,
3✔
21
            'F' if ship.direction == 90 => ship.position.1 += value,
1!
22
            'F' if ship.direction == 180 => ship.position.0 -= value,
1!
23
            'F' if ship.direction == 270 => ship.position.1 -= value,
1!
24
            'F' if ship.direction == -90 => ship.position.1 -= value,
1!
25
            'F' if ship.direction == -180 => ship.position.0 -= value,
×
26
            'F' if ship.direction == -270 => ship.position.1 += value,
×
27
            _ => println!("pfffffff"),
×
28
        }
29

30
        ship.direction %= 360;
5✔
31
    });
5✔
32

33
    ship.position.0.abs() + ship.position.1.abs()
2✔
34
}
2✔
35

36
struct Ship {
37
    position: (i32, i32),
38
    direction: i32,
39
}
40

41
struct Titanic {
42
    position: (i32, i32),
43
    waypoint: (i32, i32),
44
}
45

46
pub fn find_answer2(input: &str) -> i32 {
2✔
47
    let mut titanic = Titanic {
2✔
48
        position: (0, 0),
2✔
49
        waypoint: (10, 1),
2✔
50
    };
51

52
    input.lines().for_each(|line| {
7✔
53
        let mut chars = line.chars();
5✔
54
        let direction = chars.next().unwrap();
5✔
55
        let value_str = chars.collect::<String>();
5✔
56
        let value = value_str.parse::<i32>().unwrap();
5✔
57
        let value_neg = -value;
5✔
58

59
        let ca = (value as f64).to_radians().cos() as i32;
5✔
60
        let sa = (value as f64).to_radians().sin() as i32;
5✔
61

62
        let cra = (value_neg as f64).to_radians().cos() as i32;
5✔
63
        let sra = (value_neg as f64).to_radians().sin() as i32;
5✔
64

65
        match direction {
5!
66
            'N' => titanic.waypoint.1 += value,
1✔
67
            'S' => titanic.waypoint.1 -= value,
×
68
            'E' => titanic.waypoint.0 += value,
×
69
            'W' => titanic.waypoint.0 -= value,
×
70

71
            'L' => {
72
                titanic.waypoint = (
×
73
                    ca * titanic.waypoint.0 - sa * titanic.waypoint.1,
×
74
                    sa * titanic.waypoint.0 + ca * titanic.waypoint.1,
×
75
                )
×
76
            }
77
            'R' => {
78
                titanic.waypoint = (
1✔
79
                    cra * titanic.waypoint.0 - sra * titanic.waypoint.1,
1✔
80
                    sra * titanic.waypoint.0 + cra * titanic.waypoint.1,
1✔
81
                )
1✔
82
            }
83

84
            'F' => {
3✔
85
                titanic.position.0 += value * titanic.waypoint.0;
3✔
86
                titanic.position.1 += value * titanic.waypoint.1;
3✔
87
            }
88

89
            _ => println!("pfffffff"),
×
90
        }
91

92
        println!("{} - {:?} {:?}", line, titanic.position, titanic.waypoint);
5✔
93
    });
5✔
94

95
    titanic.position.0.abs() + titanic.position.1.abs()
2✔
96
}
2✔
97
/*
98
fn parse_input<'a>(input: &'a str) -> impl Iterator<Item=Seat> + 'a {
99
    input.lines()
100
        .map(&str::trim)
101
        .map(parse_seat)
102
}
103
*/
104

105
#[cfg(test)]
106
mod tests {
107
    use super::*;
108

109
    #[test]
110
    fn test_empty_answers() {
2✔
111
        assert_eq!(
1✔
112
            find_answer1(
1!
113
                r#"F10
114
N3
115
F7
116
R90
117
F11"#
118
            ),
119
            25
120
        );
121
        assert_eq!(
1✔
122
            find_answer2(
1!
123
                r#"F10
124
N3
125
F7
126
R90
127
F11"#
128
            ),
129
            286
130
        );
131
    }
2✔
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc