Day03 *finally* done.

This commit is contained in:
Gabe Venberg 2023-12-15 16:32:29 -06:00
parent bd5d5ae901
commit bb245b8711
12 changed files with 1002 additions and 56 deletions

7
Cargo.lock generated
View file

@ -38,6 +38,13 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "day03"
version = "0.1.0"
dependencies = [
"aoc_libs",
]
[[package]] [[package]]
name = "day04" name = "day04"
version = "0.1.0" version = "0.1.0"

View file

@ -1,50 +1,53 @@
#[derive(Debug, PartialEq, Eq, Clone, Copy)] // #[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// range that includes the start, but excludes the end. // /// range that includes the start, but excludes the end.
pub struct Range<T> // pub struct Range<T>
where // where
T: PartialOrd + Ord + PartialEq + Copy, // T: PartialOrd + Ord + PartialEq + Copy,
{ // {
start: T, // start: T,
end: T, // end: T,
// }
use std::ops::Range;
pub trait RangeIntersection {
fn any_overlap(&self, other: &Self) -> bool;
fn calc_intersection(&self, other: &Self) -> Option<Self>
where
Self: std::marker::Sized;
fn complete_overlap(&self, other: &Self) -> bool;
} }
impl<T> Range<T> impl<T> RangeIntersection for Range<T>
where where
T: PartialOrd + Ord + PartialEq + Copy, T: PartialOrd + Ord + PartialEq + Copy,
{ {
pub fn new(start: T, end: T) -> Self {
Self {
start: start.min(end),
end: end.max(start),
}
}
/// calcs whether self and other overlap at all. symettric. /// calcs whether self and other overlap at all. symettric.
pub fn any_overlap(&self, other: &Self) -> bool { fn any_overlap(&self, other: &Self) -> bool {
self.start < other.end && self.end > other.start if self.is_empty() || other.is_empty() {
false
} else {
self.start < other.end && self.end > other.start
}
} }
/// calculates the range that is part of both ranges. /// calculates the range that is part of both ranges.
/// Returns None if the ranges do not overlap. /// Returns None if the ranges do not overlap.
pub fn calc_intersection(&self, other: &Self) -> Option<Self> { fn calc_intersection(&self, other: &Self) -> Option<Self> {
if self.any_overlap(other) { if self.any_overlap(other) {
Some( Some(self.start.max(other.start)..self.end.min(other.end))
Self::new(
self.start.max(other.start), self.end.min(other.end)
)
)
} else { } else {
None None
} }
} }
///calcs whether self completely contains other ///calcs whether self completely contains other
pub fn complete_overlap(&self, other: &Self) -> bool { fn complete_overlap(&self, other: &Self) -> bool {
self.start <= other.start && self.end >= other.end if self.is_empty() || other.is_empty() {
} false
} else {
pub fn in_range(&self, other: T) -> bool { self.start <= other.start && self.end >= other.end
self.start <= other && other < self.end }
} }
} }
@ -54,31 +57,31 @@ mod tests {
#[test] #[test]
fn calc_intersection() { fn calc_intersection() {
let a = Range::new(1, 5); let a = 1..5;
let b = Range::new(3, 9); let b = 3..9;
let c = Range::new(4, 6); let c = 4..6;
let d = Range::new(6, 8); let d = 6..8;
let e = Range::new(1, 4); let e = 1..4;
assert_eq!(a.calc_intersection(&b), Some(Range::new(3,5))); assert_eq!(a.calc_intersection(&b), Some(3..5));
assert_eq!(b.calc_intersection(&a), Some(Range::new(3,5))); assert_eq!(b.calc_intersection(&a), Some(3..5));
assert_eq!(a.calc_intersection(&d), None); assert_eq!(a.calc_intersection(&d), None);
assert_eq!(d.calc_intersection(&a), None); assert_eq!(d.calc_intersection(&a), None);
assert_eq!(c.calc_intersection(&b), Some(c)); assert_eq!(c.calc_intersection(&b), Some(c.clone()));
assert_eq!(b.calc_intersection(&c), Some(c)); assert_eq!(b.calc_intersection(&c), Some(c.clone()));
assert_eq!(e.calc_intersection(&b), Some(Range::new(3,4))); assert_eq!(e.calc_intersection(&b), Some(3..4));
assert_eq!(b.calc_intersection(&e), Some(Range::new(3,4))); assert_eq!(b.calc_intersection(&e), Some(3..4));
} }
#[test] #[test]
fn test_any_overlap() { fn test_any_overlap() {
let a = Range::new(1, 5); let a = 1..5;
let b = Range::new(3, 9); let b = 3..9;
let c = Range::new(4, 6); let c = 4..6;
let d = Range::new(6, 8); let d = 6..8;
let e = Range::new(1, 4); let e = 1..4;
assert!(a.any_overlap(&b)); assert!(a.any_overlap(&b));
assert!(b.any_overlap(&a)); assert!(b.any_overlap(&a));
@ -96,11 +99,11 @@ mod tests {
} }
#[test] #[test]
fn test_complete_overlap() { fn test_complete_overlap() {
let a = Range::new(1, 5); let a = 1..5;
let b = Range::new(3, 9); let b = 3..9;
let c = Range::new(4, 6); let c = 4..6;
let d = Range::new(6, 8); let d = 6..8;
let e = Range::new(1, 4); let e = 1..4;
assert!(a.complete_overlap(&a)); assert!(a.complete_overlap(&a));
assert!(a.complete_overlap(&e)); assert!(a.complete_overlap(&e));

9
days/day03/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "day03"
authors.workspace = true
description.workspace = true
version.workspace = true
edition.workspace = true
[dependencies]
aoc_libs.workspace = true

140
days/day03/src/input.txt Normal file
View file

@ -0,0 +1,140 @@
....................................18..........889.270.....748.280...997.................617..............622........763............476....
...529......434.....191..489...717...@.....................&....................939*7.....*....................606............760....*......
....*...473....*221................$........182......812........493.84....793..........794.......589..407..41...*.....................68....
..380.................553..559..105.....749.............*..........*.........*.............-968....@.....*..*....908..603...................
.........................................#...600.........441..#833...490....366.........................225..488........&...456.........224.
........288=......896*469..........831..........*....................*..............#...@.146.......*.............790#......%...194....@....
..730.........................250+......./......359....=.462..............138...#.49..713.-........342...604*676.........=.....*.........418
....*....907.......526................274...........346.....*.876..........-..725.............376......................541...600.....366*...
.....747...*..........*......56$...10........422........-.86.....*.......................728./............*.................................
.........486.........349............*..........=......696....$.............850&......906*......831.....539.195.........202..................
..............592.............................................592....+.847.......676......365..=......................*........469.%........
.542............*...............385....60.................329......657..@......=..*....&...........355.........=....52.........*....352.....
.............994......937...................45.....48........*..............835...791..654....................75.............523............
...................&..............269.486.....+...*......&....954.........#........................246&...&..............203......693.......
.....262..........920...248.454......*...........289...822.........530...569..............................256.......929....*......*.........
........%............../.........................................../..............+..57.@............616=...........*...462...198.757.......
...............65........=.........764*.....$581.727........62.......4.....796..280......444.807...............174@.20................512...
.........877....*...687..187.522.......853..........*321.....*........*345...........513.....=.......849..........................@....*....
.........*...635......*........=..............................380...........77...44*..*................#......*......484.....65.886.........
.....153.........@.&.....320........96..619....510......221.............139..*........957..977......20.....232.20......&.....*..............
......*...397*.610..479...*....=....%.....*.......*.............839.......*................*............*.................144........#554...
..510...................927.....384........714...286....................799.....733.........832......815.187.....*....470...................
........892..................................................645.427.............%......562...................133.442....*906...............
.........=..........803............577.......%.....447..185.........*957.637*97..............132...............................83.258@..$...
...........=55......+....+...435.......930...143.............475................./..............#......................=..675...%......709..
777....................841..*...........*......................*........*.........859...../...............640%..955..903.*..................
..............639/...........803.829.....877........#.........716....873....387.........857..837.................*.......390................
.................................*...............994......................%....*.............&.......749.........718.174.....138..697...463.
.....................697...873#.435......*141...........914*656....@......414..981.....541.......%....*...+.#.........*......&....+.........
.....................*................544.......524.............446.......................+.397..486....603.463......497....................
.........161........412....273..218..............*..80............................708.......=....................899........+......-........
............*..435*.........-..$.........666..401.....*897.214..472..499............*...955......................@...867....607.491..43@....
666%.851..653......44...............152....................*....@.....*..........294.....*...313...237....580...............................
............................989....*.....954.....69.....488../.......90.495*..........697....*.......*.....*..686.......=.......594*981.....
.....104..#...................=.....773...$../.....*396.....959.............272...........433........889.821.....*759.469...................
.......*..865.318.@812........................267...............969.............................758........................913........646...
......132......*.........*699......+...............+.......126......468.....651............424........270.765.....53...965.*...19...........
..............725.....449.......337..187........862.............553*........*....=282.28....*........*....*........*.....%.779..&....218*...
.....744...36.....203.......452.......%..846........&......................................811..501..342...891.....776...................395
.................$.....567&...@..689.......%.......619..392.........847#..#.....236*833............*.........................408............
............./.....732............*........................*..............568................375..561..-270..111....=..565..................
...........801............221....336...493....263..81*.....981...........................449*....................809......*239...........686
...................+.........*87......*..........#....510...........20*684.../..387.....................................*...........132.....
.......674......&..364...........734.141.854*290...........................572.*......878...929.141.#.......526...919.646..*..........*.....
..............980...............*...............................500...229......876....*..............220.....*.....*........809......522....
394....374......................194......660.....172..............&.....*...........774...................367......452..952.................
.......*...................918/............*.....*.......220........776.542..............843..857....560....................................
........728.......@951.668.........651......589.857.........../.....*.........354....509.......@........*.................+.....151.........
......-......262.........#.....*95...................878./...563.642.............#.......................586........638..933....=...........
504.454.......*............540..........524..........*...531...........942...................632@.830.........302..-................%.......
.........498..357..........*....&..........%.......340.........925.......*....623.707.479............*36....%........+.....659*149.401.117..
34..................608.....156.747...602@...............................748..*....*..$............*.........565.....93................*....
........426.................................60.938...........................615.328.....818....143.118..................*.@...........916..
...............547..............25*32.......*..*.......64..-813..........405.........802...*............188............246.236..............
652................*426...................74.....822...*.................*...533....*....764..............*....................%............
....920.64......437......598....738..#..............+..44...625...*....879......*..879.......668.#.......509...385..123....677..553.62......
....*....+.783..........*..........*.552...534...............*.....312........=.........@...-....819............#..-..........*.............
....779.......&....21..582......264.........*.............564...............561........811...........................943....68..$...........
.....................&.....................915.472*918.................848.......943.........803....................*..........386..........
...........................62........668..........................*801.*.........*...............878.......212....613.......................
.......208.......302..313*...*........*...........&.717........237.....11.........551.569.................*...............717...............
...742*......886*.........15.402....806...84...527.....*....=................650......*.....56.....401...276......964......-.....@..........
..........................................*.........411...277.722.337.........*.....915.252*........*................+..........356....553..
.......*...2.........406...............922.....=605............*............403......................316..........84.......%..........=.....
.......385.+....%893........200............150...........420.548.......+111...........70............................*675.895................
...........................+.......198-...@.........532...........23..........349#....*......#...............................920.........844
.....#.....387/.............................31..638..*....580.....#...........................397..720..........161...+.......*..628....*...
......816..............669....36..#393..848..........884.+...........&676.....876*341...565%.......*..........*....#...914...356..-..210....
...............646........&..$.........*...................542....................................370......686.567..........................
............../....94................414......18.............................861....*.....878..........24@.............875=.693*320..864.989
...................*.........665..............*...595..........201......738..+...784..187....*237..../..............................*.......
......236-.......126.....641*..../............486.*.....638.............................*.%.......242....*153....+.........63.837.723.41....
...........35....................114...............661....-....534.........626*......796..28..........579......367..78.929...*..............
......108.........271*144.........................................*............5.....................................*.*.......669.533..704.
.........*635................#..@947.......290.....544.............707...................465..632.....771...181........471.380*......*......
.....114..........89$.....699...........=....*........=.....416...........................&.....$......$..........365...............131.....
.......-.............................410....64......*.........*.........&....684.....15.....317...340*..............*.......................
.........26.609........152........................94.498.....26......671.......*.221*..........*......718...824.....56...........915........
..218..............707*.......786.....839................790.....192.........181......836....287..............%.......................-.....
......937.722...............%.........*...378$...........*.........@.449..............%............226...........&...........428.......341..
.795....*...&..185..405.....60.121....159.......275...996............*....981.....710........363...%...528....-.888....*641...*....880......
...-..806.......*.....*......................@.....#......545..467...289..*.........*....681..........*.....86......326.......664.&.........
.............221.....142...860+....933..653..291......52&../..*............300.%...811...*..................................................
......................................-....*.....861.........149...............673......762........./284..588..................%.....316....
..373......290..............................665.....*................&.............&552.....51.............=.......105.......162.......*....
...-........*.....388........%...772....................=...57....163..932..41...............*...693.116*......#.....*..............434.....
......*489...248..*........395...=.....................676..#..........*...................835...........570.199..678.......................
780...............713................952...479...379..................83.554.........919.......486..980...............984.$468.....720......
..........66..-.....................*.......*....*...........................380%.....$.........*..........899*..118..............*.........
............*..694........382....793.........668.92.....747.......49.....&..........#.....284...462....303........*....518......50.....798..
...*146..642.......@880...*............79=.............*.....93....&...885...785..780.......*..........=...-.....663..................*.....
533...........996.........352..................619%...200......+......................142....408....=....634............379*491.588@..183...
........$127........970...............902..........................943.649............$..........909.........619............................
................-..*....640............*.......-459..#.....446......&.....*.934.....#....313............/....*........509...473..901....132.
..........*..415..805..*.....786.....12...............863...*...........674....$...296.....&...474......281...326.......&....*......$.......
........909...........136.......@.........645................961................................/...250...............$................*554.
...*.........589....................253..../...166.....&429.........96......568*684.%186...............*958........678..............466.....
.759............*96..........658.......*......*...............105...-....................428.............................=...73.............
......54*...................+......168.592..536.=................*8...671........753+.......*.................74........564....*............
.304.....728..764................=...*...........804.575....266........*.....................94......27.316....=.-644.*.......209...485.....
.....997.......*.........#....792...855.................#......*169...496./615........683.......225.*..................474............$.395.
............573....206.477...................*158..........702....................298../......*...=..100....................714.........*...
448...............*..................769...27........691..$.......178....675......*........980.28...............*727...%.....@...201.545....
............223...561..........261.....*.............*................+...........610.250....................496......408..........*........
....@775.....*............&.....@....195..........303..+.............141..865..............629..+....659...................855*....958......
...........407........92...22................493......605..................@..........477...*....160...................743.....163..........
......................../......*......602@......=..+.......*....668$..245................*...731..........69.....213.....*........./....%...
................213*........903.75..........64......945...221.........+.......740........739..........393*...301*......562...../...53...4...
...164&.....145.....580.......................*................425........900......828*............................*........536.............
...............*.....................91....185..................../....................85.................414...890.204.....................
....772............422..*.351...218..................584................263...849...............*...639*.....*..............................
.......*307...897...*..28.*........*.*49.......300....=../72...........%......*.....544......974..@.....214...55.......347.............914..
901............*..67......840...343.........................................447....*...............400...................*..................
.......684..498.........*......................544...347..+........493.............612..........59.........741..........593.........416*....
..........*........369.577........690...770....*......*..983.*160....#.................623*248.#......437..*........258.................275.
....+...603....+...*............./......*...913....609.................823........................62.%....83...........&....................
....538.....356..=..117......-.........556................................*..............920.......*...............746......................
................483.......197...................&....................231...210......92....*.....982..&......199....$........848.$.....$.....
...370..413.........$.............*............556........325...902@....$..........@...182..........933................*.....&...829.402....
......*..%...........524.131..134..163.....185...............*...............................756........282&.774......822...................
....340....373...*...............*...........*....649.20....289....799..549.939.....120..........61............*..345.......778...550.267...
............&...699......&319..170...492....61.....*.......................*.....+.................*.........345.....@........*...+.........
534.............................................144..849.......174......*........219.....$......140...476..................970..............
.../....681............444.....819$...................*..=305../.....413.95...............589.................229...235................644..
...........-.....28.......*403................&....548.............&...........17..........................33..........+........962.........
373..........500....%429.....................291........*....881.296.....163*....*..............796...585....&.............802..*.......138.
................*..................983&................809.....*.............417.999....$.......*...............633.........*...322....*....
......907..$921.............665...........731...............903.........................638..662..............-..........495.........130.9..
.........*......168............&..353.....-..........639.........#...........349......................660....914...570......................
...%......695..*....679...........=..........$...958.+.....494..369..........*...........................*44........*.........964......548..
...826.........268..*.....675..............136....-...................650.964....728.823.403..................842.983..........*......*.....
...................328................................519.....210.......................*....143.....*46....../.................241..55.....
.......855$...199.......526.....849...764*294..........#.....*.....280..........261.885.........#.434...................610.................
...............=...............+...................498.......936......*..831.......*.......&...........462.939+..439.......*845.....443.....
.....@360.842......986%.902-.....21.307....234*......*...324.......505....*.............315..............*.............................*....
..........*....................*.*....*..............42..*................474...../919................50.674..713................./..27.....
........249....291...........448.622..228.......308.........189...............629........-...................*............594..127......782.
..................*173..817.....................*.......91.....*..#................*649...492...400.........17...878.....*..........950.....
...32.674......-........%.....402/...........610..%.....*.....549..853...........96.................=.............*....779.......@..........
.............885.................................600.456...........................................679.............16.........392.....841...

14
days/day03/src/main.rs Normal file
View file

@ -0,0 +1,14 @@
mod part1;
mod part2;
mod parse;
fn main() {
let input = include_str!("./input.txt");
let structured_input = parse::parse(input);
println!("Part One");
println!("Result: {}", part1::part1(&structured_input));
println!("Part Two");
println!("Result: {}", part2::part2(&structured_input));
}

560
days/day03/src/parse.rs Normal file
View file

@ -0,0 +1,560 @@
use std::{iter, ops::Range};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PartNumber {
pub number: usize,
pub x: Range<usize>,
pub y: usize,
}
impl PartNumber {
fn is_adjacent_to_symbol(&self, symbol: &Symbol) -> bool {
(self.x.start.saturating_sub(1)..=self.x.end).contains(&symbol.x)
&& (self.y.saturating_sub(1)..=self.y + 1).contains(&symbol.y)
}
pub fn is_ajacent_to_any_symbol(&self, symbols: &[Symbol]) -> bool {
symbols
.iter()
.any(|symbol| self.is_adjacent_to_symbol(symbol))
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Symbol {
pub x: usize,
pub y: usize,
pub char: char,
}
impl Symbol {
fn ajacent_part_numbers(&self, part_numbers: &[PartNumber]) -> Vec<usize> {
part_numbers
.iter()
.filter(|pn| pn.is_adjacent_to_symbol(self))
.map(|pn| pn.number)
.collect()
}
pub fn gear_ratio(&self, part_numbers: &[PartNumber]) -> Option<usize> {
if self.char != '*' {
return None;
}
let ajacent_parts = self.ajacent_part_numbers(part_numbers);
if ajacent_parts.len() != 2{
return None;
}
Some(ajacent_parts.iter().product())
}
}
pub type StructuredInput = (Vec<PartNumber>, Vec<Symbol>);
pub fn parse(input: &str) -> StructuredInput {
let mut part_numbers = vec![];
let mut symbols = vec![];
for (y, line) in input.lines().enumerate() {
let mut length: usize = 0;
let mut number: Option<usize> = None;
for (x, char) in line.chars().chain(iter::once('.')).enumerate() {
if let Some(digit) = char.to_digit(10) {
length += 1;
// this essentially 'shifts' the number left if it already exists.
number = number.map_or(Some(digit as usize), |n| Some(n * 10 + digit as usize))
} else {
if char != '.' {
symbols.push(Symbol { x, y, char })
}
// if number is not none, we must have just 'left' a number.
if let Some(number) = number {
part_numbers.push(PartNumber {
number,
x: (x - length)..x,
y,
})
}
number = None;
length = 0;
}
}
}
(part_numbers, symbols)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gear_ratio() {
let partnums = vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
];
let symbols = vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
];
assert_eq!(symbols[0].gear_ratio(&partnums), Some(467*35));
assert_eq!(symbols[1].gear_ratio(&partnums), None);
}
#[test]
fn test_num_ajacent_parts() {
let partnums = vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
];
let symbols = vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
];
assert_eq!(symbols[0].ajacent_part_numbers(&partnums), [467, 35]);
assert_eq!(symbols[1].ajacent_part_numbers(&partnums), [633]);
}
#[test]
fn test_is_ajacent_to_symbol() {
let partnums = vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
];
let symbols = vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
];
assert!(partnums[0].is_adjacent_to_symbol(&symbols[0]));
assert!(!partnums[0].is_adjacent_to_symbol(&symbols[1]));
assert!(partnums[3].is_adjacent_to_symbol(&symbols[1]));
}
#[test]
fn test_is_ajacent_to_any_symbol() {
let partnums = vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
];
let symbols = vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
];
assert!(partnums[0].is_ajacent_to_any_symbol(&symbols));
assert!(!partnums[1].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[2].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[3].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[4].is_ajacent_to_any_symbol(&symbols));
assert!(!partnums[5].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[6].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[7].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[8].is_ajacent_to_any_symbol(&symbols));
assert!(partnums[9].is_ajacent_to_any_symbol(&symbols));
}
#[test]
fn test_parse() {
let input = concat!(
"467..114..\n",
"...*......\n",
"..35..633.\n",
"......#...\n",
"617*......\n",
".....+.58.\n",
"..592.....\n",
"......755.\n",
"...$.*....\n",
".664.598..\n",
);
assert_eq!(
parse(input),
(
vec![
PartNumber {
number: 467,
x: 0..3,
y: 0
},
PartNumber {
number: 114,
x: 5..8,
y: 0
},
PartNumber {
number: 35,
x: 2..4,
y: 2
},
PartNumber {
number: 633,
x: 6..9,
y: 2
},
PartNumber {
number: 617,
x: 0..3,
y: 4
},
PartNumber {
number: 58,
x: 7..9,
y: 5
},
PartNumber {
number: 592,
x: 2..5,
y: 6
},
PartNumber {
number: 755,
x: 6..9,
y: 7
},
PartNumber {
number: 664,
x: 1..4,
y: 9
},
PartNumber {
number: 598,
x: 5..8,
y: 9
}
],
vec![
Symbol {
x: 3,
y: 1,
char: '*'
},
Symbol {
x: 6,
y: 3,
char: '#'
},
Symbol {
x: 3,
y: 4,
char: '*'
},
Symbol {
x: 5,
y: 5,
char: '+'
},
Symbol {
x: 3,
y: 8,
char: '$'
},
Symbol {
x: 5,
y: 8,
char: '*'
}
]
)
);
}
}

106
days/day03/src/part1.rs Normal file
View file

@ -0,0 +1,106 @@
use crate::parse::*;
pub fn part1(input: &StructuredInput) -> usize {
input
.0
.iter()
.filter(|pn| pn.is_ajacent_to_any_symbol(&input.1))
.map(|pn| pn.number)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
let input = (
vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
],
vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
],
);
assert_eq!(part1(&input), 4361);
}
}

105
days/day03/src/part2.rs Normal file
View file

@ -0,0 +1,105 @@
use crate::parse::*;
pub fn part2(input: &StructuredInput) -> usize {
input
.1
.iter()
.filter_map(|symbol| symbol.gear_ratio(&input.0))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part2() {
let input = (
vec![
PartNumber {
number: 467,
x: 0..3,
y: 0,
},
PartNumber {
number: 114,
x: 5..8,
y: 0,
},
PartNumber {
number: 35,
x: 2..4,
y: 2,
},
PartNumber {
number: 633,
x: 6..9,
y: 2,
},
PartNumber {
number: 617,
x: 0..3,
y: 4,
},
PartNumber {
number: 58,
x: 7..9,
y: 5,
},
PartNumber {
number: 592,
x: 2..5,
y: 6,
},
PartNumber {
number: 755,
x: 6..9,
y: 7,
},
PartNumber {
number: 664,
x: 1..4,
y: 9,
},
PartNumber {
number: 598,
x: 5..8,
y: 9,
},
],
vec![
Symbol {
x: 3,
y: 1,
char: '*',
},
Symbol {
x: 6,
y: 3,
char: '#',
},
Symbol {
x: 3,
y: 4,
char: '*',
},
Symbol {
x: 5,
y: 5,
char: '+',
},
Symbol {
x: 3,
y: 8,
char: '$',
},
Symbol {
x: 5,
y: 8,
char: '*',
},
],
);
assert_eq!(part2(&input), 467835);
}
}

View file

@ -7,8 +7,8 @@ fn main() {
let structured_input = parse::parse(input); let structured_input = parse::parse(input);
println!("Part One"); println!("Part One");
println!("Result: {}", part1::part1()); println!("Result: {}", part1::part1(&structured_input));
println!("Part Two"); println!("Part Two");
println!("Result: {}", part2::part2()); println!("Result: {}", part2::part2(&structured_input));
} }

View file

@ -1,4 +1,6 @@
pub fn parse(input: &str) -> usize { pub type StructuredInput = usize;
pub fn parse(input: &str) -> StructuredInput {
unimplemented!() unimplemented!()
} }

View file

@ -1,6 +1,6 @@
use crate::parse::*; use crate::parse::*;
pub fn part1() -> usize { pub fn part1(input: &StructuredInput) -> usize {
unimplemented!() unimplemented!()
} }

View file

@ -1,6 +1,6 @@
use crate::parse::*; use crate::parse::*;
pub fn part2() -> usize { pub fn part2(input: &StructuredInput) -> usize {
unimplemented!() unimplemented!()
} }