|
| 1 | +advent_of_code::solution!(1); |
| 2 | + |
| 3 | +pub fn part_one(input: &str) -> Option<u64> { |
| 4 | + let lines = input.lines(); |
| 5 | + |
| 6 | + let mut hitted_zero_times = 0; |
| 7 | + let mut current_state: i32 = 50; |
| 8 | + |
| 9 | + for line in lines { |
| 10 | + let movement_direction = &line[0..1]; |
| 11 | + let movement_count: i32 = line[1..].parse().unwrap(); |
| 12 | + |
| 13 | + match movement_direction { |
| 14 | + "L" => { |
| 15 | + current_state = (current_state - movement_count) % 100; |
| 16 | + } |
| 17 | + "R" => { |
| 18 | + current_state = (movement_count + current_state) % 100; |
| 19 | + } |
| 20 | + _ => { |
| 21 | + panic!("Nein") |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + if current_state == 0 { |
| 26 | + hitted_zero_times += 1 |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + Some(hitted_zero_times) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn part_two(input: &str) -> Option<u64> { |
| 34 | + let lines = input.lines(); |
| 35 | + |
| 36 | + let mut hitted_zero_times: u64 = 0; |
| 37 | + let mut current_state: i64 = 50; |
| 38 | + |
| 39 | + for line in lines { |
| 40 | + let movement_direction = &line[0..1]; |
| 41 | + let movement_count: i64 = line[1..].parse().unwrap(); |
| 42 | + |
| 43 | + match movement_direction { |
| 44 | + "L" => { |
| 45 | + let first_hit = if current_state == 0 { |
| 46 | + 100 |
| 47 | + } else { |
| 48 | + current_state |
| 49 | + }; |
| 50 | + let zeros = if movement_count >= first_hit { |
| 51 | + 1 + (movement_count - first_hit) / 100 |
| 52 | + } else { |
| 53 | + 0 |
| 54 | + }; |
| 55 | + hitted_zero_times += zeros as u64; |
| 56 | + |
| 57 | + current_state = ((current_state - movement_count) % 100 + 100) % 100; |
| 58 | + } |
| 59 | + "R" => { |
| 60 | + let first_hit = if current_state == 0 { |
| 61 | + 100 |
| 62 | + } else { |
| 63 | + 100 - current_state |
| 64 | + }; |
| 65 | + let zeros = if movement_count >= first_hit { |
| 66 | + 1 + (movement_count - first_hit) / 100 |
| 67 | + } else { |
| 68 | + 0 |
| 69 | + }; |
| 70 | + hitted_zero_times += zeros as u64; |
| 71 | + |
| 72 | + current_state = (current_state + movement_count) % 100; |
| 73 | + } |
| 74 | + _ => { |
| 75 | + panic!("Nein") |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Some(hitted_zero_times) |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use super::*; |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_part_one() { |
| 89 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 90 | + assert_eq!(result, None); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_part_two() { |
| 95 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 96 | + assert_eq!(result, None); |
| 97 | + } |
| 98 | +} |
0 commit comments