diff --git a/.vscode/launch.json b/.vscode/launch.json index 58b0f3a..617d3c1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,10 +8,19 @@ { "type": "lldb", "request": "launch", - "name": "Debug", + "name": "Debug Day 2", "program": "${workspaceFolder}/day_2/target/debug/day_2", "args": [], "cwd": "${workspaceFolder}/day_2/target/debug" + }, + + { + "type": "lldb", + "request": "launch", + "name": "Debug Day 3", + "program": "${workspaceFolder}/day_3/target/debug/day_3", + "args": [], + "cwd": "${workspaceFolder}/day_3/target/debug" } ] } \ No newline at end of file diff --git a/day_3/bulid.rs b/day_3/build.rs similarity index 100% rename from day_3/bulid.rs rename to day_3/build.rs diff --git a/day_3/src/main.rs b/day_3/src/main.rs index f89eb05..0a2b590 100644 --- a/day_3/src/main.rs +++ b/day_3/src/main.rs @@ -14,6 +14,7 @@ enum TokenType struct Token { ttype: TokenType, + length: u32, value: i32, symbol: u8 } @@ -21,7 +22,7 @@ struct Token fn main() { - let data = load_data("data/test_input"); + let data = load_data("data/input"); let schematic = parse_schematic(&data); let mut sum = 0; @@ -52,68 +53,53 @@ fn has_symbol_neighbor(row: usize, col: usize, schematic: &Vec>) -> b // TOP ROW if row as isize - 1 >= 0 { - // TOP LEFT - if col as isize - 1 >= 0 - { - if schematic[row - 1][col - 1].ttype == TokenType::Symbol - { - return true; - } - } - - // TOP CENTER - if schematic[row - 1][col].ttype == TokenType::Symbol + if check_row(row - 1, col, schematic, schematic[row][col].length as i32) { return true; } - - // TOP RIGHT - if col + 1 < schematic[row - 1].len() - { - if schematic[row - 1][col + 1].ttype == TokenType::Symbol - { - return true; - } - } } + // BOTTOM ROW if row + 1 < schematic.len() { - // BOTTOM LEFT - if col as isize - 1 >= 0 - { - if schematic[row + 1][col - 1].ttype == TokenType::Symbol - { - return true; - } - } - - // BOTTOM CENTER - if schematic[row + 1][col].ttype == TokenType::Symbol + if check_row(row + 1, col, schematic, schematic[row][col].length as i32) { return true; } + } - // BOTTOM RIGHT - if col + 1 < schematic[row + 1].len() - { - if schematic[row + 1][col + 1].ttype == TokenType::Symbol - { - return true; - } - } + // CENTER ROW + if check_row(row, col, schematic, schematic[row][col].length as i32) + { + return true; } - // LEFT - if col as isize - 1 > 0 && schematic[row][col - 1].ttype == TokenType::Symbol + return false; +} + +fn check_row(row: usize, col: usize, schematic: &Vec>, len: i32) -> bool +{ + let mut offset: i32 = 0; + if col > 0 { - return true + offset = -1; } - // RIGHT - if col + 1 < schematic[row].len() && schematic[row][col + 1].ttype == TokenType::Symbol + while offset <= len as i32 { - return true + let col_off = (col as i32 + offset) as usize; + let l = schematic[row].len(); + if col_off >= l + { + return false; + } + + if schematic[row][col_off].ttype == TokenType::Symbol + { + return true; + } + + offset += 1; } return false; @@ -131,6 +117,7 @@ fn parse_schematic(input: &str) -> Vec> match bytes[idx] { // NEW LINE + b'\r' => (), b'\n' => { parsed.push(Vec::new()); @@ -138,14 +125,17 @@ fn parse_schematic(input: &str) -> Vec> } // EMPTY - b'.' => parsed[row].push(Token { ttype: TokenType::Empty, value: 0, symbol: b'.' }), + b'.' => parsed[row].push(Token { ttype: TokenType::Empty, length: 1, value: 0, symbol: b'.' }), // NUMBER b'0'..=b'9' => { let mut num: String = String::new(); + let mut len = 0; while bytes[idx] >= b'0' && bytes[idx] <= b'9' { + + len += 1; num.push(bytes[idx] as char); idx += 1; } @@ -155,11 +145,19 @@ fn parse_schematic(input: &str) -> Vec> idx -= 1; let num = num.parse::().expect(&format!("Failed to parse number: {} on line: {}", num, row)); - parsed[row].push(Token { ttype: TokenType::Number, value: num, symbol: bytes[idx] }); + parsed[row].push(Token { ttype: TokenType::Number, length: len, value: num, symbol: bytes[idx] }); + + // Pad out the grid with extra empties to account for the digits being + // squished into 1 grid space + for i in 1..len + { + parsed[row].push(Token { ttype: TokenType::Empty, length: 1, value: 0, symbol: b'.'}); + } + } // SYMBOL - _ => parsed[row].push(Token { ttype: TokenType::Symbol, value: 0, symbol: bytes[idx] }), + _ => parsed[row].push(Token { ttype: TokenType::Symbol, length: 1, value: 0, symbol: bytes[idx] }), } idx += 1;