Musings from the mountains himwant.org

AoC Day 3

+243
+145
src/content/aoc/2025/03.md
··· 1 + --- 2 + title: "Lobby" 3 + published: 2025-12-24 4 + draft: false 5 + --- 6 + 7 + Entire question [here](https://adventofcode.com/2025/day/3). 8 + 9 + We are given several line separated strings of numbers (0-9) and a number `n`, for each string, we have two select two characters, appending which, in order, produces the largest number; and return their sum. 10 + 11 + For example, in the string "97856483" and n = 2, our answer is 98. In "811111111111119", we have 89! 12 + 13 + The only differentiating part in part 1 and part 2 is n. Part 1 has n = 2, and part 2 has it equal to 12. 14 + 15 + Let's get started then, and get done with the boilerplate first. 16 + 17 + ```rust 18 + use std::fs; 19 + 20 + fn main() { 21 + let input = fs::read_to_string("./sample.txt").unwrap(); 22 + let mut sum = 0; 23 + 24 + for line in input.lines() { 25 + let num = parse(&line, 12); 26 + sum += num; 27 + } 28 + println!("Final number: {sum}"); 29 + } 30 + ``` 31 + 32 + The real meat is in the parse function! 33 + 34 + Let us go through the logic. 35 + 36 + 1. We store three variables, sum, last<sub>index</sub>, and placeholder. 37 + 38 + 1. `sum` is the running sum for the current line. 39 + 2. `last_index`- We only search after last<sub>index</sub>. 40 + 3. `placeholder`- To store the place value we are currently searching for. For example, 10s, 100s, etc. 41 + 42 + ```rust 43 + fn parse(line: &str, pl: u32) -> u64 { 44 + let mut sum: u64 = 0; 45 + let mut last_index: isize = -1; 46 + let mut placeholder: i32 = pl as i32 - 1; 47 + 48 + <<logic>> 49 + } 50 + ``` 51 + 52 + 2. Now onto the loop!! We go through each character in the line, starting after the last<sub>index</sub> and check if the current char is equal to curr<sub>num</sub> (curr<sub>num</sub> starts from 9 and ticks down each time we can't find that number in the line). One thing to note is the range in which we check the chars, starting from `last_index + 1` to length of the line - placeholder! 53 + 54 + ```rust 55 + while placeholder >= 0 { 56 + let mut found = false; 57 + 58 + for curr_num in (0u64..=9).rev() { 59 + for (i, c) in line.chars().enumerate() { 60 + if i as isize <= last_index { 61 + continue; 62 + } 63 + 64 + if let Some(num) = c.to_digit(10) { 65 + if num as u64 == curr_num 66 + && i < line.len() - placeholder as usize 67 + { 68 + last_index = i as isize; 69 + sum += curr_num * 10_u64.pow(placeholder as u32); 70 + placeholder -= 1; 71 + found = true; 72 + break; 73 + } 74 + } 75 + } 76 + 77 + if found { 78 + break; 79 + } 80 + } 81 + 82 + if !found { 83 + break; 84 + } 85 + } 86 + 87 + sum 88 + ``` 89 + 90 + We have our final program 91 + 92 + ```rust title="main.rs" 93 + use std::fs; 94 + 95 + fn main() { 96 + let input = fs::read_to_string("./sample.txt").unwrap(); 97 + let mut sum = 0; 98 + 99 + for line in input.lines() { 100 + let num = parse(&line, 12); 101 + sum += num; 102 + } 103 + println!("Final number: {sum}"); 104 + } 105 + fn parse(line: &str, pl: u32) -> u64 { 106 + let mut sum: u64 = 0; 107 + let mut last_index: isize = -1; 108 + let mut placeholder: i32 = pl as i32 - 1; 109 + 110 + while placeholder >= 0 { 111 + let mut found = false; 112 + 113 + for curr_num in (0u64..=9).rev() { 114 + for (i, c) in line.chars().enumerate() { 115 + if i as isize <= last_index { 116 + continue; 117 + } 118 + 119 + if let Some(num) = c.to_digit(10) { 120 + if num as u64 == curr_num 121 + && i < line.len() - placeholder as usize 122 + { 123 + last_index = i as isize; 124 + sum += curr_num * 10_u64.pow(placeholder as u32); 125 + placeholder -= 1; 126 + found = true; 127 + break; 128 + } 129 + } 130 + } 131 + 132 + if found { 133 + break; 134 + } 135 + } 136 + 137 + if !found { 138 + break; 139 + } 140 + } 141 + 142 + sum 143 + } 144 + ``` 145 +
+98
src/content/aoc/2025/03.org
··· 1 + #+title: Lobby 2 + #+OPTIONS: toc:nil 3 + #+PROPERTY: header-args:rust :noweb no-export 4 + 5 + Entire question [[https://adventofcode.com/2025/day/3][here]]. 6 + 7 + We are given several line separated strings of numbers (0-9) and a number =n=, for each string, we have two select two characters, appending which, in order, produces the largest number; and return their sum. 8 + 9 + For example, in the string "97856483" and n = 2, our answer is 98. In "811111111111119", we have 89! 10 + 11 + The only differentiating part in part 1 and part 2 is n. Part 1 has n = 2, and part 2 has it equal to 12. 12 + 13 + Let's get started then, and get done with the boilerplate first. 14 + 15 + #+name: boiler 16 + #+begin_src rust 17 + use std::fs; 18 + 19 + fn main() { 20 + let input = fs::read_to_string("./sample.txt").unwrap(); 21 + let mut sum = 0; 22 + 23 + for line in input.lines() { 24 + let num = parse(&line, 12); 25 + sum += num; 26 + } 27 + println!("Final number: {sum}"); 28 + } 29 + #+end_src 30 + 31 + The real meat is in the parse function! 32 + 33 + Let us go through the logic. 34 + 35 + 1. We store three variables, sum, last_index, and placeholder. 36 + 1) =sum= is the running sum for the current line. 37 + 2) =last_index=- We only search after last_index. 38 + 3) =placeholder=- To store the place value we are currently searching for. For example, 10s, 100s, etc. 39 + 40 + #+name: parse 41 + #+begin_src rust 42 + fn parse(line: &str, pl: u32) -> u64 { 43 + let mut sum: u64 = 0; 44 + let mut last_index: isize = -1; 45 + let mut placeholder: i32 = pl as i32 - 1; 46 + 47 + <<logic>> 48 + } 49 + #+end_src 50 + 51 + 2. Now onto the loop!! We go through each character in the line, starting after the last_index and check if the current char is equal to curr_num (curr_num starts from 9 and ticks down each time we can't find that number in the line). One thing to note is the range in which we check the chars, starting from =last_index + 1= to length of the line - placeholder! 52 + 53 + #+name: logic 54 + #+begin_src rust 55 + while placeholder >= 0 { 56 + let mut found = false; 57 + 58 + for curr_num in (0u64..=9).rev() { 59 + for (i, c) in line.chars().enumerate() { 60 + if i as isize <= last_index { 61 + continue; 62 + } 63 + 64 + if let Some(num) = c.to_digit(10) { 65 + if num as u64 == curr_num 66 + && i < line.len() - placeholder as usize 67 + { 68 + last_index = i as isize; 69 + sum += curr_num * 10_u64.pow(placeholder as u32); 70 + placeholder -= 1; 71 + found = true; 72 + break; 73 + } 74 + } 75 + } 76 + 77 + if found { 78 + break; 79 + } 80 + } 81 + 82 + if !found { 83 + break; 84 + } 85 + } 86 + 87 + sum 88 + #+end_src 89 + 90 + 91 + 92 + We have our final program 93 + 94 + #+name: main 95 + #+begin_src rust :title "main.rs" :noweb yes 96 + <<boiler>> 97 + <<parse>> 98 + #+end_src