Compare commits
No commits in common. "6585356f0e978be4f991b7226e8b7fccc36f3a6d" and "e215cd7472725526fbe2734d66d1d6e57e87b7e5" have entirely different histories.
6585356f0e
...
e215cd7472
|
@ -131,9 +131,11 @@ impl NumButton {
|
|||
}
|
||||
}
|
||||
|
||||
type Input = String;
|
||||
|
||||
#[aoc_generator(day21)]
|
||||
fn parse(input: &str) -> &'static str {
|
||||
unsafe { std::mem::transmute(input) }
|
||||
fn parse(input: &str) -> Input {
|
||||
input.to_string()
|
||||
}
|
||||
|
||||
type Path = [DistanceSize; 6];
|
||||
|
@ -334,7 +336,7 @@ const CHAR_IDX: [usize; 10] = [
|
|||
];
|
||||
|
||||
#[aoc(day21, part1)]
|
||||
fn part1(input: &str) -> DistanceSize {
|
||||
fn part1(input: &Input) -> DistanceSize {
|
||||
let num_paths = const {
|
||||
let steps = precompute_steps_new::<2>([[1; 5]; 5]);
|
||||
let paths = precompute_paths();
|
||||
|
@ -356,7 +358,7 @@ fn part1(input: &str) -> DistanceSize {
|
|||
}
|
||||
|
||||
#[aoc(day21, part2)]
|
||||
fn part2(input: &str) -> DistanceSize {
|
||||
fn part2(input: &Input) -> DistanceSize {
|
||||
let num_paths = const {
|
||||
let steps = precompute_steps_new::<25>([[1; 5]; 5]);
|
||||
let paths = precompute_paths();
|
||||
|
@ -383,10 +385,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(part1(parse("379A")), 64 * 379);
|
||||
assert_eq!(part1(parse("179A")), 68 * 179);
|
||||
assert_eq!(part1(parse("456A")), 64 * 456);
|
||||
assert_eq!(part1(parse("980A")), 60 * 980);
|
||||
assert_eq!(part1(parse("029A")), 68 * 29);
|
||||
assert_eq!(part1(&parse("379A")), 64 * 379);
|
||||
assert_eq!(part1(&parse("179A")), 68 * 179);
|
||||
assert_eq!(part1(&parse("456A")), 64 * 456);
|
||||
assert_eq!(part1(&parse("980A")), 60 * 980);
|
||||
assert_eq!(part1(&parse("029A")), 68 * 29);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,233 +0,0 @@
|
|||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
|
||||
type Input = (usize, [Vec<usize>; 26 * 26], HashMap<usize, String>);
|
||||
|
||||
#[aoc_generator(day23)]
|
||||
fn parse(input: &str) -> Input {
|
||||
let mut i = 0;
|
||||
let mut names = HashMap::new();
|
||||
let mut ids = HashMap::new();
|
||||
let mut adj_list = [const { Vec::new() }; 26 * 26];
|
||||
input.lines().for_each(|line| {
|
||||
let mut splits = line.split('-');
|
||||
let left = splits.next().unwrap().to_string();
|
||||
let right = splits.next().unwrap().to_string();
|
||||
|
||||
let left_id = {
|
||||
*ids.entry(left.clone()).or_insert_with(|| {
|
||||
let id = i;
|
||||
i += 1;
|
||||
names.insert(id, left);
|
||||
id
|
||||
})
|
||||
};
|
||||
let right_id = {
|
||||
*ids.entry(right.clone()).or_insert_with(|| {
|
||||
let id = i;
|
||||
i += 1;
|
||||
names.insert(id, right);
|
||||
id
|
||||
})
|
||||
};
|
||||
|
||||
adj_list[left_id].push(right_id);
|
||||
adj_list[right_id].push(left_id);
|
||||
});
|
||||
|
||||
(i, adj_list, names)
|
||||
}
|
||||
|
||||
#[aoc(day23, part1)]
|
||||
fn part1(input: &Input) -> usize {
|
||||
let num = input.0;
|
||||
let adj_list = &input.1;
|
||||
let names = &input.2;
|
||||
|
||||
(0..num)
|
||||
.filter(|id| names[id].starts_with("t"))
|
||||
.flat_map(|id| {
|
||||
let mut three_groups = HashSet::new();
|
||||
let adj = &adj_list[id];
|
||||
|
||||
for left in 0..adj.len() {
|
||||
for right in left + 1..adj.len() {
|
||||
if adj_list[adj[left]].contains(&adj[right]) {
|
||||
let mut group = [id, adj[left], adj[right]];
|
||||
group.sort();
|
||||
three_groups.insert((group[0], group[1], group[2]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
three_groups
|
||||
})
|
||||
.collect::<HashSet<(usize, usize, usize)>>()
|
||||
.into_iter()
|
||||
.count()
|
||||
}
|
||||
|
||||
#[aoc(day23, part2)]
|
||||
fn part2(input: &Input) -> String {
|
||||
let num = input.0;
|
||||
let adj_list = &input.1;
|
||||
let names = &input.2;
|
||||
|
||||
let max_clique = (0..num)
|
||||
.filter(|id| names[id].starts_with("t"))
|
||||
.flat_map(|id| {
|
||||
let mut three_groups = HashSet::new();
|
||||
let adj = &adj_list[id];
|
||||
|
||||
for left in 0..adj.len() {
|
||||
for right in left + 1..adj.len() {
|
||||
if adj_list[adj[left]].contains(&adj[right]) {
|
||||
let mut group = [id, adj[left], adj[right]];
|
||||
group.sort();
|
||||
three_groups.insert((group[0], group[1], group[2]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
three_groups
|
||||
})
|
||||
.collect::<HashSet<(usize, usize, usize)>>()
|
||||
.into_iter()
|
||||
.flat_map(|(a, b, c)| {
|
||||
let clique = vec![a, b, c];
|
||||
expand_cliques(clique, num, adj_list)
|
||||
})
|
||||
.max_by_key(|clique| clique.len())
|
||||
.unwrap();
|
||||
|
||||
let mut names = max_clique
|
||||
.iter()
|
||||
.map(|id| names[id].clone())
|
||||
.collect::<Vec<String>>();
|
||||
names.sort();
|
||||
|
||||
names.join(",")
|
||||
}
|
||||
|
||||
fn expand_cliques(current: Vec<usize>, num: usize, adj: &[Vec<usize>]) -> Vec<Vec<usize>> {
|
||||
let mut expansions: Vec<Vec<usize>> = vec![current.clone()];
|
||||
|
||||
for node in 0..num {
|
||||
if current.contains(&node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !current
|
||||
.iter()
|
||||
.all(|clique_node| adj[*clique_node].contains(&node))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut pushed = false;
|
||||
for clique in &mut expansions {
|
||||
if clique
|
||||
.iter()
|
||||
.all(|clique_node| adj[*clique_node].contains(&node))
|
||||
{
|
||||
clique.push(node);
|
||||
pushed = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if !pushed {
|
||||
let mut new_clique = current.clone();
|
||||
new_clique.push(node);
|
||||
expansions.push(new_clique);
|
||||
}
|
||||
}
|
||||
|
||||
expansions
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(
|
||||
part1(&parse(
|
||||
"kh-tc
|
||||
qp-kh
|
||||
de-cg
|
||||
ka-co
|
||||
yn-aq
|
||||
qp-ub
|
||||
cg-tb
|
||||
vc-aq
|
||||
tb-ka
|
||||
wh-tc
|
||||
yn-cg
|
||||
kh-ub
|
||||
ta-co
|
||||
de-co
|
||||
tc-td
|
||||
tb-wq
|
||||
wh-td
|
||||
ta-ka
|
||||
td-qp
|
||||
aq-cg
|
||||
wq-ub
|
||||
ub-vc
|
||||
de-ta
|
||||
wq-aq
|
||||
wq-vc
|
||||
wh-yn
|
||||
ka-de
|
||||
kh-ta
|
||||
co-tc
|
||||
wh-qp
|
||||
tb-vc
|
||||
td-yn"
|
||||
)),
|
||||
7
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(
|
||||
part2(&parse(
|
||||
"kh-tc
|
||||
qp-kh
|
||||
de-cg
|
||||
ka-co
|
||||
yn-aq
|
||||
qp-ub
|
||||
cg-tb
|
||||
vc-aq
|
||||
tb-ka
|
||||
wh-tc
|
||||
yn-cg
|
||||
kh-ub
|
||||
ta-co
|
||||
de-co
|
||||
tc-td
|
||||
tb-wq
|
||||
wh-td
|
||||
ta-ka
|
||||
td-qp
|
||||
aq-cg
|
||||
wq-ub
|
||||
ub-vc
|
||||
de-ta
|
||||
wq-aq
|
||||
wq-vc
|
||||
wh-yn
|
||||
ka-de
|
||||
kh-ta
|
||||
co-tc
|
||||
wh-qp
|
||||
tb-vc
|
||||
td-yn"
|
||||
)),
|
||||
"co,de,ka,ta"
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,303 +0,0 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use nom::{
|
||||
bytes::complete::tag,
|
||||
character::complete::{anychar, newline, one_of, space1, u64},
|
||||
multi::{fill, many1, separated_list0},
|
||||
sequence::{delimited, separated_pair},
|
||||
IResult,
|
||||
};
|
||||
|
||||
type Line = [char; 3];
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Operation {
|
||||
And,
|
||||
Or,
|
||||
Xor,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Gate {
|
||||
operation: Operation,
|
||||
in_0: Line,
|
||||
in_1: Line,
|
||||
out: Line,
|
||||
}
|
||||
|
||||
type Input = (Vec<(Line, u64)>, Vec<Gate>);
|
||||
|
||||
#[aoc_generator(day24)]
|
||||
fn parse(input: &str) -> Input {
|
||||
separated_pair(
|
||||
separated_list0(newline, separated_pair(parse_line, tag(": "), u64)),
|
||||
tag("\n\n"),
|
||||
separated_list0(newline, parse_instruction),
|
||||
)(input)
|
||||
.unwrap()
|
||||
.1
|
||||
}
|
||||
|
||||
fn parse_line(input: &str) -> IResult<&str, [char; 3]> {
|
||||
let mut buffer = ['0'; 3];
|
||||
let (input, _) = fill(anychar, &mut buffer)(input)?;
|
||||
Ok((input, buffer))
|
||||
}
|
||||
|
||||
fn parse_instruction(input: &str) -> IResult<&str, Gate> {
|
||||
let (input, in_0) = parse_line(input)?;
|
||||
let (input, operation) = delimited(space1, many1(one_of("ANDXOR")), space1)(input)?;
|
||||
let (input, in_1) = parse_line(input)?;
|
||||
let (input, _) = tag(" -> ")(input)?;
|
||||
let (input, out) = parse_line(input)?;
|
||||
match operation[0] {
|
||||
'A' => Ok((
|
||||
input,
|
||||
Gate {
|
||||
in_0,
|
||||
in_1,
|
||||
out,
|
||||
operation: Operation::And,
|
||||
},
|
||||
)),
|
||||
'O' => Ok((
|
||||
input,
|
||||
Gate {
|
||||
in_0,
|
||||
in_1,
|
||||
out,
|
||||
operation: Operation::Or,
|
||||
},
|
||||
)),
|
||||
'X' => Ok((
|
||||
input,
|
||||
Gate {
|
||||
in_0,
|
||||
in_1,
|
||||
out,
|
||||
operation: Operation::Xor,
|
||||
},
|
||||
)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc(day24, part1)]
|
||||
fn part1(input: &Input) -> usize {
|
||||
let fixed = &input.0;
|
||||
|
||||
let mut values = HashMap::new();
|
||||
|
||||
for (line, value) in fixed {
|
||||
values.insert(line, *value);
|
||||
}
|
||||
|
||||
let mut gates = VecDeque::new();
|
||||
|
||||
for gate in &input.1 {
|
||||
gates.push_back(gate);
|
||||
}
|
||||
|
||||
while let Some(gate) = gates.pop_front() {
|
||||
if values.contains_key(&gate.in_0) && values.contains_key(&gate.in_1) {
|
||||
let in_0 = values[&gate.in_0];
|
||||
let in_1 = values[&gate.in_1];
|
||||
let out = match gate.operation {
|
||||
Operation::And => in_0 & in_1,
|
||||
Operation::Or => in_0 | in_1,
|
||||
Operation::Xor => in_0 ^ in_1,
|
||||
};
|
||||
values.insert(&gate.out, out);
|
||||
} else {
|
||||
gates.push_back(gate);
|
||||
}
|
||||
}
|
||||
|
||||
values
|
||||
.into_iter()
|
||||
.filter(|(line, value)| line[0] == 'z' && *value == 1)
|
||||
.map(|(line, _)| {
|
||||
1 << line[1..3]
|
||||
.iter()
|
||||
.collect::<String>()
|
||||
.parse::<u64>()
|
||||
.unwrap()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[aoc(day24, part2)]
|
||||
fn part2(input: &Input) -> String {
|
||||
let swaps: &[(Line, Line)] = &[
|
||||
(['h', 'm', 't'], ['z', '1', '8']),
|
||||
(['b', 'f', 'q'], ['z', '2', '7']),
|
||||
(['h', 'k', 'h'], ['z', '3', '1']),
|
||||
(['f', 'j', 'p'], ['b', 'n', 'g']),
|
||||
];
|
||||
// let swaps: &[(Line, Line)] = &[];
|
||||
let mut gates = input.1.clone();
|
||||
for gate in &mut gates {
|
||||
for (from, to) in swaps {
|
||||
if gate.out == *from {
|
||||
gate.out = *to;
|
||||
} else if gate.out == *to {
|
||||
gate.out = *from;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for gate in &gates {
|
||||
if gate.out[0] == 'z' && gate.operation != Operation::Xor {
|
||||
println!("gate {:?} outputs to a z but is not xor", gate);
|
||||
}
|
||||
}
|
||||
|
||||
let mut c = ['r', 'j', 'r'];
|
||||
|
||||
for bit in 1..=44 {
|
||||
let x = [
|
||||
'x',
|
||||
(bit as u8 / 10 + 48) as char,
|
||||
(bit as u8 % 10 + 48) as char,
|
||||
];
|
||||
let y = [
|
||||
'y',
|
||||
(bit as u8 / 10 + 48) as char,
|
||||
(bit as u8 % 10 + 48) as char,
|
||||
];
|
||||
|
||||
let xy_gates = find_gate(&gates, &x, &y);
|
||||
|
||||
assert_eq!(
|
||||
xy_gates.len(),
|
||||
2,
|
||||
"there is not exactly two xy gates {:?}\nrelated gates: {:?}",
|
||||
xy_gates,
|
||||
find_debug_gate(&gates, &x, &y)
|
||||
);
|
||||
|
||||
let line_xy_xor = xy_gates
|
||||
.iter()
|
||||
.find(|g| g.operation == Operation::Xor)
|
||||
.unwrap()
|
||||
.out;
|
||||
|
||||
let line_xy_and = xy_gates
|
||||
.iter()
|
||||
.find(|g| g.operation == Operation::And)
|
||||
.unwrap()
|
||||
.out;
|
||||
|
||||
let xyc_gates = find_gate(&gates, &line_xy_xor, &c);
|
||||
|
||||
assert_eq!(xyc_gates.len(), 2,
|
||||
"there is not exactly two output gates {:?}\nrelated gates: {:?}\nxy_and: {}\nxy_xor: {}",
|
||||
xyc_gates,
|
||||
find_debug_gate(&gates, &line_xy_xor, &c),
|
||||
display_line(&line_xy_and),
|
||||
display_line(&line_xy_xor),
|
||||
);
|
||||
|
||||
let line_xyc_xor = xyc_gates
|
||||
.iter()
|
||||
.find(|g| g.operation == Operation::Xor)
|
||||
.unwrap()
|
||||
.out;
|
||||
|
||||
let line_xyc_and = xyc_gates
|
||||
.iter()
|
||||
.find(|g| g.operation == Operation::And)
|
||||
.unwrap()
|
||||
.out;
|
||||
|
||||
let xyco_gates = find_gate(&gates, &line_xyc_and, &line_xy_and);
|
||||
|
||||
assert_eq!(
|
||||
xyco_gates.len(),
|
||||
1,
|
||||
"there is no or more than one output gate {:?}\nrelated gates: {:?}\nxy_and: {}\nxy_xor: {}\nxyc_and: {}\nxyc_xor: {}",
|
||||
xyco_gates,
|
||||
find_debug_gate(&gates, &line_xyc_and, &line_xy_and),
|
||||
display_line(&line_xy_and),
|
||||
display_line(&line_xy_xor),
|
||||
display_line(&line_xyc_and),
|
||||
display_line(&line_xyc_xor),
|
||||
);
|
||||
|
||||
c = xyco_gates[0].out;
|
||||
|
||||
println!(
|
||||
"for bit {} z-output is {} and carry out is {}",
|
||||
bit,
|
||||
display_line(&line_xyc_xor),
|
||||
display_line(&c)
|
||||
);
|
||||
}
|
||||
|
||||
let mut wires = swaps
|
||||
.iter()
|
||||
.flat_map(|(a, b)| [display_line(a), display_line(b)])
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
wires.sort();
|
||||
|
||||
wires.join(",")
|
||||
}
|
||||
|
||||
fn display_line(line: &Line) -> String {
|
||||
line.iter().collect()
|
||||
}
|
||||
|
||||
fn find_gate(gates: &[Gate], in_0: &Line, in_1: &Line) -> Vec<Gate> {
|
||||
gates
|
||||
.iter()
|
||||
.filter(|gate| {
|
||||
(gate.in_0 == *in_0 && gate.in_1 == *in_1) || (gate.in_0 == *in_1 && gate.in_1 == *in_0)
|
||||
})
|
||||
.copied()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn find_debug_gate(gates: &[Gate], in_0: &Line, in_1: &Line) -> Vec<Gate> {
|
||||
gates
|
||||
.iter()
|
||||
.filter(|gate| {
|
||||
gate.in_0 == *in_0 || gate.in_1 == *in_1 || gate.in_0 == *in_1 || gate.in_1 == *in_0
|
||||
})
|
||||
.copied()
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(
|
||||
part1(&parse(
|
||||
"x00: 1
|
||||
x01: 1
|
||||
x02: 1
|
||||
y00: 0
|
||||
y01: 1
|
||||
y02: 0
|
||||
|
||||
x00 AND y00 -> z00
|
||||
x01 XOR y01 -> z01
|
||||
x02 OR y02 -> z02"
|
||||
)),
|
||||
4
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(
|
||||
part2(&parse(include_str!("../input/2024/day24.txt").trim_end())),
|
||||
"z00,z01,z02,z05"
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
mod day24;
|
||||
mod day23;
|
||||
mod day22;
|
||||
mod day21;
|
||||
mod day1;
|
||||
|
|
Loading…
Reference in New Issue