Compare commits

...

10 Commits

Author SHA1 Message Date
Samuel Ortion 1a8565b67c
day 7: part 2: rust 2024-11-29 19:55:24 +01:00
Samuel Ortion f3a948d4bf
day 7: part 1: rust 2024-11-29 19:33:19 +01:00
Samuel Ortion 031471c8eb
day5: part2 lisp 2024-11-28 13:57:44 +01:00
Samuel Ortion c1b01e4461
day5: part 1 lisp 2024-11-27 19:21:35 +01:00
Samuel Ortion 45277d41f5
day4: part 1 and 2 with python 2024-11-27 18:00:13 +01:00
Samuel Ortion e8ca549ac5
day3: lisp, part2 2024-11-27 17:44:27 +01:00
Samuel Ortion 1f4534f41b
day3: lisp 2024-11-27 17:21:07 +01:00
Samuel Ortion c583bfb813
Solve day 3 with python 2024-11-27 10:50:13 +01:00
Samuel Ortion 4e9b501da1
Day 2: Wrapping paper
Solve part 1 in lisp
2024-06-03 08:20:44 +02:00
Samuel Ortion 26a67f0dc8
Day 2 input data 2024-06-02 21:17:52 +02:00
13 changed files with 1507 additions and 0 deletions

1000
2015/days/02/data/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,42 @@
;; 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)

51
2015/days/03/walk.lisp Normal file
View File

@ -0,0 +1,51 @@
;; 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)

34
2015/days/03/walk.py Normal file
View File

@ -0,0 +1,34 @@
#!/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)

62
2015/days/03/walk2.lisp Normal file
View File

@ -0,0 +1,62 @@
;; 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)

13
2015/days/04/md5.py Normal file
View File

@ -0,0 +1,13 @@
#!/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

51
2015/days/05/nice.lisp Normal file
View File

@ -0,0 +1,51 @@
(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)

46
2015/days/05/nice2.lisp Normal file
View File

@ -0,0 +1,46 @@
(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)

View File

@ -0,0 +1,6 @@
[package]
name = "part1"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,92 @@
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(())
}

7
2015/days/07/part2/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "part2"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "part2"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,97 @@
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(())
}