Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

rename Turn to Direction for more consistent naming

Rain dc27fdd1 5edbf062

+30 -30
+30 -30
2025/days/1/src/main.rs
··· 1 1 #[derive(Debug, Clone)] 2 - enum Turn { 2 + enum Direction { 3 3 Left, 4 4 Right, 5 5 } 6 6 7 - fn parse(rotation: &str) -> (Turn, usize) { 7 + fn parse(rotation: &str) -> (Direction, usize) { 8 8 let (turn_str, num_str) = rotation.split_at(1); 9 9 10 10 let turn = match turn_str { 11 - "L" => Turn::Left, 12 - "R" => Turn::Right, 11 + "L" => Direction::Left, 12 + "R" => Direction::Right, 13 13 _ => panic!("unexpected turn value"), 14 14 }; 15 15 ··· 18 18 (turn, num_clicks) 19 19 } 20 20 21 - fn turn(starting_point: u8, direction: Turn, mut num_clicks: usize) -> (u8, usize) { 21 + fn turn(starting_point: u8, direction: Direction, mut num_clicks: usize) -> (u8, usize) { 22 22 let full_rotations = (num_clicks / 100) as usize; 23 23 num_clicks = num_clicks - full_rotations * 100; 24 24 ··· 26 26 let mut zeros_hit = full_rotations; 27 27 28 28 let endpoint = match direction { 29 - Turn::Left => { 29 + Direction::Left => { 30 30 if (starting_point as usize) < num_clicks { 31 31 // we're already on zero and we won't hit zero again, so don't count it 32 32 if starting_point != 0 { ··· 43 43 starting_point - num_clicks as u8 44 44 } 45 45 } 46 - Turn::Right => { 46 + Direction::Right => { 47 47 if starting_point as usize + num_clicks > 99 { 48 48 zeros_hit += 1; 49 49 (starting_point + num_clicks as u8) - 100 ··· 56 56 (endpoint, zeros_hit) 57 57 } 58 58 59 - fn part1(turns: Vec<(Turn, usize)>) -> usize { 59 + fn part1(turns: Vec<(Direction, usize)>) -> usize { 60 60 let mut counter = 0; 61 61 let mut starting_point = 50; 62 62 ··· 72 72 counter 73 73 } 74 74 75 - fn part2(turns: Vec<(Turn, usize)>) -> usize { 75 + fn part2(turns: Vec<(Direction, usize)>) -> usize { 76 76 let mut counter = 0; 77 77 let mut starting_point = 50; 78 78 ··· 87 87 88 88 fn main() { 89 89 let file_contents = std::fs::read_to_string("input.txt").expect("unable to read file"); 90 - let mut turns: Vec<(Turn, usize)> = Vec::new(); 90 + let mut turns: Vec<(Direction, usize)> = Vec::new(); 91 91 for line in file_contents.lines() { 92 92 turns.push(parse(line)); 93 93 } ··· 100 100 #[test] 101 101 fn test_part1() { 102 102 let turns = vec![ 103 - (super::Turn::Left, 68), 104 - (super::Turn::Left, 30), 105 - (super::Turn::Right, 48), 106 - (super::Turn::Left, 5), 107 - (super::Turn::Right, 60), 108 - (super::Turn::Left, 55), 109 - (super::Turn::Left, 1), 110 - (super::Turn::Left, 99), 111 - (super::Turn::Right, 14), 112 - (super::Turn::Left, 82), 103 + (super::Direction::Left, 68), 104 + (super::Direction::Left, 30), 105 + (super::Direction::Right, 48), 106 + (super::Direction::Left, 5), 107 + (super::Direction::Right, 60), 108 + (super::Direction::Left, 55), 109 + (super::Direction::Left, 1), 110 + (super::Direction::Left, 99), 111 + (super::Direction::Right, 14), 112 + (super::Direction::Left, 82), 113 113 ]; 114 114 115 115 assert_eq!(super::part1(turns), 3); ··· 118 118 #[test] 119 119 fn test_part2() { 120 120 let turns = vec![ 121 - (super::Turn::Left, 68), 122 - (super::Turn::Left, 30), 123 - (super::Turn::Right, 48), 124 - (super::Turn::Left, 5), 125 - (super::Turn::Right, 60), 126 - (super::Turn::Left, 55), 127 - (super::Turn::Left, 1), 128 - (super::Turn::Left, 99), 129 - (super::Turn::Right, 14), 130 - (super::Turn::Left, 82), 121 + (super::Direction::Left, 68), 122 + (super::Direction::Left, 30), 123 + (super::Direction::Right, 48), 124 + (super::Direction::Left, 5), 125 + (super::Direction::Right, 60), 126 + (super::Direction::Left, 55), 127 + (super::Direction::Left, 1), 128 + (super::Direction::Left, 99), 129 + (super::Direction::Right, 14), 130 + (super::Direction::Left, 82), 131 131 ]; 132 132 133 133 assert_eq!(super::part2(turns), 6);