Created
December 2, 2025 04:35
-
-
Save ivelasq/ee2888b11586dfa2245d9561c6be9465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rotations = scan("input.txt", character()) | |
| counter = 0 | |
| new_starting_position = 50 | |
| max_dial = 99 | |
| pattern = "\\d+" | |
| # Part 1 --- | |
| for (rotation in rotations){ | |
| clicks = gregexpr(pattern, rotation) | |
| clicks = as.numeric(regmatches(rotation, clicks)) | |
| if(grepl("L", rotation) == TRUE) { | |
| ending_position = (new_starting_position + -1*clicks) | |
| } else { | |
| ending_position = (new_starting_position + clicks) | |
| } | |
| if(ending_position == 0) { | |
| counter = counter + 1 | |
| } | |
| new_starting_position = ending_position %% 100 | |
| } | |
| # Part 2 -- | |
| for (rotation in rotations){ | |
| clicks = gregexpr(pattern, rotation) | |
| clicks = as.numeric(regmatches(rotation, clicks)) | |
| if(clicks > max_dial) { | |
| full_rotations = floor(clicks/(max_dial + 1)) | |
| clicks = clicks - (full_rotations * (max_dial + 1)) | |
| } else { | |
| full_rotations = 0 | |
| } | |
| if(grepl("L", rotation) == TRUE) { | |
| ending_position = new_starting_position + -1*clicks | |
| } else { | |
| ending_position = new_starting_position + clicks | |
| } | |
| counter = counter + full_rotations | |
| if(ending_position == 0 | ending_position < 0 && new_starting_position != 0 | ending_position >= 100 && new_starting_position != 0) { | |
| counter = counter + 1 | |
| } | |
| new_starting_position = ending_position %% 100 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment