···11+---
22+title: "Lobby"
33+published: 2025-12-24
44+draft: false
55+---
66+77+Entire question [here](https://adventofcode.com/2025/day/3).
88+99+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.
1010+1111+For example, in the string "97856483" and n = 2, our answer is 98. In "811111111111119", we have 89!
1212+1313+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.
1414+1515+Let's get started then, and get done with the boilerplate first.
1616+1717+```rust
1818+use std::fs;
1919+2020+fn main() {
2121+ let input = fs::read_to_string("./sample.txt").unwrap();
2222+ let mut sum = 0;
2323+2424+ for line in input.lines() {
2525+ let num = parse(&line, 12);
2626+ sum += num;
2727+ }
2828+ println!("Final number: {sum}");
2929+}
3030+```
3131+3232+The real meat is in the parse function!
3333+3434+Let us go through the logic.
3535+3636+1. We store three variables, sum, last<sub>index</sub>, and placeholder.
3737+3838+ 1. `sum` is the running sum for the current line.
3939+ 2. `last_index`- We only search after last<sub>index</sub>.
4040+ 3. `placeholder`- To store the place value we are currently searching for. For example, 10s, 100s, etc.
4141+4242+ ```rust
4343+ fn parse(line: &str, pl: u32) -> u64 {
4444+ let mut sum: u64 = 0;
4545+ let mut last_index: isize = -1;
4646+ let mut placeholder: i32 = pl as i32 - 1;
4747+4848+ <<logic>>
4949+ }
5050+ ```
5151+5252+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!
5353+5454+ ```rust
5555+ while placeholder >= 0 {
5656+ let mut found = false;
5757+5858+ for curr_num in (0u64..=9).rev() {
5959+ for (i, c) in line.chars().enumerate() {
6060+ if i as isize <= last_index {
6161+ continue;
6262+ }
6363+6464+ if let Some(num) = c.to_digit(10) {
6565+ if num as u64 == curr_num
6666+ && i < line.len() - placeholder as usize
6767+ {
6868+ last_index = i as isize;
6969+ sum += curr_num * 10_u64.pow(placeholder as u32);
7070+ placeholder -= 1;
7171+ found = true;
7272+ break;
7373+ }
7474+ }
7575+ }
7676+7777+ if found {
7878+ break;
7979+ }
8080+ }
8181+8282+ if !found {
8383+ break;
8484+ }
8585+ }
8686+8787+ sum
8888+ ```
8989+9090+We have our final program
9191+9292+```rust title="main.rs"
9393+use std::fs;
9494+9595+fn main() {
9696+ let input = fs::read_to_string("./sample.txt").unwrap();
9797+ let mut sum = 0;
9898+9999+ for line in input.lines() {
100100+ let num = parse(&line, 12);
101101+ sum += num;
102102+ }
103103+ println!("Final number: {sum}");
104104+}
105105+fn parse(line: &str, pl: u32) -> u64 {
106106+ let mut sum: u64 = 0;
107107+ let mut last_index: isize = -1;
108108+ let mut placeholder: i32 = pl as i32 - 1;
109109+110110+ while placeholder >= 0 {
111111+ let mut found = false;
112112+113113+ for curr_num in (0u64..=9).rev() {
114114+ for (i, c) in line.chars().enumerate() {
115115+ if i as isize <= last_index {
116116+ continue;
117117+ }
118118+119119+ if let Some(num) = c.to_digit(10) {
120120+ if num as u64 == curr_num
121121+ && i < line.len() - placeholder as usize
122122+ {
123123+ last_index = i as isize;
124124+ sum += curr_num * 10_u64.pow(placeholder as u32);
125125+ placeholder -= 1;
126126+ found = true;
127127+ break;
128128+ }
129129+ }
130130+ }
131131+132132+ if found {
133133+ break;
134134+ }
135135+ }
136136+137137+ if !found {
138138+ break;
139139+ }
140140+ }
141141+142142+ sum
143143+}
144144+```
145145+
+98
src/content/aoc/2025/03.org
···11+#+title: Lobby
22+#+OPTIONS: toc:nil
33+#+PROPERTY: header-args:rust :noweb no-export
44+55+Entire question [[https://adventofcode.com/2025/day/3][here]].
66+77+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.
88+99+For example, in the string "97856483" and n = 2, our answer is 98. In "811111111111119", we have 89!
1010+1111+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.
1212+1313+Let's get started then, and get done with the boilerplate first.
1414+1515+#+name: boiler
1616+#+begin_src rust
1717+ use std::fs;
1818+1919+ fn main() {
2020+ let input = fs::read_to_string("./sample.txt").unwrap();
2121+ let mut sum = 0;
2222+2323+ for line in input.lines() {
2424+ let num = parse(&line, 12);
2525+ sum += num;
2626+ }
2727+ println!("Final number: {sum}");
2828+ }
2929+#+end_src
3030+3131+The real meat is in the parse function!
3232+3333+Let us go through the logic.
3434+3535+1. We store three variables, sum, last_index, and placeholder.
3636+ 1) =sum= is the running sum for the current line.
3737+ 2) =last_index=- We only search after last_index.
3838+ 3) =placeholder=- To store the place value we are currently searching for. For example, 10s, 100s, etc.
3939+4040+ #+name: parse
4141+ #+begin_src rust
4242+ fn parse(line: &str, pl: u32) -> u64 {
4343+ let mut sum: u64 = 0;
4444+ let mut last_index: isize = -1;
4545+ let mut placeholder: i32 = pl as i32 - 1;
4646+4747+ <<logic>>
4848+ }
4949+ #+end_src
5050+5151+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!
5252+5353+ #+name: logic
5454+ #+begin_src rust
5555+ while placeholder >= 0 {
5656+ let mut found = false;
5757+5858+ for curr_num in (0u64..=9).rev() {
5959+ for (i, c) in line.chars().enumerate() {
6060+ if i as isize <= last_index {
6161+ continue;
6262+ }
6363+6464+ if let Some(num) = c.to_digit(10) {
6565+ if num as u64 == curr_num
6666+ && i < line.len() - placeholder as usize
6767+ {
6868+ last_index = i as isize;
6969+ sum += curr_num * 10_u64.pow(placeholder as u32);
7070+ placeholder -= 1;
7171+ found = true;
7272+ break;
7373+ }
7474+ }
7575+ }
7676+7777+ if found {
7878+ break;
7979+ }
8080+ }
8181+8282+ if !found {
8383+ break;
8484+ }
8585+ }
8686+8787+ sum
8888+ #+end_src
8989+9090+9191+9292+We have our final program
9393+9494+#+name: main
9595+#+begin_src rust :title "main.rs" :noweb yes
9696+ <<boiler>>
9797+ <<parse>>
9898+#+end_src