xenua 1 year ago
parent
commit
ffc5ff27d1
Signed by: xenua
GPG Key ID: 8F93B68BD37255B8
  1. 7
      c-ratio/Cargo.lock
  2. 8
      c-ratio/Cargo.toml
  3. 140
      c-ratio/src/input
  4. 171
      c-ratio/src/main.rs

7
c-ratio/Cargo.lock generated

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

8
c-ratio/Cargo.toml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
[package]
name = "c-ratio"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

140
c-ratio/src/input

@ -0,0 +1,140 @@ @@ -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

171
c-ratio/src/main.rs

@ -0,0 +1,171 @@ @@ -0,0 +1,171 @@
use std::{collections::HashMap, fmt::Display};
static INPUT: &str = include_str!("input");
fn main() {
println!("part 1: {}", part1());
println!("part 2: {}", part2());
}
struct Grid {
data: Vec<Vec<char>>,
}
impl From<&str> for Grid {
fn from(value: &str) -> Self {
let data = value.lines().map(|line| line.chars().collect()).collect();
Self { data }
}
}
#[derive(Clone, Copy)]
struct Number {
value: usize,
location: Point,
length: usize,
}
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
struct Symbol {
sym: char,
location: Point,
}
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
struct Point {
x: usize,
y: usize,
}
impl Display for Number {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} at {},{}",
self.value, self.location.x, self.location.y
)
}
}
impl Grid {
fn get(&self, x: usize, y: usize) -> Option<char> {
self.data.get(y).and_then(|row| row.get(x)).copied()
}
fn read_numbers(&self) -> Vec<Number> {
self.data
.iter()
.enumerate()
.flat_map(|(iy, line)| {
let mut buf = String::new();
let mut nums_on_line = Vec::new();
let mut x = 0;
for (ix, c) in line.iter().enumerate() {
if c.is_ascii_digit() {
if buf.is_empty() {
x = ix;
}
buf.push(*c);
} else if !buf.is_empty() {
let length = buf.len();
let value = buf.parse().unwrap();
buf = "".into();
nums_on_line.push(Number {
value,
length,
location: Point { x, y: iy },
});
}
}
if !buf.is_empty() {
let length = buf.len();
let value = buf.parse().unwrap();
nums_on_line.push(Number {
value,
length,
location: Point { x, y: iy },
});
}
nums_on_line
})
.collect()
}
fn get_adjacent_points(&self, num: &Number) -> Vec<Point> {
let mut out = Vec::new();
let x_min = num.location.x.saturating_sub(1);
let x_max = num.location.x + num.length;
let y_min = num.location.y.saturating_sub(1);
let y_max = num.location.y + 1;
for x in x_min..=x_max {
for y in y_min..=y_max {
out.push(Point { x, y })
}
}
out
}
fn has_adjacent_symbol(&self, num: &Number) -> bool {
self.get_adjacent_points(num)
.into_iter()
.filter_map(|p| self.get(p.x, p.y))
.any(|c| c != '.' && !c.is_ascii_digit())
}
fn adjacent_symbols(&self, num: &Number) -> Vec<Symbol> {
self.get_adjacent_points(num)
.into_iter()
.filter_map(|p| {
self.get(p.x, p.y)
.filter(|c| *c != '.' && !c.is_ascii_digit())
.map(|c| Symbol {
sym: c,
location: p,
})
})
.collect()
}
}
fn part1() -> usize {
let grid = Grid::from(INPUT);
grid.read_numbers()
.into_iter()
.filter(|num| grid.has_adjacent_symbol(num))
.map(|num| num.value)
.sum()
}
fn part2() -> usize {
let grid = Grid::from(INPUT);
let mut map: HashMap<Symbol, Vec<Number>> = HashMap::new();
grid.read_numbers()
.into_iter()
.map(|num| (num, grid.adjacent_symbols(&num)))
.filter(|(_, syms)| syms.iter().any(|sym| sym.sym == '*'))
.map(|(num, syms)| {
(
syms.into_iter()
.filter(|s| s.sym == '*')
.collect::<Vec<_>>()[0],
num,
)
})
.for_each(|(sym, num)| {
if let Some(v) = map.get_mut(&sym) {
v.push(num);
} else {
map.insert(sym, vec![num]);
}
});
map.into_values()
.filter(|v| v.len() == 2)
.map(|v| v.into_iter().map(|n| n.value).product::<usize>())
.sum()
}
Loading…
Cancel
Save