Bad day 3 solution

day_5_part_2_broken
Joey Pollack 2 years ago
parent a6c3d7b75d
commit 75aaded41d

1
.gitignore vendored

@ -0,0 +1 @@
/target

@ -263,7 +263,9 @@ fn tokenize_data(game_data: &str) -> Vec<DataToken>
idx += 1; idx += 1;
} }
idx -= 1; // The loop left the idx pointing to the byte after the number, need to do this so we don't skip it (with the idx += 1 at the end of the loop) // The loop left the idx pointing to the byte after the number,
// need to do this so we don't skip it (with the idx += 1 at the end of the loop)
idx -= 1;
let num = num.parse::<u32>().expect(&format!("Failed to parse number: {} on line: {}", num, line_count)); let num = num.parse::<u32>().expect(&format!("Failed to parse number: {} on line: {}", num, line_count));
tokens.push(DataToken { ttype: TokenType::Number, value: num }); tokens.push(DataToken { ttype: TokenType::Number, value: num });

1
day_3/.gitignore vendored

@ -0,0 +1 @@
/target

7
day_3/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day_3"
version = "0.1.0"

@ -0,0 +1,21 @@
use std::{env, fs, path::Path};
fn main()
{
let out_path = format!("target/{}", &env::var("PROFILE").unwrap());
let out_path = Path::new(&out_path);
let out_path_data = out_path.join(Path::new("data"));
if !out_path_data.exists()
{
fs::create_dir(&out_path_data).expect(&format!("Could not create data directory at: {}", out_path_data.to_string_lossy()));
}
for file in fs::read_dir("data").unwrap()
{
let file = file.unwrap();
let dest = out_path.join(file.path());
fs::copy(file.path(), &dest).expect(&format!("Could not copy file {} to {}", file.path().to_string_lossy(), dest.to_string_lossy()));
}
}

@ -0,0 +1,140 @@
....401.............425.......323......791......697...............963............................................420........................
...*..................................%......#.....*....290.........................492.............656...@953.....................+830.....
..159...........823...33.717.....572.......806...896......-.....335....834......815.............791....*..............776...................
.........-.....#........*.........*..................715..........*.....*........*.....................5...*.....................688........
....=..=..573..............212.......553.632....622....*.......260....29.........692.129....727...........290.........%32.....%...*..441....
.791..998..............................*............531.......................84.......*..../.......506%......=............240..831..*......
.....................321.586........447...&.-276.21......@..........12.......&.......-..333...................619..94................982....
..........@...........#..*..............525.......*...183............*............407.......23.........%............&......846..............
....97..686.............89.....................-..494..............875....*607.............*.......239.407..................%.......529.....
.....+.........815..703............208.=555.856...........@............133........................*..........672...501........898#...*......
...............*.......*...659.......*....................183..............528.......614.......4....................................39......
...602..........804..313....*.......480..............602.............354......*877................832@.....140...$...........707.......222..
...*....999.................23.264..............516.....*.......-....*....................................*.....722......24....*........*...
436....+................./......./..........765*......37.....843....218..........852*68....290............410...........*.....539....896....
.........../...#........907............587...............&................149...............*.......*..............#780..707................
........349....603.598....................-....203.....178.......431......./...............152......571....926.750.............127..........
.............@........*176.283...................*...%......642......../......20..............................*.........@.............534...
...749..../.194..............*.......337.395*....682.429.=...#..........186.....*.........682..../......498..........%.8.............*......
....*..324..........893*...878......*........659..........65....671.189.......641...........+.859.........$.......149....648........53.+....
...776......$.....................667.................432...........*..............697............282.901...................*889........471.
............103............../........-..................-...748&.257.............&...........739*....*......491*543................997.....
.........2.........813*.....278..316.333.....296.142.+...................673.%....../.....+.........490...................790........*......
......@.*.......................*...............*....643.464............*....25....108..807..179.........931.......289..........365...522...
....367.250.927../.............754.........14...............*.+..........23...................*......&.....*..%..........323................
................939...644..............791..*.551....35...937..806...............&.............536.930...703...565......*...............*640
534$.................*.........$..448.....=......*...................707*658.....33....920..74.....................844................36....
.......572....672...875......770.+................380.............52......................*.......-....251............%........*909......677
.......*.......=........................658.889.........882.....=......................534..*..291...%....*.................289.............
.212......993..............438./..........*....@..532......*586.163....213...307...........167.....981.700..................................
....*.....*..................-..12...611...196.......*309.....................+.......*685............................403....243..+385......
59...428..14...22.747....990............*........575...............................247.....985....&..................*.......*..............
.................*........%...539.......416.712.....*2..408.......%....................999.......275......738.....715......670..............
..357........653..............*....374..........285................785.........384......*.............277*........................765.137...
...@..........*..@632......361.......=..............35.........329..............................=858..............798.................*.....
......................216*.......459..................*..........&........479.....255......302......................*.530.........269..453..
............./.573........153.....*........#.......462...%..........45......+.122*............*672....545........157.....*862.....*.........
820....%...977.+...................726...790.199.........631....308..................755..............*...460..................643....#237..
.......229.............727..425............../..........................228........#..$...............385....&...........695................
...................978.*....*....................700....*.........256..@.........625......311.170...+................642*...................
.492.....983.........-.577.743..................*....267.521........%....479.......................696.980......................*...........
....*820.#....................................236.................*......*...856.............137...........489......409........177..450.....
974.........479.$......182..456......./.............*755..617..333.849.........*...............*..........*..........=...-...........*......
...+..........*..83.#....@.=...........893.......610.........*.................727....6*..488.839...449..541../97......560.........449......
............208......168......971..........................547..........559............................=...........237...................641
.......336...................*....563..993.911.=......853................*...............391...@572.................*.......*......614..*...
........$...........579.....810....-......*....480....*......649.138..410..201..604........*..............450#.....485...124.841..*......790
..399...........928.............67..................#..295......*............*...#....333...83...#111.............................635.......
...=........./.....*..807...529..*......642#......603......704.........@....599......&.................=....................................
..............233.618..../........114........566......100..*.....423.26...............................168................555$..........#625.
.....608*372..................543..........*.*............4.....*.......+374.......436*...................680.135....................*......
.................................@...385.309..111......69....991..127........693#......985....25..856*96.+.......*860..........%..278.160...
.......351.......495=.772..............*...............*...........*...........................@............569...............905...........
........................*.$402........640.*.............366.........350...623&...250.............446*341.....=.........................292..
....#..*...595.289...347..........75.......844......307..........................*....585.....*..................766+...................*...
.148..906.....*..........232.449...*...........................................326..$...-..102.595.....406............-...........650....682
..........619...23........$...*.....138.893.......890...79....53.........118........966................+..............233............*......
.........*......../..........62..........*........*....*.............@..*....................245...........-....806.........98........318...
....534+.264..993...420.................675..77..659..263..........615.405...............=..*......%..75...614.*..................4.........
...............*..............421.........................834*287.......................813..651.902...*........134.....$...........334.....
....631....=..443........................352..........941...............317.......*286...............562.855............690....387.....*....
.27...$.430......................89....-....*90...&......%..........155.......899.......%59....*986.................775..............25.....
...*....................-.....+...*...952.........62.........478...........&...../..........694.........775.....640..$......................
....102..........+197....109.270.453.....................................641.......123..........573....*.......=............880.403+........
.........87.186..........................709.+219........224.726...882.............*...868..203*....732............928*607..*...............
..819....*..*.............*....81........*....................-../..%...+..........700.=.......................149............*....438..=...
....#.875..223.106.........646......%.....519...579.............38.....883.626@....................149..........=..........205.272./....480.
.......................965.........332.........*......221.292...........................594..945......*931..365....814*214..................
..228*....29............*....79...........+.....811.....*..*..592..........681.............*....#.427........*................170..58.......
......563...*........817..93*...+60..507...898........796.942...*..=335.......@...490....488.......-......974....817...........*....+.......
...........519..&...................*..........948.............454......184.........*.........=.......................409......947..........
...............272..........257....712............+.@.....338..............*381...824..923..756.............971*181....=....+......430......
....228.............709.......&......................685.....*..597....135......+.....*..........+......471..................452..*......923
....*..................#...........271.....................167.*.........*.......325.935..........224......*.....749&...-..........587......
..464.../199..$..........50...........*..794..........36.......733....................................@....414...........605................
...............765.%.......=.215*717........*715....................210......845.......................643........66...........*908.........
.........215........761............................544....781...390*.....576..*....869..781....517................*.....356.664.......=.....
..............316...........235/...............715*.....-....*.............*..130..........*..@..........&.........564.*...............89...
...................................&....169...........353....390....57.71.847...........716.........466..516..651......447..................
.......459..................101.379.....*......324...................#.#............943..............*...........%.........$625.../...586...
........*.....173...................198..306...*....313........51............629...*..........-958..228.178.......................967.......
......714...@.........206.......934*.........109...........*.........../......*....832...................*........934.....921..........897..
...........487............700......................*507..415....349%...179...961................437.......489.......*............../...*....
.................*440.952*................667...533.....................................359.618....*927.........................968...412...
...........-..778..................667.....@..........764..401.........279.......#.748.....*................534*.....444....................
...835..779......../.....443.........*.........................53/......=......774......=....784...204..385.....929..........+...861........
......%..........74..947*.....187&.874..........&.........................545........752.......$....$...*..................308.....*..284...
........#.................190...........445$..784...51*226...................*............*245..........435.......=...200..........88.*.....
640.....310...&...........*......296..........................................107......943..........244...........662......=..440+....302...
...*..........67........381..............856....349...........790*...911........................901...*......733........844.................
.327..............885........169.....419........*.....62..........70..@..664...........=...422%...*....617.....*....................230*....
...........916...@.............................95....@.....+.........................355.........104...........149......................569.
.............*.........................230.../..............350..............................432.....710............=.........=.............
..925..545..634.....@..297*933...........*....839...33...........34.....162.......577..248.....#...........814...524..........790......9/...
...*...............66....................421............606+..*../.......&.........*....&..129..............=......................529......
659..........253...........$504.......*......................379....&........./..860......+..............&...............%.....658*....310..
........208...*........................354..........................647......326...............911.....503.......11.......293...............
....*.....&.617..511.........885.906......................166/......................589..........*..........749....*931..........915........
.852.908........................*................../........................297.470*........#628..269...613......#................*.........
.........................716.................689...460...737*502...510.............../...........................778.......373..734.....+...
..520=.........../.....*....*226........326.....+.................+............971...362........800$..904.303.........*874..........=..548..
............554.889.720.854......808.....*..258............284*........639/...*...........................*........603.............839......
...........*........................*690.71..*.........314.....795.86.........53.................786%...134...................890*..........
663.....225...%........513...................951..........*........*...............275.995...764.....................=............30........
...*...........353....*...........%......................468.....87..225........31..=.....%.=......../.......886.....84.....................
..694......166........243........380...275..287..............261.....@.....888....*............162...278.......&..........351.534........197
........$.......209.-......261...........*.......876.....15.....*.........*........712.....870...*.......897.....46...........#.............
......450.........%..463.....%.679....119.....=.*....@...@.........871...408..995*...........*...922.+..........*...66.466..................
..............803.........................$.250...493...............&.............356.519.552........737.......153....*.............+.......
......982........*.....................540...................641.......................*......&....................................851../...
..............638..............=.306....................+938..@................*520....511.174......*..........-......*503...832.........219
.292..239...................775..*......334..../582...................173...859..................238.678......117..429.......%...708.534....
........*........240....491.....546...................+................*.............%....498.............................55........*.....74
.816.....406.......*...&..............181...........765.......776....+..636...&337...264..*............*716..$908...........*...............
...................860....................980...................$..323....................163.......851............*.......396......501.....
...-..20*197................722..........*...............................&.....408..............109.......794*155..590........./....$.......
.482...........257..........*............999./850.........482..........933........*.*982...../.....*..........................826.......908.
.......%597......@..345.329..172.....295...........349....*.....=527........174.727........645...167......@..........*......................
.....................*.....+..........$.............*....725...............*............$............+.447..723...346.759............967....
............638..............................589.....................................=..312..+....951..........=..........809*..........#...
.............@............325..........322.........&......................508......359......768........8..&.........992.....................
.783.32...................*............*.........656.............98*178.....%.....................74*.....2...410&....@............+........
...*...*..54*525...112..720...66......641.+280.......................................................467.......................580..348.....
..699.673......................................491..869............#......171..............98............473......%..............$..........
........................%.....214.......303$......#..%.......191.852..560*.......835..550...$.150.........#........291.......240............
.......579...982.........713....*...536.......................+...................*....#........$..840.......43................*............
......$.........*....*.......606......+....48...........993.......838*744.880....969........@........*......*....*726..........168...&......
...............395.579............340......*........386.=...420..............*........833..835........257...842....................810...635
......#......................%...........983...632.@........*........-...%..157...193*...........................@..137....209..............
.....819.....................278..377........................578...195..230........................$......586.985...............263.417.....
................592....../............*......../.............................922.......350........138.156*............698...78*.........$571
....................405/..573.764....257.@940...34.977$..441....304.............*.....*................................*.......93..600......
...643....675...262..........................................................993.....556...........314........278.258.322............*......
...*.........*.....&................/.....................222.........959................@..121.......*...930.......#.......170*462.....588.
384....+..19.863..............407=.283.........972...340....#....427*....*737...........558...%.....91......*...........................=...
.....812...@.........298..563....................*......*............506..........845......................634...&............624+...&......
.....................*.....=.....*.............492.563...950..827*...............+....................968......652..................49......
.730.........498..382.........399.......824.........&...................390.630........248...........*.......-......448......-..............
................*......333..........832*.....#...............$...565.....*...............*............701..564.........*.115..718.......313.
.960...*965....993......*.....*..............604..........730...*..................234&..662.....+..............=69.930...*.......967*......
.....30..............355...978...........67.........419........239..........732...............645..........................484........664...

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

@ -1,3 +1,188 @@
fn main() {
println!("Hello, world!"); // Advent of Code - Day 3
use std::{fs::File, path::Path, io::Read};
#[derive(Copy, Clone, Debug, PartialEq)]
enum TokenType
{
Empty,
Number,
Symbol,
}
#[derive(Copy, Clone, Debug)]
struct Token
{
ttype: TokenType,
value: i32,
symbol: u8
}
fn main()
{
let data = load_data("data/test_input");
let schematic = parse_schematic(&data);
let mut sum = 0;
for (row_idx, row) in schematic.iter().enumerate()
{
for (col_idx, token) in row.iter().enumerate()
{
if token.ttype == TokenType::Number
&& has_symbol_neighbor(row_idx, col_idx, &schematic)
{
sum += token.value;
}
else
{
if token.ttype == TokenType::Number
{
println!("DEBUG: Num does not have neighbor: {}", token.value);
}
}
}
}
println!("Sum: {}", sum);
}
fn has_symbol_neighbor(row: usize, col: usize, schematic: &Vec<Vec<Token>>) -> bool
{
// 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
{
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
{
return true;
}
// BOTTOM RIGHT
if col + 1 < schematic[row + 1].len()
{
if schematic[row + 1][col + 1].ttype == TokenType::Symbol
{
return true;
}
}
}
// LEFT
if col as isize - 1 > 0 && schematic[row][col - 1].ttype == TokenType::Symbol
{
return true
}
// RIGHT
if col + 1 < schematic[row].len() && schematic[row][col + 1].ttype == TokenType::Symbol
{
return true
}
return false;
}
fn parse_schematic(input: &str) -> Vec<Vec<Token>>
{
let mut parsed: Vec<Vec<Token>> = Vec::new();
parsed.push(Vec::new());
let bytes = input.as_bytes();
let mut row = 0;
let mut idx = 0;
while idx < bytes.len()
{
match bytes[idx]
{
// NEW LINE
b'\n' =>
{
parsed.push(Vec::new());
row += 1;
}
// EMPTY
b'.' => parsed[row].push(Token { ttype: TokenType::Empty, value: 0, symbol: b'.' }),
// NUMBER
b'0'..=b'9' =>
{
let mut num: String = String::new();
while bytes[idx] >= b'0' && bytes[idx] <= b'9'
{
num.push(bytes[idx] as char);
idx += 1;
}
// The loop left the idx pointing to the byte after the number,
// need to do this so we don't skip it (with the idx += 1 at the end of the loop)
idx -= 1;
let num = num.parse::<i32>().expect(&format!("Failed to parse number: {} on line: {}", num, row));
parsed[row].push(Token { ttype: TokenType::Number, value: num, symbol: bytes[idx] });
}
// SYMBOL
_ => parsed[row].push(Token { ttype: TokenType::Symbol, value: 0, symbol: bytes[idx] }),
}
idx += 1;
}
parsed
}
fn load_data(file_name: &str) -> String
{
let mut file = match File::open(Path::new(file_name))
{
Ok(file) => file,
Err(why) => panic!("Could not open file {}: {}", Path::new(file_name).display(), why),
};
let mut s = String::new();
let file_contents = match file.read_to_string(&mut s)
{
Err(why) => panic!("couldn't read {}: {}", Path::new(file_name).display(), why),
Ok(_) => s,
};
return file_contents;
} }
Loading…
Cancel
Save