summary refs log tree commit diff
path: root/02/src/main.rs
blob: 92ba083a14025c09eb8141ded9cc3ae78b3e5ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use advent_lib::prelude::*;


fn main() -> Result<()> {
  let mut args = std::env::args();
  if args.len() != 2 {
    eprintln!("Usage: advent input");
  }
  let _ = args.next();
  let filename = args.next().unwrap();

  let input = advent_lib::read_lines_file(&filename)?;

  {
    let mut depth = 0;
    let mut horizontal = 0;

    for line in &input {
      let mut words = line.split(' ');
      let command = words.next().unwrap();
      let parameter = words.next().unwrap().parse::<i64>().unwrap();

      match command {
        "forward" => {
          horizontal += parameter;
        },
        "down" => {
          depth += parameter;
        },
        "up" => {
          depth -= parameter;
        },
        _ => { },
      }
    }

    println!("{}", horizontal * depth);
  }

  {
    let mut aim = 0;
    let mut depth = 0;
    let mut horizontal = 0;

    for line in &input {
      let mut words = line.split(' ');
      let command = words.next().unwrap();
      let parameter = words.next().unwrap().parse::<i64>().unwrap();

      match command {
        "forward" => {
          horizontal += parameter;
          depth += aim * parameter;
        },
        "down" => {
          aim += parameter;
        },
        "up" => {
          aim -= parameter;
        },
        _ => { },
      }
    }

    println!("{}", horizontal * depth);
  }

  Ok(())
}