summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/six/src/main.rs
blob: 679b561c31cba31e2cbdce7afad8a456e82be7d9 (plain) (blame)
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
static INPUT: &str = include_str!("input.txt");

#[derive(Debug)]
enum LightCommand {
  TurnOff,
  TurnOn,
  Toggle
}

fn main() {
  let valueified = INPUT.split('\n').filter(|s| !s.is_empty()).map(|s| {
    let mut rsplit = s.rsplit(' ');
    let mut cd = rsplit.next().unwrap().split(',').map(str::parse::<usize>).map(Result::unwrap);
    let mut ab = rsplit.nth(1).unwrap().split(',').map(str::parse::<usize>).map(Result::unwrap);
    let ab = (ab.next().unwrap(), ab.next().unwrap());
    let cd = (cd.next().unwrap(), cd.next().unwrap());
    match &s[0..7] {
      "turn on" => (LightCommand::TurnOn, ab,cd),
      "toggle " => (LightCommand::Toggle, ab,cd),
      "turn of" => (LightCommand::TurnOff, ab,cd),
      _ => unreachable!()
    }
  });

  let mut lights = vec![false; 1_000_000];
  for (cmd, (x1, y1), (x2, y2)) in valueified.clone() {
    use LightCommand::*;
    for x in x1..=x2 {
      for y in y1..=y2 {
        match cmd {
          TurnOff => { lights[x + y * 1000] = false; },
          TurnOn => { lights[x + y * 1000] = true; },
          Toggle => { lights[x + y * 1000] = !lights[x + y * 1000]; }
        }
      }
    }
  }
  let light_count = lights.into_iter().filter(|a| *a).count();
  println!("Lit light count: {light_count}");

  let mut lights = vec![0u64; 1_000_000];
  for (cmd, (x1, y1), (x2, y2)) in valueified {
    use LightCommand::*;
    for x in x1..=x2 {
      for y in y1..=y2 {
        match cmd {
          TurnOff if lights[x + y * 1000] != 0 => { lights[x + y * 1000] -= 1; },
          TurnOn => { lights[x + y * 1000] += 1; },
          Toggle => { lights[x + y * 1000] += 2 },
          _ => ()
        }
      }
    }
  }
  let light_count = lights.into_iter().sum::<u64>();
  println!("Lit light count(Ancient Nordic Elvish): {light_count}");
}