Merge branch 'main' of ssh://git.flueren.eu:2202/JonOfUs/advent-of-code-2024

This commit is contained in:
JonOfUs 2024-12-14 15:32:08 +01:00
commit 36fe030208
6 changed files with 1411 additions and 5 deletions

1
res/11/example Normal file
View file

@ -0,0 +1 @@
125 17

1
res/11/input Normal file
View file

@ -0,0 +1 @@
814 1183689 0 1 766231 4091 93836 46

15
res/13/example Normal file
View file

@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279

1279
res/13/input Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
use std::fs; use std::{collections::HashMap, fs};
pub fn solve() { pub fn solve() {
let path = "res/11/input"; let path = "res/11/input";
@ -6,11 +6,58 @@ pub fn solve() {
#[allow(unused)] #[allow(unused)]
let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
let result: usize = 0; let mut map: HashMap<usize, usize> = HashMap::new();
contents
.trim()
.split(' ')
.map(|n| n.parse::<usize>().unwrap())
.for_each(|n| {
map.insert(n, 1);
});
for _ in 0..25 {
map = blink(map);
}
let result: usize = map.iter().map(|(_, v)| v).sum();
println!("Result 1: {}", result); println!("Result 1: {}", result);
let result: usize = 0; for _ in 0..50 {
map = blink(map);
}
let result: usize = map.iter().map(|(_, v)| v).sum();
println!("Result 2: {}", result); println!("Result 2: {}", result);
} }
fn blink(map: HashMap<usize, usize>) -> HashMap<usize, usize> {
let mut new_map: HashMap<usize, usize> = HashMap::new();
for (k, v) in map.iter() {
if *k == 0 {
*new_map.entry(1).or_insert(0) += *v;
} else if k.to_string().len() % 2 == 0 {
// split num into two halves
let n1 = k
.to_string()
.chars()
.take(k.to_string().len() / 2)
.collect::<String>()
.parse::<usize>()
.unwrap();
let n2 = k
.to_string()
.chars()
.skip(k.to_string().len() / 2)
.collect::<String>()
.parse::<usize>()
.unwrap();
*new_map.entry(n1).or_insert(0) += *v;
*new_map.entry(n2).or_insert(0) += *v;
} else {
*new_map.entry((*k) * 2024).or_insert(0) += *v;
}
}
new_map
}

View file

@ -1,16 +1,79 @@
use std::fs; use std::fs;
use scan_fmt::scan_fmt;
pub fn solve() { pub fn solve() {
let path = "res/13/input"; let path = "res/13/input";
#[allow(unused)] #[allow(unused)]
let contents = fs::read_to_string(path).expect("Something went wrong reading the file"); let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
let result: usize = 0; let machines: Vec<((isize, isize), (isize, isize), (isize, isize))> = contents
.split("\n\n")
.map(|block| {
let (x1, y1, x2, y2, xt, yt) = scan_fmt!(
block,
"Button A: X+{}, Y+{}\nButton B: X+{}, Y+{}\nPrize: X={}, Y={}",
isize,
isize,
isize,
isize,
isize,
isize
)
.unwrap();
((x1, y1), (x2, y2), (xt, yt))
})
.collect();
let result: isize = machines
.iter()
.map(|m| {
let ((ax, ay), (bx, by), (xt, yt)) = *m;
let (mut a, mut b) = (0, 0);
while b * bx < xt && b * by < yt {
b += 1;
}
while (b * bx + a * ax != xt || b * by + a * ay != yt) && b > 0 {
b -= 1;
while b * bx + a * ax < xt && b * by + a * ay < yt {
a += 1;
}
}
if b * bx + a * ax == xt && b * by + a * ay == yt {
return b + a * 3;
}
return 0;
})
.sum();
println!("Result 1: {}", result); println!("Result 1: {}", result);
let result: usize = 0; let result: isize = machines
.iter()
.map(|m| {
let ((ax, ay), (bx, by), (xt, yt)) = *m;
let xt = xt + 10000000000000;
let yt = yt + 10000000000000;
let b_left = by * ax - bx * ay;
let b_right = yt * ax - xt * ay;
let b = b_right / b_left;
if b_left * b != b_right || b < 0 {
return 0;
}
let a_left = ax;
let a_right = xt - b * bx;
let a = a_right / a_left;
if a_left * a != a_right || a < 0 {
return 0;
}
a * 3 + b
})
.sum();
println!("Result 2: {}", result); println!("Result 2: {}", result);
} }