adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
f31c202b7a601a53f261ded29cc0e4b4d88dcc90
parent
58f3f9d86ec5647efb5e4e08d96454e20b9ecb8a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-12-10 19:54
2024-12-10

Diffstat

A 2024/10/input.txt 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2024/10/solution.rs 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A 2024/10/test.txt 8 ++++++++

3 files changed, 130 insertions, 0 deletions


diff --git a/2024/10/input.txt b/2024/10/input.txt

@@ -0,0 +1,54 @@
   -1     1 034565433432321821235456556798712438943432345432101010
   -1     2 125674344341210930012367349897601123876501656543238923
   -1     3 545981265210523841112398232098532014101665437654147014
   -1     4 436780378019654756501454101123442125012878328956056985
   -1     5 521091489328765661498760130210356916763969012347841076
   -1     6 342698456410644570187321221202197809854054521078932987
   -1     7 434784387569653089298435430143087612870123654169410673
   -1     8 525601295678762120387589347834561543965434783254321034
   -1     9 510567856789854871456678956921670123457650199165890321
   -1    10 654356943889903962345665067830589894398743278056785410
   -1    11 701245012958712653434784167845678765210634565410156789
   -1    12 898434321867012344321093254976104543231023876321943089
   -1    13 686321780154102101287765323987267650122012989423872176
   -1    14 567980691233283237898894015416398532143606788714565765
   -1    15 408974500543098946907623234505458545014545698601159854
   -1    16 312363217632127655416510107612369876105894332589018345
   -1    17 221457898565634562323421458733478983276701201478121230
   -1    18 100656967874567641032102369021098744589856782363210121
   -1    19 789345450923498634543001078112321654678345891054308701
   -1    20 654212321012654521694210981205430903543232654143489610
   -1    21 503403430156787610784300870376567812654121743267876523
   -1    22 012567567343898525695421889487854925780010893210965432
   -1    23 123498788210123436786438976598943056891234984921014101
   -1    24 019567699101898345877567887678762147878765675870123012
   -1    25 108754543210567212968900194589054038969454566767636323
   -1    26 205610121056750303453213293032123229454323879098545438
   -1    27 014981232347841219874984782143210110301012978121654569
   -1    28 123672654338932306705675676154560121212307665430743478
   -1    29 298543789221069455012310145069430430925408578989832387
   -1    30 567010176109178764563498232178521567876519454567601296
   -1    31 432123485458769853074567310123678106898323343218970345
   -1    32 012034394367458102189601489433439256765401252102181234
   -1    33 123465210210343012078732676512508349850106769843098321
   -1    34 054896990101212043896543216109612556743219856784587410
   -1    35 969887889654302158987894107898743445654340145697656531
   -1    36 878791078745983467856765545677654354567854234548545621
   -1    37 745622363215676500345653231987980123498960129639343210
   -1    38 034215454301876211256570120345678154323870038721658343
   -1    39 122100189321945308967981011234569067212721801290569012
   -1    40 543893276430932457898876323233432178008934980387378143
   -1    41 456764345567801966501895400145567889127645673456269654
   -1    42 369632105478765873432689312176876901234560012562158765
   -1    43 278543254309894569546576543089985432321071239873043210
   -1    44 103450569212723278637434010034394321810980548954560127
   -1    45 894321078112010198728922121165223010901289687643073438
   -1    46 765697151003434587017213033278112987874398796542189569
   -1    47 056788769876524326501304544569001056965834321233675678
   -1    48 145679458985015415432987693432102345216945610344569547
   -1    49 230989347034376102341056780540989104307898700123678632
   -1    50 321078234123289211652346791691071234487465410878766781
   -1    51 989165106000105690789905882782360943296578321969215690
   -1    52 076234245612234782349814943485456850123489100154304385
   -1    53 145210238763145671456723876596306765012101256701235276
   -1    54 234300149854078980365432101487217898721032349810120123

diff --git a/2024/10/solution.rs b/2024/10/solution.rs

@@ -0,0 +1,68 @@
   -1     1 #[path = "../lib.rs"]
   -1     2 mod lib;
   -1     3 
   -1     4 fn parse_input() -> (Vec<u8>, usize) {
   -1     5     let mut map = vec![];
   -1     6     let mut h = 0;
   -1     7     for line in lib::iter_input() {
   -1     8         h += 1;
   -1     9         for c in line.chars() {
   -1    10             map.push(c.to_digit(10).unwrap() as u8);
   -1    11         }
   -1    12     }
   -1    13     return (map, h);
   -1    14 }
   -1    15 
   -1    16 fn unique(a: &mut Vec<usize>) {
   -1    17     a.sort();
   -1    18     let mut i = 1;
   -1    19     while i < a.len() {
   -1    20         if a[i] == a[i - 1] {
   -1    21             a.remove(i);
   -1    22         } else {
   -1    23             i += 1;
   -1    24         }
   -1    25     }
   -1    26 }
   -1    27 
   -1    28 fn find_next(positions: Vec<usize>, map: &Vec<u8>, h: usize) -> Vec<usize> {
   -1    29     let w = map.len() / h;
   -1    30     let mut result = vec![];
   -1    31     for i in positions.into_iter() {
   -1    32         let v = map[i] - 1;
   -1    33         if i / w != 0 && map[i - w] == v {
   -1    34             result.push(i - w);
   -1    35         }
   -1    36         if i / w != h - 1 && map[i + w] == v {
   -1    37             result.push(i + w);
   -1    38         }
   -1    39         if i % w != 0 && map[i - 1] == v {
   -1    40             result.push(i - 1);
   -1    41         }
   -1    42         if i % w != w - 1 && map[i + 1] == v {
   -1    43             result.push(i + 1);
   -1    44         }
   -1    45     }
   -1    46     return result;
   -1    47 }
   -1    48 
   -1    49 fn main() {
   -1    50     let (map, h) = parse_input();
   -1    51     let mut sum1 = 0;
   -1    52     let mut sum2 = 0;
   -1    53 
   -1    54     for (i, v) in map.iter().enumerate() {
   -1    55         if *v == 9 {
   -1    56             let mut positions = vec![i];
   -1    57             for _ in 0..9 {
   -1    58                 positions = find_next(positions, &map, h);
   -1    59             }
   -1    60             sum2 += positions.len();
   -1    61             unique(&mut positions);
   -1    62             sum1 += positions.len();
   -1    63         }
   -1    64     }
   -1    65 
   -1    66     println!("part1: {}", sum1);
   -1    67     println!("part2: {}", sum2);
   -1    68 }

diff --git a/2024/10/test.txt b/2024/10/test.txt

@@ -0,0 +1,8 @@
   -1     1 89010123
   -1     2 78121874
   -1     3 87430965
   -1     4 96549874
   -1     5 45678903
   -1     6 32019012
   -1     7 01329801
   -1     8 10456732