mirror of
https://github.com/peter-tanner/advent-of-code-2022.git
synced 2024-11-30 14:20:22 +08:00
day 14 (python, lazy)
starting to learn that recursive data structures are hard in rust...
This commit is contained in:
parent
357dc93473
commit
eab082f18b
86
day_13/src/main.py
Normal file
86
day_13/src/main.py
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
from enum import Enum
|
||||||
|
import functools
|
||||||
|
import json
|
||||||
|
import pprint
|
||||||
|
|
||||||
|
class ORDER(Enum):
|
||||||
|
CORRECT=1,
|
||||||
|
SAME=0,
|
||||||
|
INCORRECT=-1,
|
||||||
|
|
||||||
|
def cmp(a: list, b: list) -> ORDER:
|
||||||
|
for i in range(0, min(len(a),len(b))):
|
||||||
|
left = a[i]
|
||||||
|
right = b[i]
|
||||||
|
|
||||||
|
if type(left) == int and type(right) == int:
|
||||||
|
if left < right:
|
||||||
|
return ORDER.CORRECT
|
||||||
|
elif left > right:
|
||||||
|
return ORDER.INCORRECT
|
||||||
|
|
||||||
|
elif type(left) == int and type(right) == list:
|
||||||
|
left = [a[i]]
|
||||||
|
|
||||||
|
elif type(left) == list and type(right) == int:
|
||||||
|
right = [b[i]]
|
||||||
|
|
||||||
|
|
||||||
|
if type(left) == list and type(right) == list:
|
||||||
|
res = cmp(left,right)
|
||||||
|
if res != ORDER.SAME:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
if len(a) < len(b):
|
||||||
|
return ORDER.CORRECT
|
||||||
|
elif len(a) > len(b):
|
||||||
|
return ORDER.INCORRECT
|
||||||
|
|
||||||
|
return ORDER.SAME
|
||||||
|
|
||||||
|
def cmp_int(a: list, b: list) -> int:
|
||||||
|
return cmp(a,b).value[0]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test()
|
||||||
|
correct_sum = 0
|
||||||
|
packets = [
|
||||||
|
[[2]],
|
||||||
|
[[6]]
|
||||||
|
]
|
||||||
|
with open("src/input","r") as f:
|
||||||
|
pair = 1
|
||||||
|
while True:
|
||||||
|
a_str = f.readline()
|
||||||
|
if not a_str or a_str == '':
|
||||||
|
break
|
||||||
|
a = json.loads(a_str)
|
||||||
|
b = json.loads(f.readline())
|
||||||
|
if cmp(a,b) == ORDER.CORRECT:
|
||||||
|
correct_sum += pair
|
||||||
|
f.readline()
|
||||||
|
packets.append(a)
|
||||||
|
packets.append(b)
|
||||||
|
|
||||||
|
pair += 1
|
||||||
|
f.close()
|
||||||
|
print(f"PART 1 {correct_sum}")
|
||||||
|
|
||||||
|
packets.sort(key=functools.cmp_to_key(cmp_int), reverse=True)
|
||||||
|
# pprint.pp(packets)
|
||||||
|
print(f"PART 2 {(packets.index([[2]]) + 1) * (packets.index([[6]]) + 1)}")
|
||||||
|
|
||||||
|
def test():
|
||||||
|
assert ORDER.CORRECT == cmp([1,1,3,1,1],[1,1,5,1,1])
|
||||||
|
assert ORDER.CORRECT == cmp([[1],[2,3,4]],[[1],4])
|
||||||
|
assert ORDER.INCORRECT == cmp([9],[[8,7,6]])
|
||||||
|
assert ORDER.CORRECT == cmp([[4,4],4,4],[[4,4],4,4,4])
|
||||||
|
assert ORDER.INCORRECT == cmp([7,7,7,7],[7,7,7])
|
||||||
|
assert ORDER.CORRECT == cmp([],[3])
|
||||||
|
assert ORDER.INCORRECT == cmp([[[]]],[[]])
|
||||||
|
assert ORDER.INCORRECT == cmp([1,[2,[3,[4,[5,6,7]]]],8,9],[1,[2,[3,[4,[5,6,0]]]],8,9])
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
main()
|
|
@ -1,3 +1,35 @@
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
data: Option<u32>,
|
||||||
|
children: Vec<Node>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
fn new() -> Self {
|
||||||
|
Node {
|
||||||
|
data: None,
|
||||||
|
children: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_node(mut self) -> Box<Node> {
|
||||||
|
let test = Box::from(Node::new());
|
||||||
|
self.children.push(*test.deref());
|
||||||
|
return test;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let list_s = "[1,[2,[3,[4,[5,6,7]]]],8,9]";
|
||||||
|
|
||||||
|
let mut splitted = list_s.split(',').into_iter();
|
||||||
|
|
||||||
|
let root = Node::new();
|
||||||
|
|
||||||
|
while let token = splitted.next().unwrap() {
|
||||||
|
if token.contains('[') {}
|
||||||
|
}
|
||||||
|
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user