Compare commits
No commits in common. "cc9475f4d664541fafdc6424880009a2787fad02" and "0f75dd0dff6bfbd004686ba28e94f73af561d0fc" have entirely different histories.
cc9475f4d6
...
0f75dd0dff
6 changed files with 5 additions and 1411 deletions
|
@ -1 +0,0 @@
|
||||||
125 17
|
|
|
@ -1 +0,0 @@
|
||||||
814 1183689 0 1 766231 4091 93836 46
|
|
|
@ -1,15 +0,0 @@
|
||||||
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
1279
res/13/input
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::HashMap, fs};
|
use std::fs;
|
||||||
|
|
||||||
pub fn solve() {
|
pub fn solve() {
|
||||||
let path = "res/11/input";
|
let path = "res/11/input";
|
||||||
|
@ -6,58 +6,11 @@ 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 mut map: HashMap<usize, usize> = HashMap::new();
|
let result: usize = 0;
|
||||||
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);
|
||||||
|
|
||||||
for _ in 0..50 {
|
let result: usize = 0;
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,79 +1,16 @@
|
||||||
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 machines: Vec<((isize, isize), (isize, isize), (isize, isize))> = contents
|
let result: usize = 0;
|
||||||
.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: isize = machines
|
let result: usize = 0;
|
||||||
.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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue