Compare commits
No commits in common. "1a8565b67c8558f175d937c23a00cdfb23e6185a" and "dcc3c287bb8cd463720e4bc401662217a45b6ca1" have entirely different histories.
1a8565b67c
...
dcc3c287bb
File diff suppressed because it is too large
Load Diff
|
@ -1,42 +0,0 @@
|
||||||
;; Day 2 - Wrapping paper
|
|
||||||
;;
|
|
||||||
;; Part 1: How many totral square feet of wrapping paper should the elves order?
|
|
||||||
;;
|
|
||||||
;; ref. https://asdf.common-lisp.dev/uiop.html#index-split_002dstring
|
|
||||||
;;
|
|
||||||
(defun dimension (str)
|
|
||||||
"split the 'lxwxh' encoding of present dimensions into a list of integers"
|
|
||||||
(mapcar (lambda (str-number) (parse-integer str-number))
|
|
||||||
(uiop:split-string str :separator "x")))
|
|
||||||
|
|
||||||
(defun side-areas (l w h)
|
|
||||||
"areas of the three kinds of side of a cuboid"
|
|
||||||
(list (* l w) (* w h) (* h l)))
|
|
||||||
|
|
||||||
(defun paper-area (l w h)
|
|
||||||
"area of the whole present wrapping paper: the area of the lxwxh cuboid plus the smaller paper square"
|
|
||||||
(let ((areas (side-areas l w h)))
|
|
||||||
(let ((min-area (apply #'min areas)))
|
|
||||||
(let ((present-area
|
|
||||||
(reduce '+ (mapcar (lambda (area) (* 2 area)) areas))))
|
|
||||||
(+ present-area min-area)))))
|
|
||||||
|
|
||||||
(defun paper-area-dimension (dimension)
|
|
||||||
(paper-area (nth 0 dimension) (nth 1 dimension) (nth 2 dimension)))
|
|
||||||
|
|
||||||
#|
|
|
||||||
(paper-area 2 3 4) ;; 58
|
|
||||||
(paper-area 1 1 10) ;; 43
|
|
||||||
|#
|
|
||||||
|
|
||||||
(defparameter area-sum 0)
|
|
||||||
|
|
||||||
(with-open-file (in "./data/input")
|
|
||||||
(do ((line (read-line in nil)
|
|
||||||
(read-line in nil)))
|
|
||||||
((null line))
|
|
||||||
(setf area-sum (+ area-sum
|
|
||||||
(paper-area-dimension
|
|
||||||
(dimension line))))))
|
|
||||||
|
|
||||||
(print area-sum)
|
|
|
@ -1,51 +0,0 @@
|
||||||
;; Day 3: Perfectly Spherical Houses in a Vacuum
|
|
||||||
;;
|
|
||||||
;; Part 1: How many houses recieve at least one present?
|
|
||||||
|
|
||||||
|
|
||||||
(defun move (char)
|
|
||||||
(cond
|
|
||||||
((equal char #\>)
|
|
||||||
(list 1 0))
|
|
||||||
((equal char #\^)
|
|
||||||
(list 0 -1))
|
|
||||||
((equal char #\<)
|
|
||||||
(list -1 0))
|
|
||||||
((equal char #\v)
|
|
||||||
(list 0 1))
|
|
||||||
(t nil)
|
|
||||||
))
|
|
||||||
|
|
||||||
(defparameter *houses* (make-hash-table :test 'equal))
|
|
||||||
|
|
||||||
(defparameter visited-houses-counter 0)
|
|
||||||
|
|
||||||
(defun visit (house)
|
|
||||||
(if (gethash house *houses*)
|
|
||||||
"key exist"
|
|
||||||
(progn
|
|
||||||
(setf visited-houses-counter (+ visited-houses-counter 1))
|
|
||||||
(setf (gethash house *houses*) 1))))
|
|
||||||
|
|
||||||
|
|
||||||
(defparameter x 0)
|
|
||||||
(defparameter y 0)
|
|
||||||
|
|
||||||
(visit (list x y))
|
|
||||||
|
|
||||||
(defvar walk)
|
|
||||||
|
|
||||||
(defun make-move (char)
|
|
||||||
(progn
|
|
||||||
(setq walk (move char))
|
|
||||||
(setf x (+ x (nth 0 walk)))
|
|
||||||
(setf y (+ y (nth 1 walk)))
|
|
||||||
(visit (list x y))))
|
|
||||||
|
|
||||||
(with-open-file (in "./data/input")
|
|
||||||
(do ((c (read-char in)
|
|
||||||
(read-char in nil 'the-end)))
|
|
||||||
((not (characterp c)))
|
|
||||||
(make-move c)))
|
|
||||||
|
|
||||||
(print visited-houses-counter)
|
|
|
@ -1,34 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
count = 0
|
|
||||||
x = 0
|
|
||||||
y = 0
|
|
||||||
visited = {}
|
|
||||||
|
|
||||||
MOTION = {
|
|
||||||
"^": [0, -1],
|
|
||||||
"<": [-1, 0],
|
|
||||||
"v": [0, 1],
|
|
||||||
">": [1, 0]
|
|
||||||
}
|
|
||||||
|
|
||||||
def move(char):
|
|
||||||
global x
|
|
||||||
global y
|
|
||||||
motion = MOTION[char]
|
|
||||||
x += motion[0]
|
|
||||||
y += motion[1]
|
|
||||||
visit(x, y)
|
|
||||||
|
|
||||||
def visit(x, y):
|
|
||||||
global count
|
|
||||||
if (x, y) not in visited:
|
|
||||||
visited[(x, y)] = 1
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
visit(x, y)
|
|
||||||
with open("./data/input", "r") as f:
|
|
||||||
for line in f:
|
|
||||||
for char in line:
|
|
||||||
move(char)
|
|
||||||
print(count)
|
|
|
@ -1,62 +0,0 @@
|
||||||
;; Day 3: Perfectly Spherical Houses in a Vacuum
|
|
||||||
;;
|
|
||||||
;; Part 1: How many houses recieve at least one present?
|
|
||||||
|
|
||||||
|
|
||||||
(defun move (char)
|
|
||||||
(cond
|
|
||||||
((equal char #\>)
|
|
||||||
(list 1 0))
|
|
||||||
((equal char #\^)
|
|
||||||
(list 0 -1))
|
|
||||||
((equal char #\<)
|
|
||||||
(list -1 0))
|
|
||||||
((equal char #\v)
|
|
||||||
(list 0 1))
|
|
||||||
(t nil)
|
|
||||||
))
|
|
||||||
|
|
||||||
(defparameter *houses* (make-hash-table :test 'equal))
|
|
||||||
|
|
||||||
(defparameter visited-houses-counter 0)
|
|
||||||
|
|
||||||
(defun visit (house)
|
|
||||||
(if (gethash house *houses*)
|
|
||||||
"key exist"
|
|
||||||
(progn
|
|
||||||
(setf visited-houses-counter (+ visited-houses-counter 1))
|
|
||||||
(setf (gethash house *houses*) 1))))
|
|
||||||
|
|
||||||
|
|
||||||
(defparameter xsanta 0)
|
|
||||||
(defparameter ysanta 0)
|
|
||||||
(defparameter xrobot 0)
|
|
||||||
(defparameter yrobot 0)
|
|
||||||
|
|
||||||
(visit (list 0 0))
|
|
||||||
|
|
||||||
(defparameter i 0)
|
|
||||||
|
|
||||||
(defvar walk)
|
|
||||||
|
|
||||||
(defun make-move (char)
|
|
||||||
(setq walk (move char))
|
|
||||||
(setq i (+ i 1))
|
|
||||||
(if (= 0 (mod i 2))
|
|
||||||
(progn
|
|
||||||
(setf xrobot (+ xrobot (nth 0 walk)))
|
|
||||||
(setf yrobot (+ yrobot (nth 1 walk)))
|
|
||||||
(visit (list xrobot yrobot)))
|
|
||||||
(progn
|
|
||||||
(setf xsanta (+ xsanta (nth 0 walk)))
|
|
||||||
(setf ysanta (+ ysanta (nth 1 walk)))
|
|
||||||
(visit (list xsanta ysanta)))))
|
|
||||||
|
|
||||||
(with-open-file (in "./data/input")
|
|
||||||
(do ((c (read-char in)
|
|
||||||
(read-char in nil 'the-end)))
|
|
||||||
((not (characterp c)))
|
|
||||||
(if (not (equal c #\Newline))
|
|
||||||
(make-move c))))
|
|
||||||
|
|
||||||
(print visited-houses-counter)
|
|
|
@ -1,13 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
key = "yzbqklnj"
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
while True:
|
|
||||||
result = hashlib.md5(f'{key}{i}'.encode())
|
|
||||||
if result.hexdigest().startswith("0"*6):
|
|
||||||
print(i)
|
|
||||||
break
|
|
||||||
i += 1
|
|
|
@ -1,51 +0,0 @@
|
||||||
(defvar c)
|
|
||||||
(defvar counter)
|
|
||||||
(defun threevowels? (string)
|
|
||||||
(setq counter 0)
|
|
||||||
(loop for i from 0 to (- (length string) 1)
|
|
||||||
do (setq c (char string i))
|
|
||||||
(setq counter (+ counter (if (or
|
|
||||||
(equal c #\a)
|
|
||||||
(equal c #\e)
|
|
||||||
(equal c #\i)
|
|
||||||
(equal c #\o)
|
|
||||||
(equal c #\u))
|
|
||||||
1
|
|
||||||
0)))
|
|
||||||
(if (>= counter 3)
|
|
||||||
(return-from threevowels? t)))
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defvar c1)
|
|
||||||
(defvar c2)
|
|
||||||
(defun twice? (string)
|
|
||||||
(loop for i from 0 to (- (length string) 2)
|
|
||||||
do (setq c1 (char string i))
|
|
||||||
(setq c2 (char string (+ i 1)))
|
|
||||||
(if (equal c1 c2)
|
|
||||||
(return-from twice? t))
|
|
||||||
)
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defun redflag? (string)
|
|
||||||
(or (search "ab" string)
|
|
||||||
(search "cd" string)
|
|
||||||
(search "pq" string)
|
|
||||||
(search "xy" string)))
|
|
||||||
|
|
||||||
(defun nice? (string)
|
|
||||||
(and (threevowels? string)
|
|
||||||
(twice? string)
|
|
||||||
(not (redflag? string))))
|
|
||||||
|
|
||||||
(defvar my-count 0)
|
|
||||||
|
|
||||||
(with-open-file (in "./data/input")
|
|
||||||
(do ((line (read-line in nil)
|
|
||||||
(read-line in nil)))
|
|
||||||
((null line))
|
|
||||||
(if (nice? line)
|
|
||||||
(setq my-count (+ my-count 1)))
|
|
||||||
))
|
|
||||||
|
|
||||||
(print my-count)
|
|
|
@ -1,46 +0,0 @@
|
||||||
(defun twice? (string pattern)
|
|
||||||
(numberp (search pattern string
|
|
||||||
:start2 (+
|
|
||||||
(search pattern string)
|
|
||||||
(length pattern)))))
|
|
||||||
|
|
||||||
|
|
||||||
(defvar pattern)
|
|
||||||
|
|
||||||
(defun overtwice? (string)
|
|
||||||
(loop for i from 0 to (- (length string) 2)
|
|
||||||
do
|
|
||||||
(setq pattern (subseq string i (+ i 2)))
|
|
||||||
(if (twice? string pattern)
|
|
||||||
(return-from overtwice? t))
|
|
||||||
)
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defvar c1)
|
|
||||||
(defvar c2)
|
|
||||||
(defvar c3)
|
|
||||||
(defun tandem? (string)
|
|
||||||
(loop for i from 0 to (- (length string) 3)
|
|
||||||
do
|
|
||||||
(setq c1 (char string i))
|
|
||||||
(setq c2 (char string (+ i 1)))
|
|
||||||
(setq c3 (char string (+ i 2)))
|
|
||||||
(if (and (equal c1 c3) (not (equal c1 c2)))
|
|
||||||
(return-from tandem? t)))
|
|
||||||
nil)
|
|
||||||
|
|
||||||
(defun nice? (string)
|
|
||||||
(and (overtwice? string)
|
|
||||||
(tandem? string)))
|
|
||||||
|
|
||||||
(defvar my-count 0)
|
|
||||||
|
|
||||||
(with-open-file (in "./data/input")
|
|
||||||
(do ((line (read-line in nil)
|
|
||||||
(read-line in nil)))
|
|
||||||
((null line))
|
|
||||||
(if (nice? line)
|
|
||||||
(setq my-count (+ my-count 1)))
|
|
||||||
))
|
|
||||||
|
|
||||||
(print my-count)
|
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "part1"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
|
@ -1,92 +0,0 @@
|
||||||
use core::panic;
|
|
||||||
use std::io;
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
type Formula = String;
|
|
||||||
type Variable = String;
|
|
||||||
|
|
||||||
fn emulate(circuit: &Vec<(Formula, Variable)>, variables: &mut BTreeMap<Variable, u16>, target: &Variable) {
|
|
||||||
while !variables.contains_key(target) {
|
|
||||||
let mut update = true;
|
|
||||||
for command in circuit {
|
|
||||||
let variable = &command.1;
|
|
||||||
let formula = &command.0;
|
|
||||||
if variables.contains_key(variable) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let number = formula.parse::<u16>();
|
|
||||||
if number.is_ok() {
|
|
||||||
variables.insert(variable.clone(), number.unwrap());
|
|
||||||
update = true;
|
|
||||||
} else if formula.starts_with("NOT") {
|
|
||||||
let mut parts = formula.split(" ");
|
|
||||||
parts.next();
|
|
||||||
let negated = parts.next().unwrap().to_string();
|
|
||||||
if variables.contains_key(&negated) {
|
|
||||||
let value = !(*variables.get(&negated).unwrap());
|
|
||||||
variables.insert(variable.clone(), value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
} else if !formula.contains(" "){
|
|
||||||
if variables.contains_key(formula) {
|
|
||||||
let value = *variables.get(formula).unwrap();
|
|
||||||
variables.insert(variable.clone(),value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut parts = formula.split(" ");
|
|
||||||
let operand_a = parts.next().unwrap();
|
|
||||||
let operator = parts.next().unwrap();
|
|
||||||
let operand_b = parts.next().unwrap();
|
|
||||||
let operand_a_u16_result = operand_a.parse::<u16>();
|
|
||||||
let operand_b_u16_result = operand_b.parse::<u16>();
|
|
||||||
if (!operand_a_u16_result.is_ok() && !variables.contains_key(operand_a))
|
|
||||||
|| (!operand_b_u16_result.is_ok() && !variables.contains_key(operand_b)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let operand_a_u16: u16;
|
|
||||||
let operand_b_u16: u16;
|
|
||||||
if operand_a_u16_result.is_ok() {
|
|
||||||
operand_a_u16 = operand_a_u16_result.unwrap();
|
|
||||||
} else {
|
|
||||||
operand_a_u16 = *variables.get(operand_a).unwrap();
|
|
||||||
}
|
|
||||||
if operand_b_u16_result.is_ok() {
|
|
||||||
operand_b_u16 = operand_b_u16_result.unwrap();
|
|
||||||
} else {
|
|
||||||
operand_b_u16 = *variables.get(operand_b).unwrap();
|
|
||||||
}
|
|
||||||
let value = match operator {
|
|
||||||
"AND" => operand_a_u16 & operand_b_u16,
|
|
||||||
"OR" => operand_a_u16 | operand_b_u16,
|
|
||||||
"LSHIFT" => operand_a_u16 << operand_b_u16,
|
|
||||||
"RSHIFT" => operand_a_u16 >> operand_b_u16,
|
|
||||||
&_ => 0,
|
|
||||||
};
|
|
||||||
variables.insert(variable.clone(), value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if !update {
|
|
||||||
panic!("I cannot progress in the emulation, target cannot be reached");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
|
||||||
let mut variables: BTreeMap<Variable, u16> = BTreeMap::new();
|
|
||||||
let target: Variable = "a".to_string();
|
|
||||||
let mut circuit: Vec<(Formula, Variable)> = Vec::new();
|
|
||||||
for line in io::stdin().lines() {
|
|
||||||
let line = line.unwrap();
|
|
||||||
let mut parts = line.split(" -> ");
|
|
||||||
let formula: Formula = parts.next().unwrap().to_string();
|
|
||||||
let variable: Variable = parts.next().unwrap().to_string();
|
|
||||||
circuit.push((formula, variable));
|
|
||||||
}
|
|
||||||
emulate(&circuit, &mut variables, &target);
|
|
||||||
println!("{}", variables.get(&target).unwrap());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "part2"
|
|
||||||
version = "0.1.0"
|
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "part2"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
|
@ -1,97 +0,0 @@
|
||||||
use core::panic;
|
|
||||||
use std::io;
|
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
type Formula = String;
|
|
||||||
type Variable = String;
|
|
||||||
|
|
||||||
fn emulate(circuit: &Vec<(Formula, Variable)>, variables: &mut BTreeMap<Variable, u16>, target: &Variable) {
|
|
||||||
while !variables.contains_key(target) {
|
|
||||||
let mut update = true;
|
|
||||||
for command in circuit {
|
|
||||||
let variable = &command.1;
|
|
||||||
let formula = &command.0;
|
|
||||||
if variables.contains_key(variable) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let number = formula.parse::<u16>();
|
|
||||||
if number.is_ok() {
|
|
||||||
variables.insert(variable.clone(), number.unwrap());
|
|
||||||
update = true;
|
|
||||||
} else if formula.starts_with("NOT") {
|
|
||||||
let mut parts = formula.split(" ");
|
|
||||||
parts.next();
|
|
||||||
let negated = parts.next().unwrap().to_string();
|
|
||||||
if variables.contains_key(&negated) {
|
|
||||||
let value = !(*variables.get(&negated).unwrap());
|
|
||||||
variables.insert(variable.clone(), value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
} else if !formula.contains(" "){
|
|
||||||
if variables.contains_key(formula) {
|
|
||||||
let value = *variables.get(formula).unwrap();
|
|
||||||
variables.insert(variable.clone(),value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut parts = formula.split(" ");
|
|
||||||
let operand_a = parts.next().unwrap();
|
|
||||||
let operator = parts.next().unwrap();
|
|
||||||
let operand_b = parts.next().unwrap();
|
|
||||||
let operand_a_u16_result = operand_a.parse::<u16>();
|
|
||||||
let operand_b_u16_result = operand_b.parse::<u16>();
|
|
||||||
if (!operand_a_u16_result.is_ok() && !variables.contains_key(operand_a))
|
|
||||||
|| (!operand_b_u16_result.is_ok() && !variables.contains_key(operand_b)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let operand_a_u16: u16;
|
|
||||||
let operand_b_u16: u16;
|
|
||||||
if operand_a_u16_result.is_ok() {
|
|
||||||
operand_a_u16 = operand_a_u16_result.unwrap();
|
|
||||||
} else {
|
|
||||||
operand_a_u16 = *variables.get(operand_a).unwrap();
|
|
||||||
}
|
|
||||||
if operand_b_u16_result.is_ok() {
|
|
||||||
operand_b_u16 = operand_b_u16_result.unwrap();
|
|
||||||
} else {
|
|
||||||
operand_b_u16 = *variables.get(operand_b).unwrap();
|
|
||||||
}
|
|
||||||
let value = match operator {
|
|
||||||
"AND" => operand_a_u16 & operand_b_u16,
|
|
||||||
"OR" => operand_a_u16 | operand_b_u16,
|
|
||||||
"LSHIFT" => operand_a_u16 << operand_b_u16,
|
|
||||||
"RSHIFT" => operand_a_u16 >> operand_b_u16,
|
|
||||||
&_ => 0,
|
|
||||||
};
|
|
||||||
variables.insert(variable.clone(), value);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if !update {
|
|
||||||
panic!("I cannot progress in the emulation, target cannot be reached");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
|
||||||
let mut variables: BTreeMap<Variable, u16> = BTreeMap::new();
|
|
||||||
let wire_a: Variable = "a".to_string();
|
|
||||||
let mut circuit: Vec<(Formula, Variable)> = Vec::new();
|
|
||||||
for line in io::stdin().lines() {
|
|
||||||
let line = line.unwrap();
|
|
||||||
let mut parts = line.split(" -> ");
|
|
||||||
let formula: Formula = parts.next().unwrap().to_string();
|
|
||||||
let variable: Variable = parts.next().unwrap().to_string();
|
|
||||||
circuit.push((formula, variable));
|
|
||||||
}
|
|
||||||
emulate(&circuit, &mut variables, &wire_a);
|
|
||||||
let signal_b = variables.get(&wire_a).unwrap();
|
|
||||||
let wire_b = "b".to_string();
|
|
||||||
let mut new_variables: BTreeMap<Variable, u16> = BTreeMap::new();
|
|
||||||
new_variables.insert(wire_b.clone(), *signal_b);
|
|
||||||
emulate(&circuit, &mut new_variables, &wire_a);
|
|
||||||
println!("{}", new_variables.get(&wire_a).unwrap());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
Reference in New Issue