Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://blocks.programming.dev if you prefer sending it through a URL
Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
This was quite fun! I got a bit distracted trying to rewrite safe in point-free style, but I think this version is the most readable. There's probably a more monadic way of writing lessOne as well, but I can't immediately see it.
I am so far very amazed with the compactness of your solutions, your lessOne is very much mind-Bending.
I have never used or seen <$> before, is it a monadic $?
Also I can't seem to find your logic for this safety condition: The levels are either all increasing or all decreasing, did you figure that it wasn't necessary?
For the last point, it isn't needed since the differences between elements should be all positive or all negative for the report to be safe. This is tested with the combination of negate and gradual.
I am also enjoying these Haskell solutions. I'm still learning the language, so it's been cool to compare my solution with these and grow my understanding of Haskell.
Thanks! The other two posters already answered your questions, I think :)
Haskell makes it really easy to build complex operations out of simple functional building blocks, skipping a lot of boilerplate needed in some other languages. I find the compactness easier to read, but I realize that not everyone would agree.
BTW, I'm a relative Haskell newbie. I'm sure more experienced folks could come up with even more interesting solutions!
The function is_sorted_by on Iterators turned out helpful for compactly finding if a report is safe. In part 2 I simply tried the same with each element removed, since all reports are very short.
The is_sorted_by is a really nice approach. I originally tried using that function thinking that |a, b| a > b or |a, b| a < b would cut it but it didn't end up working. I never thought to handle the check for the step being between 1 and 3 in the callback closure for that though.
Haha, you can do it that way, in fact the online Uiua Pad editor has all the operators listed along the top.
But all the operators have ascii names, so you can type e.g.
IsSmall = reduce mul mul fork(>0|<4) abs drop neg 1 - rot 1 dup
and the formatter will reduce that to
IsSmall ← /××⊃(>0|<4)⌵↘¯1-↻1.
whenever you save or execute code.
That works in the Pad, and you can enable similar functionality in other editors.
Yes, it should do. I do run the solutions against the live data, but sometimes tweak the solutions afterwards, so can't always guarantee them :-).
I left the comment as 5 choose 4 as it felt clearer in the context of the test data.
It does still feel very alien at times, but I do love being able to think about how to adopt a more arrays-based approach to solving these problems.
What is this coding style? The function type, name and open brace placement made me think GNU at first, but the code in the body doesn't look like GCS at all.
initially, for part two I was trying to ignore a bad pair not a bad value - read the question!
Only installed Rust on Sunday, day 1 was a mess, today was more controlled. Need to look at some of the rust solutions for std library methods I don't know about.
very focussed on getting it to actually compile/work over making it short or nice!
long!
`
pub mod task_2 {
pub fn task_1(input: &str) -> i32{
let mut valid_count = 0;
let reports = process_input(input);
for report in reports{
let valid = is_report_valid(report);
if valid{
valid_count += 1;
}
}
println!("Valid count: {}", valid_count);
valid_count
}
pub fn task_2(input: &str) -> i32{
let mut valid_count = 0;
let reports = process_input(input);
for report in reports{
let mut valid = is_report_valid(report.clone());
if !valid
{
for position_to_delete in 0..report.len()
{
let mut updated_report = report.clone();
updated_report.remove(position_to_delete);
valid = is_report_valid(updated_report);
if valid { break; }
}
}
if valid{
valid_count += 1;
}
}
println!("Valid count: {}", valid_count);
valid_count
}
fn is_report_valid(report:Vec<i32>) -> bool{
let mut increasing = false;
let mut decreasing = false;
let mut valid = true;
for position in 1..report.len(){
if report[position-1] > report[position]
{
decreasing = true;
}
else if report[position-1] < report[position]
{
increasing = true;
}
else
{
valid = false;
break;
}
if (report[position-1] - report[position]).abs() > 3
{
valid = false;
break;
}
if increasing && decreasing
{
valid = false;
break;
}
}
return valid;
}
pub fn process_input(input: &str) -> Vec<Vec<i32>>{
let mut reports: Vec<Vec<i32>> = Vec::new();
for report_string in input.split("\n"){
let mut report: Vec<i32> = Vec::new();
for value in report_string.split_whitespace() {
report.push(value.parse::<i32>().unwrap());
}
reports.push(report);
}
return reports;
}
Got correct answer for part 1 on first try, but website rejected it. Wasted some time debugging and trying different methods. Only to have the same answer accepted minutes later. =(
proc isSafe(report: seq[int]): bool =
let diffs = collect:
for i, n in report.toOpenArray(1, report.high): n - report[i]
(diffs.allIt(it > 0) or diffs.allIt(it < 0)) and diffs.allIt(it.abs in 1..3)
proc solve(input: string): AOCSolution[int, int] =
let lines = input.splitLines()
var reports: seq[seq[int]]
for line in lines:
reports.add line.split(' ').map(parseInt)
for report in reports:
if report.isSafe():
inc result.part1
inc result.part2
else:
for t in 0..report.high:
var mReport = report
mReport.delete t
if mReport.isSafe():
inc result.part2
break
import { AdventOfCodeSolutionFunction } from "./solutions";
/**
* this function evaluates the
* @param levels a list to check
* @returns -1 if there is no errors, or the index of where there's an unsafe event
*/
export function EvaluateLineSafe(levels: Array<number>) {
// this loop is the checking every number in the line
let isIncreasing: boolean | null = null;
for (let levelIndex = 1; levelIndex < levels.length; levelIndex++) {
const prevLevel = levels[levelIndex - 1]; // previous
const level = levels[levelIndex]; // current
const diff = level - prevLevel; // difference
const absDiff = Math.abs(diff); // absolute difference
// check if increasing too much or not at all
if (absDiff == 0 || absDiff > 3)
return levelIndex; // go to the next report
// set increasing if needed
if (isIncreasing === null) {
isIncreasing = diff > 0;
continue; // compare the next numbers
}
// check if increasing then decreasing
if (!(isIncreasing && diff > 0 || !isIncreasing && diff < 0))
return levelIndex; // go to the next report
}
return -1;
}
export const solution_2: AdventOfCodeSolutionFunction = (input) => {
const reports = input.split("\n");
let safe = 0;
let safe_damp = 0;
// this loop is for every line
main: for (let i = 0; i < reports.length; i++) {
const report = reports[i].trim();
if (!report)
continue; // report is empty
const levels = report.split(" ").map((v) => Number(v));
const evaluation = EvaluateLineSafe(levels);
if(evaluation == -1) {
safe++;
continue;
}
// search around where it failed
for (let offset = evaluation - 2; offset <= evaluation + 2; offset++) {
// delete an evaluation in accordance to the offset
let newLevels = [...levels];
newLevels.splice(offset, 1);
const newEval = EvaluateLineSafe(newLevels);
if(newEval == -1) {
safe_damp++;
continue main;
}
}
}
return `Part 1: ${safe} Part 2: ${safe + safe_damp}`;
}
God, I really wish my solutions weren't so convoluted. Also, this is an O(N^3) solution....
import strutils, times, sequtils, sugar
# check if level transition in record is safe
proc isSafe*(sign:bool, d:int): bool =
sign == (d>0) and d.abs in 1..3;
#check if record is valid
proc validate*(record:seq[int]): bool =
let sign = record[0] > record[1];
return (0..record.len-2).allIt(isSafe(sign, record[it] - record[it+1]))
# check if record is valid as-is
# or if removing any item makes the record valid
proc validate2*(record:seq[int]): bool =
return record.validate or (0..<record.len).anyIt(record.dup(delete(it)).validate)
proc solve*(input:string): array[2,int] =
let lines = input.readFile.strip.splitLines;
let records = lines.mapIt(it.splitWhitespace.map(parseInt));
result[0] = records.countIt(it.validate);
result[1] = records.countIt(it.validate2);
I got stuck on part 2 trying to check everything inside a single loop, which kept getting more ugly. So then I switched to just deleting one item at a time and re-checking the record.
Reworked it after first finding the solution to compress the code a bit, though the range iterators don't really help with readability.
I did learn about the sugar import, which I used to make the sequence duplication more compact: record.dup(delete(it).
That's smart. I haven't thought of using iterators to loop over indexes (except in a for loop).
I got stuck on part 2 trying to check everything inside a single loop, which kept getting more ugly.
Yeah I've thought of simple ways to do this and found none. And looking at the input - it's too easy to bruteforce, especially in compiled lang like Nim.
I forgot that this started yesterday, so I'm already behind. I quite like my solution for part one, but part two will have to wait edit: part 2 was a lot simpler than I thought after a night's sleep.
Rust
use color_eyre::eyre;
use std::{fs, num, str::FromStr};
#[derive(Debug, PartialEq, Eq)]
struct Report(Vec<isize>);
impl FromStr for Report {
type Err = num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Result<Vec<isize>, _> = s
.split_whitespace()
.map(|num| num.parse::<isize>())
.collect();
Ok(Report(v?))
}
}
impl Report {
fn is_safe(&self) -> bool {
let ascending = self.0[1] > self.0[0];
let (low, high) = if ascending { (1, 3) } else { (-3, -1) };
self.0.windows(2).all(|w| {
let a = w[0];
let b = w[1];
b >= a + low && b <= a + high
})
}
fn is_dampsafe(&self) -> bool {
if self.is_safe() {
return true;
}
for i in 0..self.0.len() {
let damped = {
let mut v = self.0.clone();
v.remove(i);
Self(v)
};
if damped.is_safe() {
return true;
}
}
false
}
}
fn main() -> eyre::Result<()> {
color_eyre::install()?;
let part1 = part1("d02/input.txt")?;
let part2 = part2("d02/input.txt")?;
println!("Part 1: {part1}\nPart 2: {part2}");
Ok(())
}
fn part1(filepath: &str) -> eyre::Result<isize> {
let mut num_safe = 0;
for l in fs::read_to_string(filepath)?.lines() {
if Report::from_str(l)?.is_safe() {
num_safe += 1;
}
}
Ok(num_safe)
}
fn part2(filepath: &str) -> eyre::Result<isize> {
let mut num_safe = 0;
for l in fs::read_to_string(filepath)?.lines() {
if Report::from_str(l)?.is_dampsafe() {
num_safe += 1;
}
}
Ok(num_safe)
}
(defun p1-process-line (line)
(mapcar #'parse-integer (str:words line)))
(defun line-direction-p (line)
"make sure the line always goes in the same direction"
(loop for x in line
for y in (cdr line)
count (> x y) into dec
count (< x y) into inc
when (and (> dec 0 ) (> inc 0)) return nil
when (= x y) return nil
finally (return t)))
(defun line-in-range-p (line)
"makes sure the delta is within 3"
(loop for x in line
for y in (cdr line)
for delta = (abs (- x y))
when (or (> delta 3) )
return nil
finally (return t)))
(defun test-line-p (line)
(and (line-in-range-p line) (line-direction-p line)))
(defun run-p1 (file)
(let ((data (read-file file #'p1-process-line)))
(apply #'+ (mapcar (lambda (line) (if (test-line-p line) 1 0)) data))))
Part 2
(defun test-line-p2 (line)
(or (test-line-p (cdr line))
(test-line-p (cdr (reverse line)))
(loop for back on line
collect (car back) into front
when (test-line-p (concatenate 'list front (cddr back)))
return t
finally (return nil)
)))
(defun run-p2 (file)
(let ((data (read-file file #'p1-process-line)))
(loop for line in data
count (test-line-p2 line))))
import re
import aoc
def setup():
return (aoc.get_lines(2), 0)
def safe(data):
order = 0 if data[0] < data[1] else 1
for i in range(0, len(data) - 1):
h = data[i]
t = data[i + 1]
d = abs(h - t)
if d not in [1, 2, 3] or (order == 0 and h >= t) or (
order == 1 and h <= t):
return False
return True
def one():
lines, acc = setup()
for l in lines:
if safe([int(x) for x in re.findall(r'\d+', l)]):
acc += 1
print(acc)
def two():
lines, acc = setup()
for l in lines:
data = [int(x) for x in re.findall(r'\d+', l)]
for i in range(len(data)):
if safe(data[:i] + data[i + 1:]):
acc += 1
break
print(acc)
one()
two()
def is_safe(report: list[int]) -> bool:
global removed
acceptable_range = [_ for _ in range(-3,4) if _ != 0]
diffs = []
if any([report.count(x) > 2 for x in report]):
return False
for i, num in enumerate(report[:-1]):
cur = num
next = report[i+1]
difference = cur - next
diffs.append(difference)
if difference not in acceptable_range:
return False
if len(diffs) > 1:
if diffs[-1] * diffs[-2] <= 0:
return False
return True
with open('input') as reports:
list_of_reports = reports.readlines()[:-1]
count = 0
failed_first_pass = []
failed_twice = []
for reportsub in list_of_reports:
levels = [int(l) for l in reportsub.split()]
original = levels.copy()
if is_safe(levels):
safe = True
count += 1
else:
failed_first_pass.append(levels)
for report in failed_first_pass:
print(report)
working_copy = report.copy()
for i in range(len(report)):
safe = False
working_copy.pop(i)
print("checking", working_copy)
if is_safe(working_copy):
count += 1
safe = True
break
else:
working_copy = report.copy()
print(count)
this took me so fucking long and in the end i just went for brute force anyway. there are still remnants of some of previous, overly complicated, failed attempts, like the hideous global removed. In the end, I realized I was fucking up by using remove() instead of pop(), it was causing cases with duplicates where the removal of one would yield a safe result to count as unsafe.
import kotlin.math.abs
import kotlin.math.sign
data class Report(val levels: List<Int>) {
fun isSafe(withProblemDampener: Boolean): Boolean {
var orderSign = 0.0f // - 1 is descending; +1 is ascending
levels.zipWithNext().forEachIndexed { index, level ->
val difference = (level.second - level.first).toFloat()
if (orderSign == 0.0f) orderSign = sign(difference)
if (sign(difference) != orderSign || abs(difference) !in 1.0..3.0) {
// With problem dampener: Drop either element in the pair or the first element from the original list and check if the result is now safe.
return if (withProblemDampener) {
Report(levels.drop(1)).isSafe(false) || Report(levels.withoutElementAt(index)).isSafe(false) || Report(levels.withoutElementAt(index + 1)).isSafe(false)
} else false
}
}
return true
}
}
fun main() {
fun part1(input: List<String>): Int = input.map { Report(it.split(" ").map { it.toInt() }).isSafe(false) }.count { it }
fun part2(input: List<String>): Int = input.map { Report(it.split(" ").map { it.toInt() }).isSafe(true) }.count { it }
// Or read a large test input from the `src/Day01_test.txt` file:
val testInput = readInput("Day02_test")
check(part1(testInput) == 2)
check(part2(testInput) == 4)
// Read the input from the `src/Day01.txt` file.
val input = readInput("Day02")
part1(input).println()
part2(input).println()
}
The Report#isSafe method essentially solves both parts.
I've had a bit of a trip up in part 2:
I initially only checked, if the report was safe, if either elements in the pair were to be removed. But in the edge case, that the first pair has different monotonic behaviour than the rest, the issue would only be detected by the second pair with indices (2, 3), whilst removing the first element in the list would yield a safe report.
Discovered a number of frustrations with this supposedly small and elegant language
Smalltalk's block based iteration has NO control flow
blocks are very dissimilar to functions
You cannot early return from blocks (leading to a lot of horrible nested ifs or boolean operations)
Smalltalk's messages (~functions) cannot take multiple arguments, instead it has these sort of combined messages, so instead of a function with three arguments, you would send 3 combined messages with one argument. This is fine until you try to chain messages with arguments, as smalltalk will interpret them as a combined message and fail, forcing you to either break into lots of temp variables, or do lisp-like parenthesis nesting, both of which I hate
Smalltalk's order of operations, while nice and simple, is also quite frustrating at times, similar to #4, forcing you to break into lots of temp variables, or do lisp-like parenthesis nesting. For instance (nums at: i) - (nums at: i+1) which would be nums[i] - nums[i+1] in most languages
defmodule AdventOfCode.Solution.Year2024.Day02 do
use AdventOfCode.Solution.SharedParse
@impl true
def parse(input) do
for line <- String.split(input, "\n", trim: true),
do: String.split(line) |> Enum.map(&String.to_integer/1)
end
def part1(input) do
Enum.count(input, &is_safe(&1, false))
end
def part2(input) do
Enum.count(input, &(is_safe(&1, true) or is_safe(tl(&1), false)))
end
def is_safe([a, b, c | rest], can_fix) do
cond do
(b - a) * (c - b) > 0 and abs(b - a) in 1..3 and abs(c - b) in 1..3 ->
is_safe([b, c | rest], can_fix)
can_fix ->
is_safe([a, c | rest], false) or is_safe([a, b | rest], false)
true ->
false
end
end
def is_safe(_, _), do: true
end