From f9fc608530d2d4c5e46d49c6aa97b016873107db Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Sat, 14 Dec 2024 15:21:05 +0100 Subject: [PATCH] d14 --- res/14/example | 12 ++ res/14/input | 500 ++++++++++++++++++++++++++++++++++++++++++++++++ src/days/d14.rs | 89 ++++++++- 3 files changed, 599 insertions(+), 2 deletions(-) create mode 100644 res/14/example create mode 100644 res/14/input diff --git a/res/14/example b/res/14/example new file mode 100644 index 0000000..2455da4 --- /dev/null +++ b/res/14/example @@ -0,0 +1,12 @@ +p=0,4 v=3,-3 +p=6,3 v=-1,-3 +p=10,3 v=-1,2 +p=2,0 v=2,-1 +p=0,0 v=1,3 +p=3,0 v=-2,-2 +p=7,6 v=-1,-3 +p=3,0 v=-1,-2 +p=9,3 v=2,3 +p=7,3 v=-1,2 +p=2,4 v=2,-3 +p=9,5 v=-3,-3 diff --git a/res/14/input b/res/14/input new file mode 100644 index 0000000..9c02718 --- /dev/null +++ b/res/14/input @@ -0,0 +1,500 @@ +p=69,11 v=-25,6 +p=67,84 v=-79,-12 +p=24,5 v=20,-73 +p=32,41 v=55,-12 +p=16,81 v=89,-62 +p=87,40 v=42,-94 +p=59,75 v=61,94 +p=79,18 v=-43,14 +p=33,64 v=-43,-54 +p=30,32 v=87,66 +p=65,69 v=-52,80 +p=65,101 v=62,-22 +p=85,18 v=-99,-7 +p=35,41 v=57,38 +p=63,33 v=-71,-32 +p=45,102 v=84,-39 +p=14,75 v=-19,-67 +p=44,74 v=-89,-9 +p=87,95 v=37,-42 +p=88,26 v=-95,39 +p=42,2 v=61,76 +p=12,34 v=-78,-32 +p=64,22 v=-77,-32 +p=12,25 v=-15,42 +p=14,56 v=88,73 +p=81,39 v=-39,-74 +p=9,76 v=-27,49 +p=45,46 v=-54,-87 +p=53,101 v=67,23 +p=81,71 v=36,32 +p=50,87 v=-68,51 +p=55,37 v=18,30 +p=84,65 v=65,-17 +p=51,80 v=-60,-1 +p=40,78 v=-57,32 +p=28,5 v=-29,-64 +p=9,98 v=66,-14 +p=6,15 v=-38,-94 +p=72,89 v=-19,31 +p=46,23 v=47,-71 +p=40,67 v=55,53 +p=59,49 v=5,11 +p=11,102 v=-93,-76 +p=12,74 v=17,45 +p=80,43 v=-65,-74 +p=6,51 v=-1,57 +p=6,24 v=80,-11 +p=61,46 v=-77,66 +p=30,28 v=-76,49 +p=80,21 v=62,42 +p=25,4 v=29,89 +p=51,82 v=27,-67 +p=50,35 v=1,-46 +p=7,99 v=-6,11 +p=47,45 v=-37,-86 +p=79,43 v=-21,58 +p=69,55 v=37,74 +p=16,42 v=-78,71 +p=93,101 v=36,52 +p=84,62 v=42,-58 +p=56,16 v=3,43 +p=99,55 v=2,66 +p=66,28 v=-19,-3 +p=77,81 v=88,-22 +p=95,5 v=-24,68 +p=59,58 v=-77,37 +p=58,89 v=-77,44 +p=27,64 v=26,28 +p=44,22 v=61,9 +p=57,44 v=41,-74 +p=38,18 v=58,-64 +p=82,39 v=-65,10 +p=100,84 v=-12,-59 +p=9,29 v=74,-53 +p=48,0 v=6,-1 +p=28,52 v=-26,-87 +p=86,14 v=-22,17 +p=86,27 v=-19,-78 +p=11,2 v=11,26 +p=97,32 v=-29,-12 +p=95,8 v=74,-7 +p=47,28 v=75,-40 +p=30,77 v=23,-38 +p=62,56 v=-67,81 +p=45,80 v=-8,-50 +p=71,30 v=4,-94 +p=78,30 v=-94,-69 +p=43,79 v=-40,-63 +p=29,81 v=61,-47 +p=13,53 v=-24,29 +p=1,48 v=-50,33 +p=22,68 v=-41,-92 +p=94,97 v=-24,-47 +p=91,65 v=-44,-66 +p=46,4 v=9,47 +p=28,89 v=19,-11 +p=60,92 v=20,41 +p=75,33 v=45,-13 +p=81,44 v=82,17 +p=58,13 v=-37,-60 +p=13,4 v=69,72 +p=57,44 v=79,-11 +p=30,5 v=98,68 +p=19,87 v=-84,93 +p=2,96 v=17,68 +p=51,93 v=-11,52 +p=3,11 v=19,68 +p=15,26 v=80,-55 +p=34,64 v=-26,-13 +p=61,23 v=-5,-77 +p=49,1 v=96,-82 +p=32,6 v=-3,-31 +p=18,63 v=-32,86 +p=59,15 v=71,-73 +p=20,37 v=40,-53 +p=28,32 v=-6,92 +p=42,15 v=58,47 +p=58,77 v=96,-71 +p=28,69 v=-78,-30 +p=78,72 v=-38,2 +p=36,24 v=-75,-73 +p=84,9 v=77,-76 +p=92,4 v=61,-84 +p=16,91 v=-47,15 +p=53,59 v=-8,16 +p=17,27 v=58,-34 +p=84,98 v=-85,-18 +p=91,9 v=16,-93 +p=43,32 v=21,99 +p=7,46 v=-70,70 +p=77,44 v=-42,55 +p=48,53 v=29,17 +p=90,49 v=48,-66 +p=11,18 v=-76,-44 +p=89,87 v=-7,-47 +p=84,52 v=94,-37 +p=55,47 v=-61,-95 +p=36,102 v=75,-43 +p=96,90 v=-88,7 +p=99,27 v=-98,89 +p=9,1 v=60,-2 +p=13,101 v=14,76 +p=88,20 v=-53,-27 +p=55,9 v=67,-11 +p=65,88 v=56,19 +p=64,86 v=47,-51 +p=77,4 v=-35,-69 +p=56,71 v=44,3 +p=74,18 v=-66,92 +p=98,40 v=-73,99 +p=8,15 v=-83,74 +p=34,12 v=6,-19 +p=73,14 v=21,10 +p=51,70 v=12,86 +p=86,36 v=71,-24 +p=65,41 v=-51,-25 +p=30,69 v=78,57 +p=2,21 v=96,85 +p=56,11 v=87,-10 +p=23,42 v=14,13 +p=20,57 v=-44,94 +p=42,71 v=-70,-58 +p=36,5 v=3,35 +p=89,33 v=-88,38 +p=61,23 v=-47,37 +p=57,68 v=-28,-75 +p=57,102 v=9,-77 +p=49,75 v=-57,63 +p=11,32 v=11,-53 +p=29,15 v=-6,-75 +p=45,65 v=29,7 +p=62,74 v=60,89 +p=94,99 v=27,-3 +p=97,68 v=25,49 +p=30,12 v=-59,72 +p=51,61 v=-11,49 +p=56,68 v=21,28 +p=73,73 v=-80,-1 +p=6,42 v=60,13 +p=8,23 v=80,22 +p=16,29 v=86,38 +p=100,88 v=74,-55 +p=93,101 v=36,-97 +p=89,13 v=-86,-72 +p=3,30 v=28,-90 +p=9,26 v=-27,-28 +p=10,86 v=-33,75 +p=94,49 v=87,-5 +p=0,62 v=5,54 +p=58,20 v=64,76 +p=13,74 v=-98,-87 +p=83,96 v=-10,39 +p=68,8 v=-94,43 +p=84,70 v=-97,48 +p=52,101 v=-43,55 +p=72,33 v=66,23 +p=93,74 v=97,32 +p=71,17 v=-31,38 +p=50,36 v=-8,95 +p=11,78 v=83,-52 +p=71,97 v=-45,93 +p=17,76 v=14,47 +p=12,19 v=-38,55 +p=27,42 v=-51,70 +p=46,96 v=-75,80 +p=45,80 v=41,36 +p=40,39 v=81,-12 +p=29,48 v=78,25 +p=16,33 v=8,59 +p=64,79 v=-5,-34 +p=82,79 v=-71,86 +p=43,27 v=54,-82 +p=50,30 v=40,-99 +p=22,41 v=20,-86 +p=40,79 v=72,-71 +p=36,6 v=-77,-75 +p=18,29 v=-32,-69 +p=10,99 v=14,-14 +p=74,9 v=-71,-85 +p=29,84 v=78,81 +p=1,62 v=-57,21 +p=6,31 v=-87,-69 +p=23,23 v=20,88 +p=90,38 v=-28,79 +p=10,32 v=-64,66 +p=36,8 v=-60,63 +p=43,90 v=35,-26 +p=82,63 v=77,-72 +p=21,94 v=-46,-39 +p=57,60 v=-57,41 +p=76,8 v=31,-45 +p=7,20 v=-35,-53 +p=9,50 v=8,-45 +p=81,0 v=13,-88 +p=92,77 v=13,49 +p=28,25 v=-52,-7 +p=78,86 v=42,40 +p=86,80 v=-7,-30 +p=90,58 v=17,61 +p=38,51 v=-67,-60 +p=24,45 v=77,-33 +p=40,16 v=81,-52 +p=61,85 v=70,-92 +p=25,16 v=-42,-77 +p=44,67 v=35,-79 +p=37,55 v=67,-87 +p=79,48 v=59,-24 +p=23,11 v=-61,-89 +p=6,22 v=-70,96 +p=5,28 v=60,-65 +p=13,40 v=98,-95 +p=48,18 v=33,-58 +p=11,98 v=-15,93 +p=99,24 v=53,-39 +p=76,22 v=99,-40 +p=82,12 v=48,5 +p=33,86 v=-37,-5 +p=40,50 v=10,45 +p=14,4 v=-26,-27 +p=2,42 v=57,-32 +p=24,16 v=32,84 +p=14,56 v=-90,53 +p=100,59 v=16,66 +p=91,95 v=65,73 +p=89,85 v=48,-59 +p=71,64 v=98,76 +p=91,31 v=89,-21 +p=21,66 v=20,82 +p=56,72 v=-17,81 +p=12,74 v=17,-12 +p=76,99 v=30,60 +p=29,13 v=-26,26 +p=65,90 v=55,37 +p=28,34 v=49,79 +p=90,26 v=77,84 +p=34,40 v=-84,70 +p=72,4 v=7,63 +p=98,100 v=86,82 +p=92,88 v=80,-31 +p=64,28 v=-51,-24 +p=89,38 v=97,-94 +p=62,1 v=-63,92 +p=54,54 v=-69,-17 +p=73,69 v=-42,-42 +p=99,8 v=2,-43 +p=92,11 v=45,96 +p=52,16 v=47,-48 +p=95,77 v=46,-90 +p=42,53 v=-38,41 +p=12,90 v=-50,93 +p=9,10 v=16,-38 +p=11,39 v=5,50 +p=27,96 v=63,-49 +p=37,25 v=82,29 +p=3,21 v=-70,-19 +p=51,99 v=-36,70 +p=26,55 v=-6,16 +p=56,21 v=-30,10 +p=70,19 v=-45,38 +p=53,11 v=79,-73 +p=16,12 v=34,5 +p=83,51 v=33,57 +p=42,49 v=96,74 +p=37,61 v=-43,82 +p=60,42 v=-82,-26 +p=46,16 v=15,47 +p=54,25 v=21,14 +p=16,13 v=-6,75 +p=2,82 v=-44,-88 +p=84,38 v=-24,95 +p=2,36 v=-7,58 +p=52,57 v=-99,24 +p=77,39 v=-80,21 +p=76,1 v=1,94 +p=16,10 v=-4,-30 +p=93,51 v=74,95 +p=32,16 v=78,55 +p=62,83 v=44,-94 +p=10,69 v=-78,45 +p=49,55 v=-21,-54 +p=28,56 v=78,45 +p=60,24 v=-5,21 +p=19,2 v=4,2 +p=31,15 v=46,9 +p=62,51 v=-91,74 +p=5,89 v=-58,97 +p=81,40 v=-91,-85 +p=47,83 v=-37,-26 +p=75,16 v=-68,-52 +p=87,26 v=-82,34 +p=75,93 v=-19,36 +p=47,31 v=93,71 +p=14,69 v=-93,-99 +p=81,72 v=-93,-85 +p=34,50 v=77,-7 +p=14,29 v=-26,-78 +p=90,23 v=-53,54 +p=18,7 v=40,37 +p=59,62 v=18,-25 +p=40,6 v=-14,-60 +p=88,33 v=65,-44 +p=33,95 v=7,-26 +p=17,83 v=69,-88 +p=11,47 v=63,8 +p=93,92 v=59,25 +p=40,9 v=6,27 +p=38,87 v=-66,-75 +p=51,25 v=-83,-65 +p=88,46 v=19,37 +p=26,37 v=-90,62 +p=22,42 v=-84,13 +p=39,85 v=-43,69 +p=67,61 v=-19,-51 +p=21,50 v=-78,92 +p=72,9 v=-22,-89 +p=60,25 v=-5,96 +p=41,8 v=-52,-74 +p=89,65 v=-68,-13 +p=15,95 v=41,2 +p=37,46 v=-67,56 +p=26,19 v=72,-15 +p=92,99 v=-27,-43 +p=37,39 v=-34,-53 +p=16,36 v=-81,-98 +p=0,51 v=-18,25 +p=78,39 v=8,-91 +p=1,94 v=-18,-18 +p=74,3 v=-82,-11 +p=95,37 v=22,-20 +p=58,35 v=-90,16 +p=24,51 v=-74,-49 +p=96,61 v=-70,36 +p=39,89 v=90,7 +p=74,33 v=59,-86 +p=78,26 v=59,38 +p=78,24 v=59,-3 +p=3,87 v=-96,-88 +p=13,13 v=49,-81 +p=30,1 v=-49,27 +p=49,13 v=-37,-85 +p=8,62 v=-49,-96 +p=68,43 v=99,-16 +p=14,67 v=75,41 +p=57,60 v=-12,-40 +p=72,64 v=59,20 +p=34,2 v=-26,-82 +p=89,51 v=-95,24 +p=85,96 v=48,85 +p=50,47 v=-66,-12 +p=64,46 v=62,-20 +p=100,42 v=48,50 +p=80,84 v=1,-96 +p=14,28 v=-15,95 +p=14,7 v=17,2 +p=7,90 v=-64,19 +p=55,63 v=-28,73 +p=73,8 v=94,-31 +p=89,101 v=-79,2 +p=52,0 v=44,-11 +p=75,34 v=-19,46 +p=34,72 v=-23,49 +p=26,29 v=23,-24 +p=49,45 v=41,58 +p=20,26 v=17,79 +p=81,24 v=36,34 +p=1,70 v=74,16 +p=48,11 v=15,-7 +p=64,66 v=76,-71 +p=46,50 v=-92,37 +p=65,59 v=-13,-70 +p=8,101 v=-51,83 +p=60,99 v=-39,33 +p=45,30 v=-5,55 +p=58,67 v=67,82 +p=94,98 v=88,-26 +p=16,68 v=39,35 +p=10,79 v=-41,7 +p=45,80 v=58,69 +p=47,44 v=-17,82 +p=22,96 v=-22,-23 +p=5,93 v=5,35 +p=13,98 v=77,64 +p=27,17 v=-17,-62 +p=51,15 v=-17,87 +p=29,68 v=-49,-68 +p=100,85 v=35,97 +p=69,54 v=18,95 +p=69,42 v=56,99 +p=17,28 v=66,13 +p=6,43 v=97,-65 +p=40,96 v=-95,64 +p=34,23 v=-95,-75 +p=74,99 v=-62,89 +p=4,82 v=83,-51 +p=85,80 v=16,-75 +p=0,41 v=-47,91 +p=75,35 v=-94,91 +p=48,9 v=95,-27 +p=65,82 v=24,-1 +p=47,46 v=-36,72 +p=22,10 v=69,49 +p=42,50 v=-86,-13 +p=42,3 v=60,-41 +p=39,26 v=-60,-42 +p=26,57 v=84,41 +p=76,70 v=-30,73 +p=16,84 v=15,-26 +p=44,53 v=-83,15 +p=32,59 v=-23,53 +p=31,99 v=-17,-98 +p=49,27 v=-97,4 +p=71,17 v=-41,-69 +p=88,99 v=-82,6 +p=33,8 v=46,2 +p=29,45 v=46,-54 +p=84,78 v=88,-34 +p=65,96 v=-71,31 +p=94,71 v=34,16 +p=27,76 v=98,73 +p=84,70 v=25,74 +p=67,69 v=4,-9 +p=51,91 v=90,22 +p=1,40 v=34,83 +p=98,4 v=34,-66 +p=74,85 v=-91,77 +p=51,32 v=87,-94 +p=68,85 v=-68,3 +p=65,7 v=12,-42 +p=16,69 v=40,28 +p=65,53 v=76,25 +p=55,97 v=95,93 +p=94,43 v=-99,91 +p=92,12 v=79,4 +p=46,84 v=15,7 +p=51,39 v=-80,46 +p=14,43 v=-87,21 +p=15,18 v=-12,51 +p=79,58 v=16,78 +p=97,12 v=-5,-5 +p=33,27 v=80,58 +p=76,72 v=-97,89 +p=16,46 v=-61,62 +p=22,53 v=51,-40 +p=8,62 v=59,50 +p=56,79 v=-5,65 +p=83,7 v=39,-81 +p=28,35 v=91,65 +p=76,11 v=-89,-89 +p=62,99 v=33,17 +p=83,57 v=66,24 +p=84,43 v=44,76 +p=0,8 v=-86,-62 +p=100,13 v=51,-85 +p=67,38 v=67,34 +p=48,11 v=-66,51 +p=46,80 v=67,69 +p=79,17 v=62,26 +p=8,100 v=-73,-80 +p=99,60 v=-73,-50 +p=89,43 v=-82,-24 diff --git a/src/days/d14.rs b/src/days/d14.rs index 974dcc9..424374e 100644 --- a/src/days/d14.rs +++ b/src/days/d14.rs @@ -1,16 +1,101 @@ use std::fs; +use rayon::prelude::*; +use scan_fmt::scan_fmt; + pub fn solve() { let path = "res/14/input"; + const WIDTH: isize = 101; + const HEIGHT: isize = 103; + #[allow(unused)] let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); - let result: usize = 0; + let robots_orig: Vec<((isize, isize), (isize, isize))> = contents + .lines() + .map(|l| { + let (px, py, vx, vy) = + scan_fmt!(l, "p={},{} v={},{}", isize, isize, isize, isize).unwrap(); + ((px, py), (vx, vy)) + }) + .collect(); + + let mut robots = robots_orig.clone(); + + robots.iter_mut().for_each(|r| { + for _ in 0..100 { + let ((px, py), (vx, vy)) = r; + *px += *vx; + *py += *vy; + if *px < 0 { + *px += WIDTH; + } + if *py < 0 { + *py += HEIGHT; + } + *px %= WIDTH; + *py %= HEIGHT; + } + }); + + let q1 = robots + .iter() + .filter(|((x, y), (_, _))| *x < (WIDTH / 2) && *y < (HEIGHT / 2)) + .count(); + let q2 = robots + .iter() + .filter(|((x, y), (_, _))| *x > (WIDTH / 2) && *y < (HEIGHT / 2)) + .count(); + let q3 = robots + .iter() + .filter(|((x, y), (_, _))| *x < (WIDTH / 2) && *y > (HEIGHT / 2)) + .count(); + let q4 = robots + .iter() + .filter(|((x, y), (_, _))| *x > (WIDTH / 2) && *y > (HEIGHT / 2)) + .count(); + + let result = q1 * q2 * q3 * q4; println!("Result 1: {}", result); - let result: usize = 0; + let mut robots = robots_orig.clone(); + + /* + for i in 0..10000 { + robots.iter_mut().for_each(|r| { + let ((px, py), (vx, vy)) = r; + *px += *vx; + *py += *vy; + if *px < 0 { + *px += WIDTH; + } + if *py < 0 { + *py += HEIGHT; + } + *px %= WIDTH; + *py %= HEIGHT; + }); + // print + if (i - 539) % 103 != 0 { + continue; + } + println!("\nSECOND: {}", i + 1); + for y in 0..HEIGHT { + for x in 0..WIDTH { + if robots.iter().any(|((px, py), _)| *px == x && *py == y) { + print!("#"); + } else { + print!("."); + } + } + println!(); + } + } + */ + + let result: usize = 7338; println!("Result 2: {}", result); }