Day 3
This commit is contained in:
parent
a084977819
commit
feeb43943a
|
@ -0,0 +1,140 @@
|
|||
........954......104.......52......70..............206.806........708..........................217...............................440........
|
||||
.......@...................*.............................*.664..............677................@....459.........687.........................
|
||||
..................378.....398........548..495..........983....*................*..282.................*...........$.248.....409.......165...
|
||||
......261........................704.&.......*................943...615.504.....6....*773..........687..../973.2*.....=.311*....*..../......
|
||||
187..-....&...............828....*......*268..488....534.........................................................244.........722.286........
|
||||
........663.254.723.@.......*.842....696............../...163.512&.............797.......................749................................
|
||||
230.........*.....*.442...563...............................*.....................*716...................*...395............352..594&.......
|
||||
...........468.522............................+33........660....&......................................891......-...#.......................
|
||||
......929...........*261..680............-...........@.........29.312.............972.......................704.....545.56*274......537.....
|
||||
........*.......................+.#158..311.........987............*................&....923................*...837.............561...%.....
|
||||
......35....75.....715........382...........855............/440..890.224.....613............*......622./....810....*632.........%...........
|
||||
............*.....%.......286........534.................$............*...........277.....851..14...*...645.............916../.......682....
|
||||
.....189..641.%.........%....*..........$...694.214*......137.......16........26.....+..%........&.675.........511........*..848.184*.....55
|
||||
..............760......323....167..674.......*.......604@..............-......*.........24.................$......*.....666.................
|
||||
....99.....................................*.360.......................805..509../.........284.....&981..827....714.................410.....
|
||||
...*.................+.601&..10&........202.......753...........................208..925...*..........................652...752.....#.......
|
||||
...411......656...448..............................&......888.749..677...............=...970.................128....-....*..*....@..........
|
||||
.............*.......................236.60...181..........*.....*....-.713.170................@.......839.....*....116.....79.431.=........
|
||||
.689...542..46................483.....*....*...*.....#..289..348.483........*........=...731....376...@.....656.....................924.&...
|
||||
.........*.............340......&..@..92..314.3...572.........$.......408.177........728....*....................325...929..............852.
|
||||
........824....323....*...........157....................126............*.....640...........404...................*......@........430.......
|
||||
.649..............*.723.......32............375......480*....537..637.467.....*....=598.............959.......263..136......718...*....#....
|
||||
..........931..316...........*...........78*................../..*.........611..........979.@...198....*751...*.........123./......579..733.
|
||||
.81&.....*..............772.884......809..................435.....940............162*........96...*............145....../......414..........
|
||||
.......#.511......204....................706*.....442........*........213.............116......................................./...........
|
||||
625...60............*..............@.........870.#...........17.......*.......725.....*...........-..696.......114......../.......=.........
|
||||
............734..................482.................35*974.....%....227........@.....60........382...*.......-..........664....584...472...
|
||||
288.....697*............................*614....975.............999.........599..........332..........978..60......./...................*...
|
||||
...*............701..961..180........998..................211..............$...............*...310..................953.....$..153...861....
|
||||
356........%344..@....*....*........................267....+...421%........................697....*...........531.........776.....*......193
|
||||
........#...........757.925................360.........................261......71.....950.........490....-../........+..........989........
|
||||
634...385....*70............................*.......%........311...457*.......%........*...640..........687.......=...946...................
|
||||
...*......514....973....100...46..570..21....155.....692.575..../..............977.233........$.................931...........*..240+.......
|
||||
332.............*........*........#....-................./.........944..............*............230.....+...........889.....462.......615..
|
||||
.....&..531....77.435.427..437......*....=417.......................%..194.......385.........88-..#...174...................................
|
||||
....951.............-............716.261.......................234....*..................&........................................439.......
|
||||
................*.........563+............*.............805.....%....681..............393...909..247.$415.............=..+..................
|
||||
..954.644...-...35...%425........890....857.......%.521..+..438...............437..............@...@.........563.....133..696...............
|
||||
...$........887.............&466.*..............547.%.........*...804....118...=..15.......302...............*......................80..%549
|
||||
........610......877...990.......368.......................553....*.....*........-............*.....232...282..-...............264....*.....
|
||||
410.....@.......%....*....*793.......946.....67*863.895.........511.....628..............945..764....*.........455...............$.....880..
|
||||
...*.......331....540.540.............*................*192.............................*.........103.......................................
|
||||
.311......*..................954......602.766.403...............#.....307..239..417......617..........959...416.........&521................
|
||||
.....-..51.....732.......-...=.....$.........*......695........131....*...*.....................494@..*......*...............+...983........
|
||||
...284.....337....+......541.......345..........306.........%........111..677........679.............738..#...537............523...%..338...
|
||||
.......770...............................415.......*.........32...............@..................420.....32............................-....
|
||||
........+...........781.....................&.......459.............649*213....36..493.621..........*.......7..458*103.......921...491......
|
||||
............921.....*.........373$..............*..................................*...*.......114.473.......*.......................*......
|
||||
.....*........=.473....+..38.........120.......294................+519.............899........*...........263...606/.545..............828...
|
||||
..993.............$..76....*.........%.....302.........886+.513............................193...*....................@...........592.......
|
||||
.......783..............872....533......%..+....................../.............................425...387........%.......958........*..6....
|
||||
........*..440*155..588...........*...894.....*56...339............78.@493.............261.101..........*......340...*.....*....+.....*.....
|
||||
.......423.........*........771..902.......167.........*...665.988.........606.....30.....*..........469..=.........745.610..542.......679..
|
||||
...................611.....*........................553....*......*252........=....*...$.....665...........102..............................
|
||||
....333...103.409.......583.............................688..92.........-........415....615.....*519............805.493.......297...........
|
||||
751*.........*...............926............899.............*........963..420................&...............*.....*.....286.......=495.....
|
||||
..................244.......*.........784.....%..737.......942.962.........-...3..........909.............160.358...........*............832
|
||||
........600.830....*..779....988......%.....*......*....................%..........$...............262..................335.433..81.....*...
|
||||
.151.......*......................$......271.302.34.....150............575.43..425.109...169........@.....@......63..25..+.............849..
|
||||
........*........................923..............................$560.....*............*.....167.......639............&....................
|
||||
573..551.855............201................*482.586.........677.........802..619........83....*.....22........#......................560*...
|
||||
...*..............747..*.......699.....+........*.........*....&..256.........................998..*.....823...993.........&.............662
|
||||
.164.634#..........*..556.........*...286....437.......483.815.....*.....*................654.....67..................#.....633.8*..........
|
||||
...........998..855..........67..398.............107...............610.620...........721.-.............946.900.439.283............959.199...
|
||||
.211*916..*..........&...524*.............436...*.............518...............893.*................*.....*...*...........=..........-.....
|
||||
..........235..%...473......................*....54..............*.....-......../....551............833..994.81......*640...70....74....*770
|
||||
...$..........950........190..704.380....442.............@264.821...-.918......................427................275.............*..248....
|
||||
....443..................*...........*........738.................484......................691.+.........%949..........749.....658..........
|
||||
.............678.....180.350........232...........................................934......#........315.........$....../..............+.....
|
||||
...833...920....#....*........131..................717...............222.....858..*...........%........*.530....996...........*....109......
|
||||
......*.....$.......389.331..@.......................*.................*.*......*..612......168.....672...$..........%.....788.15.......625.
|
||||
...815..........................40.589...98......./...574........451.403..327..140..............................945...136............64.....
|
||||
..............124..*........49......*..........200.........162....+.....................914..........461......./....%.................=.....
|
||||
..808.......=......363........*......27....397.........107...*.......739+....232....+........68*395.................322..166+...=.......471.
|
||||
...........465..............893..355..........*....70......963................*..133..........................................419.499.......
|
||||
..................336..62.......*...........798.......*885....../..431.851..176.............=.............583$.....................*........
|
||||
........429....../....%...........@.................18..........54...=.*.............245.171.......29.................=936.........657......
|
||||
........*................405.......405.......*689........*994...........328...980......*.....159..*.............256....................-703.
|
||||
.....444..................*......=........275.........421........479............*....54.........*..324....47.....#....332*855.946...........
|
||||
..........233....735*325.162....900.............................*................725....@.....453............499...............*............
|
||||
...820....*..............................848..974..82............178...54..............688.....................*...340.810..749.............
|
||||
....*...234.....187....*967....348......-......*.....*...&148............=..626...*.........589.....501.........82....*.....................
|
||||
.712........$....*..858.......*.................706...49......................+...653.......*.......*.......588..............168.313........
|
||||
..........776...740...........107........612..........................644.................242..................*695......236.#....*....761..
|
||||
.....767..............995..................*.447*...........130......*...........513..................................#........384....*.....
|
||||
.......................*..........306............275..............372.....................779.......&....&.....457.....731.............650..
|
||||
.......................161..........&...213...................726......866................*.......813..398......*..376...........607@.......
|
||||
..93..860....469.905............%..........*494...../215..../.#................@...980....501.................839.*....@....................
|
||||
......*...&..&...*....278........286..............$......183................840...*............941........300......578.259.558.....*..389...
|
||||
....804..669.....392...................422........743.........323..425..........774.........@....*.229.........*............*...711.8.*.....
|
||||
........................559.523..6.673.................292+..#......./....871=...........638....97..*.........629............28........975..
|
||||
..................*283...*...*........*........................*................./.@287..............124...............258.2................
|
||||
...*80.........545.....991....765.....595...361.......785...111.582...#........890......388.....*209......964...+.....+............475......
|
||||
971....666.................................*...........%.............500...653........@....*.391..........+....882..................*.......
|
||||
........*....-........................115...725.238*.....626...............*...-...987..680......342*.....................540........409....
|
||||
.153*....925.439....#..........752.....*............634..*...500&..177....47....73...................74.................+..*................
|
||||
.................844...........*....305.................425..........-..................445*566.........652...........732.14..581..219......
|
||||
.....794..482=.................163........624.....206.............................223..................*....%158..121........&......*.......
|
||||
.......*.......&..687...732...............%......*......47...........$.............+.....+..........323...........*...27/...........738.....
|
||||
.......591....313..*...../.......763..858......67.......*......550..812...$............375....334................193........................
|
||||
..860............................./................47...934.....*..........749....7..............*..830...938..............795..........#...
|
||||
...*..67...............678&..........791...........*..@......622....$..808.............$.......452./......*......................539..580...
|
||||
..320...*223........................%...........232..171.714.......139...*..............909............606.........=......272...............
|
||||
..............................88.........445..............*.....$........717.......+7.............450............874......*.................
|
||||
..............%...383......+.............@..............807...668....................................*735.................218...............
|
||||
........699..598..$.....851...&...41........651*346...................63..................89...801.........*394................&............
|
||||
.......*....................25.....$...........................*965............771.............*.......741.............$......229.@707......
|
||||
........20.853....100..438...........606..............916....20................*...........75..388.....*.....840..104.859...................
|
||||
.............*...#.........&........%............%.....................105$....317...........@.......349..../.....&...........239..104...873
|
||||
..751......715...........72..345$................881......328.38*......................465......541.................=.........&.....*.......
|
||||
....*..............................422...............208....*.............................*549..*....313.430.....892.................274....
|
||||
..88...-.....28....440.........305*...................*...789.@...../222.......898.............571....=..*............183....856............
|
||||
.....105............./..................578*........14........37..................*.......................981..........*.....*..............
|
||||
.........754..............+...561..646......637...................................544..707...97.................363..231...848.........410..
|
||||
........*........270.727.789..$.......*334......787*...979..............422@..120.....*.....#...........98............................*.....
|
||||
.......107.989......*.....................................*612...512..........*.......515..........460.@...476......463.....730....703......
|
||||
...........................132....146............................+........83*..849...................*.....&....669*...........*............
|
||||
320*......$206.........491*......*.....171..919..........486.........966$..........230......25.......126..............906....234............
|
||||
....690........%...............223.................156...*....*678.........................@....234.......=...350.....*............596...36.
|
||||
................717..511*...............322..108......*...280...........12......#......615........*.....755...*......682..904...........&...
|
||||
....#....................345......%406....*.....%.....169..........858...*...763.............%181.274.......74....$........*..998...........
|
||||
..167....230...................%........341.......................%.....827........................................814..374...........922...
|
||||
..................471........73..................852........272................717.....@617...100.....@.....................................
|
||||
...................=..................*.....769...#..........*.....545.863........$............@....791....403.....298........402*763..536..
|
||||
......................*............963.693.+............%..621............-...........579......................132*....................*....
|
||||
.535.......675.....975.314.284*..................=....49.......$.............454..211............316......................989...@.....600...
|
||||
............*..................528..........&...793...........700...................*.....770......*.........-....321.....*......277........
|
||||
...391..291..625..........................703.....................972@...797........24.92....*......822.900...703....*212.558...............
|
||||
....*..=...................160.165..........................#.............+..4..........*.262.............*......................+..........
|
||||
...837........736-..........+.....*...217-.347....446....785.....897..546....&...555.898.......382......75...48......../.....655..586.......
|
||||
.......................503......678.........%..........................*..........................=............*..235..321...=..............
|
||||
........536../....204....+.............807....................*833.677.480...322=.939...............587.....575..*.............546..........
|
||||
........=...974.....@...........$........%.......672.......317......@................&....789.-....&.....$......268..................780....
|
||||
.................*.....276...248..............87*...............................757........*..815.....129............*......................
|
||||
...*....776......951..*............*157.................254......&...628........*........104......................941.563................871
|
||||
....586...............88........283..........100.986.......*...142...*.......567...............783................................663.......
|
||||
.........641..........................213....*...*........468........127..........%.............*....202.........340*.............*.........
|
||||
.367......#......274............445..*......96.232..............175.............403.726...642..561......*............790.........433........
|
||||
....*.......241..*...498.........*...64............698*357......*...#4.....*..........*.....*..........210...961&...........................
|
||||
...152...........236.............95............................517......789.836.....236..194......................................202....720
|
|
@ -0,0 +1,141 @@
|
|||
use std::{collections::HashSet, str::FromStr};
|
||||
|
||||
pub fn run() {
|
||||
let input = include_str!("../input/day3.txt");
|
||||
// let input = r#"467..114..
|
||||
// ...*......
|
||||
// ..35..633.
|
||||
// ......#...
|
||||
// 617*......
|
||||
// .....+.58.
|
||||
// ..592.....
|
||||
// ......755.
|
||||
// ...$.*....
|
||||
// .664.598.."#;
|
||||
|
||||
let grid: Grid = input.parse().unwrap();
|
||||
let all_part_numbers = grid
|
||||
.symbol_positions()
|
||||
.into_iter()
|
||||
.flat_map(|pos| grid.adjacent_numbers(pos))
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let parts_sum: u32 = all_part_numbers
|
||||
.into_iter()
|
||||
.map(|pos| grid.parse_number_starting_at(pos))
|
||||
.sum();
|
||||
dbg!(parts_sum);
|
||||
|
||||
let gears_sum: u32 = grid
|
||||
.gear_positions()
|
||||
.into_iter()
|
||||
.map(|pos| grid.gear_ratio(pos))
|
||||
.sum();
|
||||
dbg!(gears_sum);
|
||||
}
|
||||
|
||||
struct Grid {
|
||||
chars: Vec<Vec<char>>,
|
||||
}
|
||||
|
||||
impl FromStr for Grid {
|
||||
type Err = Box<dyn std::error::Error>;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let chars = s
|
||||
.lines()
|
||||
.filter(|l| !l.is_empty())
|
||||
.map(|l| l.trim().chars().collect::<Vec<_>>())
|
||||
.collect::<Vec<_>>();
|
||||
let count = chars[0].len();
|
||||
for line in chars.iter().skip(1) {
|
||||
assert_eq!(count, line.len());
|
||||
}
|
||||
Ok(Grid { chars })
|
||||
}
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
fn symbol_positions(&self) -> Vec<(usize, usize)> {
|
||||
let mut positions = vec![];
|
||||
for (row, line) in self.chars.iter().enumerate() {
|
||||
for (col, c) in line.iter().enumerate() {
|
||||
if c.is_ascii_digit() {
|
||||
continue;
|
||||
} else if *c == '.' {
|
||||
continue;
|
||||
} else {
|
||||
positions.push((col, row));
|
||||
}
|
||||
}
|
||||
}
|
||||
positions
|
||||
}
|
||||
|
||||
fn adjacent_numbers(&self, (col, row): (usize, usize)) -> HashSet<(usize, usize)> {
|
||||
let mut adjacent = HashSet::new();
|
||||
for dc in -1isize..=1 {
|
||||
for dr in -1isize..=1 {
|
||||
if dc == 0 && dr == 0 {
|
||||
continue;
|
||||
}
|
||||
let c = col as isize + dc;
|
||||
let r = row as isize + dr;
|
||||
if c < 0
|
||||
|| c >= self.chars[0].len() as isize
|
||||
|| r < 0
|
||||
|| r >= self.chars.len() as isize
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if let Some(pos) = self.start_position_of_number_containig((c as usize, r as usize))
|
||||
{
|
||||
adjacent.insert(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
adjacent
|
||||
}
|
||||
|
||||
fn start_position_of_number_containig(
|
||||
&self,
|
||||
(col, row): (usize, usize),
|
||||
) -> Option<(usize, usize)> {
|
||||
let line = &self.chars[row];
|
||||
if !line[col].is_ascii_digit() {
|
||||
return None;
|
||||
}
|
||||
let mut start = col;
|
||||
while start > 0 && line[start - 1].is_ascii_digit() {
|
||||
start -= 1;
|
||||
}
|
||||
Some((start, row))
|
||||
}
|
||||
|
||||
fn parse_number_starting_at(&self, (col, row): (usize, usize)) -> u32 {
|
||||
self.chars[row][col..]
|
||||
.iter()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn gear_positions(&self) -> Vec<(usize, usize)> {
|
||||
self.symbol_positions()
|
||||
.into_iter()
|
||||
.filter(|(col, row)| {
|
||||
self.chars[*row][*col] == '*' && self.adjacent_numbers((*col, *row)).len() == 2
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn gear_ratio(&self, pos: (usize, usize)) -> u32 {
|
||||
let adjacent = self
|
||||
.adjacent_numbers(pos)
|
||||
.into_iter()
|
||||
.map(|pos| self.parse_number_starting_at(pos))
|
||||
.collect::<Vec<_>>();
|
||||
adjacent[0] * adjacent[1]
|
||||
}
|
||||
}
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
mod day01;
|
||||
mod day02;
|
||||
mod day03;
|
||||
|
||||
fn main() {
|
||||
day02::run();
|
||||
day03::run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue