ughh what

This commit is contained in:
RingOfStorms (Joshua Bell) 2023-12-03 02:08:16 -06:00
parent 88bb36a046
commit 1a6b47949b
2 changed files with 279 additions and 0 deletions

140
aoc_puzzle_cache/day_3 Normal file
View file

@ -0,0 +1,140 @@
..975..95..................717..........................................747................................................622..............
................/47...........@....701...610.........252.660*.............*..236.....323..........108........653............................
.......69............313..............$...$...*....@.........640........81.....*................................*332..92....................
........................*...210.............767...912...367.......505.......817..................*.........478.........=....223.....568.....
..76...968............108...@.....556.....................=..........*...............412..313...575......../...........................*107.
............773/..891............*....................744.....805...14................../..../................320&.567..#...................
.962..708............&........399....146.....385.................*..........825.......................................-..655....485...-.....
...*.........+..........................*76...+..................242....997..*......185..........207.390..870...883............*.......337..
....929...299....................*.................249........-...........*.373.....*.............*.....*.*....*.............549............
................196.745.996...364..............409...-.926.....273.....580.........31...........545..547...862......810.....................
................*.......*..............900+....*..........*........586.........936...................................*.........673..........
.../..........864.683.215....*......%.........702..166..46..........&....341..........694..496.......528.%...........839...105..*...887.....
....506...617.....*........51.807....942..424...............................*..........@..+............@.84...................*......=......
...........@....594.........................*..........*675...........345..52...$..................-..............899.........804.$.........
.362*366.................487+.48&.......600..180....845........................535...18...........397.#908....432*........229......346......
.................*................835....................578......*502...............*..416.............................................391.
....610&...514...501......./........*....*71.........../.......674......925...............*.869.....966........393................/.687.*...
............*...............786...248.104........92.633.....%..............*104...&466..138..@.........*.525...+......974......339..=...260.
....372/.889..............................55.406........134..250.=228..609...........................69...............*.....................
...........................................*........976...*...........=.......357...........................191.....150.....................
...-...........595.634.........*396......592..594......*......411.856...760....../..................201.....#...709................168......
.936..236.........*....@....436.....100....../......35...../.....*........*....................917%............*.....................*..#63.
.......*..............542..........................*........689.........26....667.&...................738....831..$.............513.53......
........10.....529........*250..........162.......390...........................*..70.............................701..........*............
..355.........&........394......301@..........542..............................142..........%..............%..........*.....507.............
.....*.........................................*.....%..283.....*.........997&.......@670....402..........808......308.459.......712...152..
..291......246.@....+986.....336/.......976...468.264...*....739.187.............................................................*.....*....
........../....46...................186.$..............674............................227......-............975...............436.......492.
.491........................810.......*........................772*.........742........%....198.....*702.......*.596=....64.......=.........
...................706......=......415..................=.199......235......*...47........................72.607...................443......
.214..............*.....771.............302...........230..+..............810.....*219.606........912.....................=.................
......277......320.............937.....*.....+....632...........&374..............................*....................898.....642%.........
.........#....................*.......716....229./.........................$................934...233...............98............../.......
.....*........-659..457.........964...................659....786....619.666........277...@.....*......930*460..618...........910..265.......
..845.877.343......................*537.../120.......*.......*.....*.................&...175..631.............=.................#...........
............*....................+................@...35.949..34.381......273...880......................451........326.....................
............876..880......827..733.......645.......38......*.........402.....+..........902....459.775........837.....*.185.......813.......
........578................/............*...............622..18.........*234..............+....%..........448.......935....#.............350
..........*...468.....546................588....146=.........*..142.795..............*424................../.....................=..........
........245............@...746.......362..................970......*..............593...............487.......+.............709..593........
...990......&415..............*381......*.....................435.......214..908..................*....*.....351.....876.25....*............
......*............293*.....%...........271.............135......*......=........540............177....604...........&.....-.46..........166
...609.....................910.................@........$.......321..........141....+.45.....................750......................98....
.......#....779........944.....653...........797...........788.................*......+....164.724....475&...+.............-.471......&.....
..794...487....&..81...........*...................@............666@.....*981.937.............*.....%..........888....+...81................
.....*...........+......694.............748...104..859..............................675.434.......290.............*..352........353=..$.....
...619.977.........859..#....235....230*......*..............429........335....-.......*......631.......=.......27...................192....
.......*............=...........&.........121..680.195.959......*417.....*..938.............../..........739..#..............194.750........
....309...439.............14...............*..........*...............723.........163..748...........853.....885.........175*........487....
.............*707..-626..........464.....382................356...........868#....*...*.....13......*............450............#.....*.....
........216......................*...619......$..270........*.....892............25.494.841*.....971.............*............157.+...119...
...#40...../............343..559.425.......536........718....739...#.......284...............726.....985..136...597.621.173.......203.......
................383............/......................*...................@....348............*.....*.......*........./...*...811.......*158
...=..............$......@....................776.829.85...&........541.......*....*...&....700......341.995.....73......114....*....267....
.700.203....591.*.....924..........$217.......@.....*.......590....*.........812.579....517.......................*.............730.........
......#......*...586.........312..................806............435...259...........................573........453.282*132.243.............
..........224.................#.......468@............-...%.............*..............453...................$.....................-........
....................................................227.673....881......828..&.....632*...................589.....................284..-....
.....680.........871....17+................117.................*.............506..................#...410...................616.........519.
........*..386................78*762.........@........28..557.655.......359........-............124..#......&..........843./................
......724...*..584.....552............................*...&.................209..857...633-..............602...932......*........132&.......
..........41../.........*..........................#..427.............=370....*................................*...353...902...........529..
.....................150.......868.......370....549.........779...................284...........431.....3.....889.............189..103.=....
.......530..355..........442...*............*63............+.......852.....935.....*..............%.....*..................@....*.-.........
..........*...*...976.....*...500....@...........572...........940....*990..*....627.790.................460..904..601-..821..930....699....
..........643.824.......248........358...4.........&..301.........&........538.........*.%436....565..........*......................../....
.768..$.....................186..........*...178........*.......................*.&520..............%........782..........497.-......*......
......762..828*...972......*..............3.*.......................@....../..836...............278..............666......*...927.413.185...
....&..........40.......86.122..........&....450....................1....202............42...51....*..............*.....215.................
.353.................94*.................995.....655.....520...828....*...........612..........&.119...........486...............713........
.........................255........................*.......+.....-..463..........*........235........836....................668............
...................................................286........338........=.393....398........*....199./............631..........*631........
................64%....704...928.........470...125............*....708.15...*.............193.......*........641....*......687..............
........$..............*...................&.....*..........582......*....652.@358..643..........125.....458........168....$.......$194.....
........293.........305......417...............117....................612............*......262.............*903............................
.....$...................615*..........581.........335...139..505.419.............656.........+.......506................710.....956........
776..110.....+............................-........#..........*....*...447......*......................*...*.....938.366*.......#....376....
..........366............844....305..761......780......560........236........725.272.....450.............67.126.....................*.......
.................669$.......$.......=............*220./.....+665........495................*.@103....992....................&........600....
.......*......................812/.........................................$............940...........+...................146...975.........
....410.650.282.....*...............696.833....957...............996...*......772.287...................921..935................%.....$.....
............*....413.921...713....................%.....769.........@.390.216..#.....@..........................=...........*.......46......
.....798.136..............%....150.....236..............*......31...........*..........@.64..17......616..........901......344..............
..............................-........*.....402......873.......*......692...592....390...*.............*277................................
......539.....799.../.....272....%.@....694.#.............667...318.......=...............762.......71%.......-153......................531.
..........556*......573.......465...916...................*..........602.........................=.......145...........................*....
........................451.............../....%.....625.401.........*........428-.....493......479.427....$.........60......951......281...
..947+....36....270....*............517....521..258.*............108..812.............*................*........................*372........
...........*....*....957.823...290.....*.............415..........*...............908..730..........589......+..............................
857.........843............*........794......................677..27........................121...........670.........548.837*....&.....664.
......175..........-.....=........................422.......*..................226-..344.......*...................2....%..........474......
...................283..190..@...........458........*.162@.666.517....202...............*..@...545.%.........705.............*..............
...............*............296.466.....#.........................*..*.....852..208...999.475......256..........=....642..188.386...........
............504.888..................52....416......394...349.....53.953...*......*...........@521.........213........*.....................
......225...............272*32........*.....*..........*...%..............728.........827.513..........287*.........924..388%.........&.....
..453...*..................................71.....*..437.......501...869.......928....%...#....................%225.............734@..157...
....*..400...........810=.300@...13.15.537.....997........./......*.....*.........+.........407.758...674............604....500.............
..970..........409...............*...*...*................159...788....338..............636*...../....#.......#895....*......./.............
..................................52.....872........+...........................-........................524.........297............314.....
...........=...469.....408.............&.........641......364%.780...526.........540.865....742............*..............549..505...*......
........404.......*...-....58..%....577...............655......*.............613......*.............343..779./854............*....*...567...
...103............284......./...999....................=.....640.....284..............566.982..........$...........476...478.793..556.......
......*.....436........916.................941.............@.........&...........284.........*183.........857.953...........................
...............*.404=.....*910..703@...20......266......815.....201.....54.332...........................@......*...........95..............
.............898........................*........*..........13.....*806...*.......280.........................905........63*................
........&.........642..271.627..920.+....534...96..780......@....=...........246../......99........................................../......
....$....518.......@......*......*..323..............-............477....*4...*...........*....979......810.................366....697......
.....439........................364.........161..594.....647..................802.........234..............*17......125..82....*............
................187....667....................*..#.......*.......*................604..................946.............*....414.............
....585...218.....*...*.............-....536.396........950...200......296*935...*..........#...382..........883......182.......326....*....
.....+......-...557.349...........217............................./105..........527..387....394...*...........#..430*.....501.......393.190.
.......*474.............16....................675.....999..............362..............*..........576...................*..................
....449.....+.296*468.....................607....*...........-..#.................619....177...............31...........194..198..187.......
.........156......................330.......+..332...631...936...477................*...........658..........%..845..........*..............
....*262.........$......362......*.....429=..............................*324....296...........+.......161.....*....57.....309..............
.403............492..97.@.....695..696......$.840..&.................21.....................@.........*.......178...+....$.............364..
......178..692*.....%.....82..........=...176.......456.....517........+...238.704.......682......266.................&..222..........*.....
......*........993........*....931*...........445..............*.................=...........@...*.......448.......251................222...
.......824............467..232.......664..299*.......322*512..850..............@..........585....259........$..................&............
...458.....572.................379....*........123.....................-........298..................-................336...908....*........
....&......*....&..732...701/....*....440.....$...........739..636.....65..../.......%151.............823.455............&..........119.....
........550....391..%.............259....................#....*............834............232.179............*.........4...795.784..........
.............%...............729...............568.............936...............952........*..*..............726..........*.........-......
...627.....87....@...........-...........6.....%...106.......................458*.....281..811..282..500..............=....833....214..780..
..............320................665..............*........584.........................*............................343...............*.....
..................997....84.590.....*795.90*487.842..%431....*.......233..692....259....391.113*........*338............./..391.............
.....62.....#689.*.........*.................................444.......*...*......*.............445..861...........498/.857..*.....505*.....
......*...........436.869.....634@..663...599..901*678.100..........666....301..493.986....174.............838*492..........454........313..
.....362.....983....../................+..*.............=.............................*....*.......259.............+....464.........27......
...%........................580....68.....45.........................................21..768...267....*.+....661..879..@....&......$........
.643....545......./435.....*.........+.............................823....919.......................20..102.*..............267.690.....55...
.........*...............557....247.......653....584.5*....430....*........../...529+...........856..........305.254..212.............*.....
........945...528................/...775..+............204...-.537....$..528................276*...................%....*....863......612...
....572.......*.........614.........+................................810...............842........438.....26...&.....%.........*............
.......*....201.@......*....671..$........................../.............................*...832*.......*....214.899..........795.421+.....
994..797.........684.390....*....452../..................989..728............685....23+.856...........498.............443...................
.....................................424.......%...............@.......228.....+..............+.......................$.....................
.....2...812.517.........602*967.........262....953...*.....*....67...*...........+......1.431.....721.....402%..&.......*.....504..........
.............*....%..................963*...........96...287.255.*...548.........534....*.............*..........896..531.789.=.......855...
...........579....595...........8................................781...............................547......................................

139
src/bin/day3.rs Normal file
View file

@ -0,0 +1,139 @@
use aoc23::prelude::*;
use std::fmt::Display;
#[derive(Debug)]
enum Item {
Digit(u32),
Symbol,
None,
}
#[derive(Debug)]
struct Number {
pub value: usize,
pub row_index: usize,
pub char_start: usize,
pub char_end: usize,
}
struct Grid {
pub rows: Vec<Vec<Item>>,
}
impl Grid {
fn get_item(&self, row: usize, col: usize) -> Option<&Item> {
// println!("\t\t\tRow:{} Col:{} {:?}", row, col, self.rows.get(row));
match self.rows.get(row) {
Some(row) => row.get(col),
None => None,
}
}
fn get_adjacent_items(&self, row: usize, start: usize, end: usize) -> Vec<&Item> {
// println!("\tget adj: r{} s{} e{}", row, start, end);
let row_start = if row > 0 { row - 1 } else { 0 };
let col_start = if start > 0 { start - 1 } else { 0 };
let mut items: Vec<&Item> = vec![];
for row_i in row_start..=row + 1 {
for col_i in col_start..=end + 1 {
if row_i != row || col_i < start || col_i > end {
let item = self.get_item(row_i, col_i);
// println!("\t\trow: {} col: {} item: {:?}", row_i, col_i, item);
if let Some(item) = item {
items.push(item);
}
}
}
}
return items;
}
fn print(&self) {
for row in self.rows.iter() {
for col in row {
match col {
Item::Digit(_) => print!("d"),
Item::Symbol => print!("s"),
Item::None => print!("."),
}
}
println!();
}
}
}
fn part1(input: String) -> Result<impl Display> {
let mut grid = Grid { rows: vec![] };
let mut numbers: Vec<Number> = vec![];
for (row_index, line) in input.lines().enumerate() {
let mut row: Vec<Item> = vec![];
let mut num: Option<usize> = None;
let mut char_start = 0;
for (char_index, char) in line.trim().chars().enumerate() {
if let Some(digit) = char.to_digit(10) {
row.push(Item::Digit(digit));
num = if let Some(existing_num) = num {
Some(existing_num * 10 + digit as usize)
} else {
char_start = char_index;
Some(digit as usize)
};
} else {
if char == '.' {
// println!("Ended number seq, hit dot after {:?} {}", num, char);
row.push(Item::None);
} else {
// println!("Ended number seq, hit symbol after {:?} {}", num, char);
row.push(Item::Symbol)
}
if let Some(value) = num {
numbers.push(Number {
value,
row_index,
char_start,
char_end: char_index - 1,
});
num = None;
char_start = 0;
}
}
}
grid.rows.push(row);
}
let grid = grid;
let mut sum = 0;
for number in numbers.iter() {
let adjacent =
grid.get_adjacent_items(number.row_index, number.char_start, number.char_end);
let is_part_no = adjacent.iter().any(|item| !matches!(item, Item::None));
// println!(
// "Checking number: {:?}, is part: {}\tadjacent: {:?}",
// number.value, is_part_no, adjacent
// );
if is_part_no {
print!("{}, ", number.value);
sum += number.value;
}
}
// grid.print();
Ok(sum)
}
#[tokio::main]
async fn main() -> Result<()> {
println!("Day 3");
println!("=====");
let input = utils::aoc::get_puzzle_input(3).await?;
// ```
// let input = "467..114..
// ```
//
// ..._...... ..35..633. ......#... 617_...... .....+.58. ..592..... ......755.
// ...$.*.... .664.598.." .to_owned();
println!("part 1: {}", part1(input.clone())?);
// println!("part 2: {}", part2(input.clone())?);
Ok(())
}