Guest User

Untitled

a guest
Dec 18th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.78 KB | None | 0 0
  1. advent_of_code::solution!(18);
  2. use advent_of_code::{Direction, MapIndex, NewIndex};
  3. use itertools::Itertools;
  4.  
  5. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
  6. struct Instruction {
  7.     dir: Direction,
  8.     step: usize,
  9. }
  10.  
  11. fn parse_part_1(s: &str) -> Instruction {
  12.     let (d, s, c) = s.split(" ").collect_tuple().unwrap();
  13.     let dir = match d {
  14.         "U" => Direction::North,
  15.         "D" => Direction::South,
  16.         "R" => Direction::East,
  17.         "L" => Direction::West,
  18.         _ => panic!("Wrong direction"),
  19.     };
  20.     Instruction {
  21.         dir: dir,
  22.         step: s.parse().unwrap(),
  23.     }
  24. }
  25.  
  26. fn parse_part_2(s: &str) -> Instruction {
  27.     let (d, s, c) = s.split(" ").collect_tuple().unwrap();
  28.     let hex: usize = usize::from_str_radix(c.strip_prefix("(#").unwrap().strip_suffix(")").unwrap(), 16).unwrap();
  29.     let dir = match hex % 16 {
  30.         3 => Direction::North,
  31.         1 => Direction::South,
  32.         0 => Direction::East,
  33.         2 => Direction::West,
  34.         _ => panic!("Wrong direction"),
  35.     };
  36.     Instruction {
  37.         dir: dir,
  38.         step: hex / 16,
  39.     }
  40. }
  41.  
  42.  
  43. struct Case {
  44.     instructions: Vec<Instruction>,
  45. }
  46.  
  47. impl Case {
  48.     fn solve(self: &Self) -> usize {
  49.         let mut curr = (0 as i64, 0 as i64);
  50.         let mut points = vec![curr];
  51.         let mut length = 0;
  52.         for inst in &self.instructions {
  53.             let diff = inst.dir.diff_len(inst.step as i32);
  54.             length += inst.step;
  55.             curr = (curr.0 + diff.0 as i64, curr.1 + diff.1 as i64);
  56.             points.push(curr);
  57.         }
  58.         points.push((0, 0));
  59.         let mut result: i64 = 0;
  60.         for i in 0..points.len() - 1 {
  61.             let a = points[i];
  62.             let b = points[i + 1];
  63.             result += a.0 * b.1 - a.1 * b.0;
  64.         }
  65.         println!("Length: {length}");
  66.         return (result.abs() / 2) as usize + length / 2 + 1;
  67.     }
  68. }
  69.  
  70. pub fn part_one(input: &str) -> Option<usize> {
  71.     let case = Case {
  72.         instructions: input.lines().map(parse_part_1).collect_vec()
  73.     };
  74.     Some(case.solve())
  75. }
  76.  
  77. pub fn part_two(input: &str) -> Option<usize> {
  78.     let case = Case {
  79.         instructions: input.lines().map(parse_part_2).collect_vec()
  80.     };
  81.     Some(case.solve())
  82. }
  83.  
  84. #[cfg(test)]
  85. mod tests {
  86.     use super::*;
  87.  
  88.     #[test]
  89.     fn test_parse_part_2() {
  90.         assert_eq!(Instruction {dir: Direction::East, step: 461937}, parse_part_2("R 6 (#70c710)"));
  91.     }
  92.  
  93.     #[test]
  94.     fn test_part_one() {
  95.         let result = part_one(&advent_of_code::template::read_file("examples", DAY));
  96.         assert_eq!(result, Some(62));
  97.     }
  98.  
  99.     #[test]
  100.     fn test_part_two() {
  101.         let result = part_two(&advent_of_code::template::read_file("examples", DAY));
  102.         assert_eq!(result, None);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment