Day 15 time
continuous-integration/drone/push Build is passing Details

main
Julius 2021-12-16 12:49:57 +01:00
parent a3961cbff0
commit 0c9603fb3e
Signed by: j00lz
GPG Key ID: AF241B0AA237BBA2
4 changed files with 306 additions and 15 deletions

1
input/day16 Normal file
View File

@ -0,0 +1 @@
E20D7880532D4E551A5791BD7B8C964C1548CB3EC1FCA41CC00C6D50024400C202A65C00C20257C008AF70024C00810039C00C3002D400A300258040F200D6040093002CC0084003FA52DB8134DE620EC01DECC4C8A5B55E204B6610189F87BDD3B30052C01493E2DC9F1724B3C1F8DC801E249E8D66C564715589BCCF08B23CA1A00039D35FD6AC5727801500260B8801F253D467BFF99C40182004223B4458D2600E42C82D07CC01D83F0521C180273D5C8EE802B29F7C9DA1DCACD1D802469FF57558D6A65372113005E4DB25CF8C0209B329D0D996C92605009A637D299AEF06622CE4F1D7560141A52BC6D91C73CD732153BF862F39BA49E6BA8C438C010E009AA6B75EF7EE53BBAC244933A48600B025AD7C074FEB901599A49808008398142013426BD06FA00D540010C87F0CA29880370E21D42294A6E3BCF0A080324A006824E3FCBE4A782E7F356A5006A587A56D3699CF2F4FD6DF60862600BF802F25B4E96BDD26049802333EB7DDB401795FC36BD26A860094E176006A0200FC4B8790B4001098A50A61748D2DEDDF4C6200F4B6FE1F1665BED44015ACC055802B23BD87C8EF61E600B4D6BAD5800AA4E5C8672E4E401D0CC89F802D298F6A317894C7B518BE4772013C2803710004261EC318B800084C7288509E56FD6430052482340128FB37286F9194EE3D31FA43BACAF2802B12A7B83E4017E4E755E801A2942A9FCE757093005A6D1F803561007A17C3B8EE0008442085D1E8C0109E3BC00CDE4BFED737A90DC97FDAE6F521B97B4619BE17CC01D94489E1C9623000F924A7C8C77EA61E6679F7398159DE7D84C015A0040670765D5A52D060200C92801CA8A531194E98DA3CCF8C8C017C00416703665A2141008CF34EF8019A080390962841C1007217C5587E60164F81C9A5CE0E4AA549223002E32BDCEA36B2E100A160008747D8B705C001098DB13A388803F1AE304600

View File

@ -106,7 +106,7 @@ pub fn dijkstra(
}
}
ans.get(end).unwrap().clone()
*ans.get(end).unwrap()
}
fn add_edge(graph: &mut Graph<Coordinate, u32>, source: Coordinate, target: Coordinate, value: u32) {
@ -157,17 +157,17 @@ mod tests {
}
// Woo slow and i again don't feel like fixing it :p
#[test]
fn part1_real() -> anyhow::Result<()> {
let d = Day15::init(crate::load_input("15")?)?;
assert_eq!("388", d.part1()?);
Ok(())
}
#[test]
fn part2_real() -> anyhow::Result<()> {
let d = Day15::init(crate::load_input("15")?)?;
assert_eq!("2819", d.part2()?);
Ok(())
}
// #[test]
// fn part1_real() -> anyhow::Result<()> {
// let d = Day15::init(crate::load_input("15")?)?;
// assert_eq!("388", d.part1()?);
// Ok(())
// }
//
// #[test]
// fn part2_real() -> anyhow::Result<()> {
// let d = Day15::init(crate::load_input("15")?)?;
// assert_eq!("2819", d.part2()?);
// Ok(())
// }
}

287
src/day16.rs Normal file
View File

@ -0,0 +1,287 @@
use crate::Day;
pub struct Day16(Packet);
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
pub struct Packet {
version: u32,
type_id: PacketType,
sub_packets: Vec<Packet>,
value: u64,
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
pub enum LengthType {
Bits,
Packets,
}
impl From<u8> for LengthType {
fn from(a: u8) -> Self {
use LengthType::{Bits, Packets};
match a {
0 => Bits,
_ => Packets
}
}
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Debug)]
enum PacketType {
Sum,
Product,
Minimum,
Maximum,
Literal,
Greater,
Less,
Eq,
}
impl From<u8> for PacketType {
fn from(n: u8) -> Self {
use PacketType::*;
match n {
0 => Sum,
1 => Product,
2 => Minimum,
3 => Maximum,
4 => Literal,
5 => Greater,
6 => Less,
7 => Eq,
_ => Literal
}
}
}
fn hex_to_bytes(hex: char) -> [u8; 4] {
match hex {
'0' => [0, 0, 0, 0],
'1' => [0, 0, 0, 1],
'2' => [0, 0, 1, 0],
'3' => [0, 0, 1, 1],
'4' => [0, 1, 0, 0],
'5' => [0, 1, 0, 1],
'6' => [0, 1, 1, 0],
'7' => [0, 1, 1, 1],
'8' => [1, 0, 0, 0],
'9' => [1, 0, 0, 1],
'A' => [1, 0, 1, 0],
'B' => [1, 0, 1, 1],
'C' => [1, 1, 0, 0],
'D' => [1, 1, 0, 1],
'E' => [1, 1, 1, 0],
'F' => [1, 1, 1, 1],
_ => [0, 0, 0, 0],
}
}
fn parse_str<T: AsRef<str>>(content: T) -> Vec<u8> {
content.as_ref().chars().map(hex_to_bytes).flatten().collect::<Vec<_>>()
}
impl Day for Day16 {
fn init(content: String) -> anyhow::Result<Self> {
let txt = parse_str(content);
let mut iter = txt.iter().copied();
Ok(Self(parse_packet(&mut iter).ok_or_else(|| anyhow::Error::msg("Could not parse packet!"))?))
}
fn part1(&self) -> anyhow::Result<String> {
let p = &self.0;
fn calc(packet: &Packet) -> u32 {
(packet.version) + if packet.type_id == PacketType::Literal {
0
} else {
packet.sub_packets.iter().map(calc).sum()
}
}
Ok(format!("{}", calc(p)))
}
fn part2(&self) -> anyhow::Result<String> {
let p = &self.0;
fn calc(packet: &Packet) -> u64 {
match packet.type_id {
PacketType::Sum => packet.sub_packets.iter().map(calc).sum(),
PacketType::Product => packet.sub_packets.iter().map(calc).product(),
PacketType::Minimum => packet.sub_packets.iter().map(calc).min().unwrap(),
PacketType::Maximum => packet.sub_packets.iter().map(calc).max().unwrap(),
PacketType::Literal => packet.value,
PacketType::Greater => {
let z = packet.sub_packets.iter().map(calc).collect::<Vec<_>>();
if z[0] > z[1] {
1
} else {
0
}
}
PacketType::Less => {
let z = packet.sub_packets.iter().map(calc).collect::<Vec<_>>();
if z[0] < z[1] {
1
} else {
0
}
}
PacketType::Eq => {
let z = packet.sub_packets.iter().map(calc).collect::<Vec<_>>();
if z[0] == z[1] {
1
} else {
0
}
}
}
}
Ok(format!("{}", calc(p)))
}
}
fn parse_packet<T: Iterator<Item=u8>>(it: &mut T) -> Option<Packet> {
let version = it.take(3).enumerate().fold(0, |a, (idx, num)| a | ((num as u32) << (2 - idx)));
let type_id = it.take(3).enumerate().fold(0, |a, (idx, num)| a | (num << (2 - idx))).into();
Some(Packet {
version,
type_id,
sub_packets: if type_id != PacketType::Literal { decode_other(it)? } else { Vec::new() },
value: if type_id == PacketType::Literal { decode_literal(it)? } else { 0 },
})
}
fn decode_other<T: Iterator<Item=u8>>(it: &mut T) -> Option<Vec<Packet>> {
let t: LengthType = it.next()?.into();
return match t {
LengthType::Bits => {
let l = it.take(15).fold(0, |a, b| (a << 1) | (b as usize));
let to_parse = it.take(l).collect::<Vec<_>>();
let mut new_iter = to_parse.iter().copied().peekable();
let mut a = Vec::new();
while new_iter.peek().is_some() {
a.push(parse_packet(&mut new_iter)?)
}
Some(a)
}
LengthType::Packets => {
let l = it.take(11).fold(0, |a, b| (a << 1) | (b as usize));
let mut packets = Vec::new();
for _ in 0..l {
let p = parse_packet(it)?;
packets.push(p);
}
Some(packets)
}
};
}
fn decode_literal<T: Iterator<Item=u8>>(it: &mut T) -> Option<u64> {
let mut z = it.take(5).collect::<Vec<_>>();
let mut n = 0;
loop {
for a in 0..4 {
n <<= 1;
n += z[a + 1] as u64
}
if z[0] == 0 {
break;
} else {
z = it.take(5).collect::<Vec<_>>();
}
}
Some(n)
}
#[cfg(test)]
mod tests {
use crate::day16::{Day16, PacketType, parse_packet, parse_str};
use crate::day::Day;
#[test]
fn test_literal() {
let inp = parse_str("D2FE28");
let mut i = inp.iter().copied();
if let Some(r) = parse_packet(&mut i) {
if r.type_id == PacketType::Literal {
assert_eq!(r.value, 2021);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
#[test]
fn test_packet_bits() {
let inp = parse_str("38006F45291200");
let mut i = inp.iter().copied();
if let Some(r) = parse_packet(&mut i) {
if r.type_id != PacketType::Literal {
let p = &r.sub_packets[0];
assert_eq!(p.value, 10);
let p = &r.sub_packets[1];
assert_eq!(p.value, 20);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
#[test]
fn test_packet_packets() {
let inp = parse_str("EE00D40C823060");
let mut i = inp.iter().copied();
if let Some(r) = parse_packet(&mut i) {
if r.type_id != PacketType::Literal {
let p = &r.sub_packets[0];
assert_eq!(p.value, 1);
let p = &r.sub_packets[1];
assert_eq!(p.value, 2);
let p = &r.sub_packets[2];
assert_eq!(p.value, 3);
} else {
assert!(false);
}
} else {
assert!(false);
}
}
#[test]
fn part1_tests() -> anyhow::Result<()> {
let d = Day16::init("8A004A801A8002F478".to_string())?;
assert_eq!(d.part1()?, "16");
let d = Day16::init("620080001611562C8802118E34".to_string())?;
assert_eq!(d.part1()?, "12");
let d = Day16::init("C0015000016115A2E0802F182340".to_string())?;
assert_eq!(d.part1()?, "23");
let d = Day16::init("A0016C880162017C3686B18A3D4780".to_string())?;
assert_eq!(d.part1()?, "31");
Ok(())
}
#[test]
fn part2_tests() -> anyhow::Result<()>{
fn calc(s: &str) -> anyhow::Result<String>{
let d = Day16::init(s.to_string())?;
d.part2()
}
assert_eq!("3", calc("C200B40A82")?);
assert_eq!("54", calc("04005AC33890")?);
assert_eq!("7", calc("880086C3E88112")?);
assert_eq!("9", calc("CE00C43D881120")?);
assert_eq!("1", calc("D8005AC2A8F0")?);
assert_eq!("0", calc("F600BC2D8F")?);
assert_eq!("0", calc("9C005AC2F8F0")?);
assert_eq!("1", calc("9C0141080250320F1802104A08")?);
Ok(())
}
}

View File

@ -18,6 +18,7 @@ use crate::day12::Day12;
use crate::day13::Day13;
use crate::day14::Day14;
use crate::day15::Day15;
use crate::day16::Day16;
mod day;
mod day01;
@ -35,6 +36,7 @@ mod day12;
mod day13;
mod day14;
mod day15;
mod day16;
fn load_input(day: &str) -> Result<String> {
read_to_string(format!("./input/day{}", day)).map_err(|x| x.into())
@ -84,7 +86,8 @@ fn main() -> anyhow::Result<()> {
Box::new(Day12::init(load_input("12")?)?),
Box::new(Day13::init(load_input("13")?)?),
Box::new(Day14::init(load_input("14")?)?),
Box::new(Day15::init(load_input("15")?)?),];
Box::new(Day15::init(load_input("15")?)?),
Box::new(Day16::init(load_input("16")?)?),];
let _verbosity = matches.occurrences_of("v");
if matches.is_present("all") {