44

Day 2: Red-Nosed Reports

Megathread guidelines

  • 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

FAQ

(page 2) 7 comments
sorted by: hot top controversial new old
[-] bugsmith@programming.dev 1 points 2 weeks ago

Elixir

defmodule Day02 do
  defp part1(reports) do
    reports
    |> Enum.map(fn report ->
      levels =
        report
        |> String.split()
        |> Enum.map(&String.to_integer/1)

      cond do
        sequence_is_safe?(levels) ->
          :safe

        true ->
          :unsafe
      end
    end)
    |> Enum.count(fn x -> x == :safe end)
  end

  defp part2(reports) do
    reports
    |> Enum.map(fn report ->
      levels =
        report
        |> String.split()
        |> Enum.map(&String.to_integer/1)

      sequences =
        0..(length(levels) - 1)
        |> Enum.map(fn i ->
          List.delete_at(levels, i)
        end)

      cond do
        sequence_is_safe?(levels) ->
          :safe

        Enum.any?(sequences, &sequence_is_safe?/1) ->
          :safe

        true ->
          :unsafe
      end
    end)
    |> Enum.count(fn x -> x == :safe end)
  end

  defp all_gaps_within_max_diff?(numbers) do
    numbers
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] -> abs(b - a) <= 3 end)
  end

  defp is_strictly_increasing?(numbers) do
    numbers
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] -> a < b end)
  end

  defp is_strictly_decreasing?(numbers) do
    numbers
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] -> a > b end)
  end

  defp sequence_is_safe?(numbers) do
    (is_strictly_increasing?(numbers) or
       is_strictly_decreasing?(numbers)) and all_gaps_within_max_diff?(numbers)
  end

  def run(data) do
    reports = data |> String.split("\n", trim: true)
    p1 = part1(reports)
    p2 = part2(reports)
    IO.puts(p1)
    IO.puts(p2)
  end
end

data = File.read!("input.in")
Day02.run(data)
[-] Hammerheart@programming.dev 1 points 2 weeks ago
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)
[-] Hammerheart@programming.dev 1 points 2 weeks ago

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.

[-] the_beber@lemm.ee 1 points 2 weeks ago

Kotlin:

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.

[-] hosaka@programming.dev 1 points 2 weeks ago

#Zig

const std = @import("std");
const List = std.ArrayList;

const splitScalar = std.mem.splitScalar;
const parseInt = std.fmt.parseInt;
const print = std.debug.print;
const concat = std.mem.concat;

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();

const Answer = struct {
    safe: u32,
    tolerated: u32,
};

pub fn isSafe(levels: []i32) bool {
    if (levels.len == 0) {
        return false;
    }
    // slide window in pairs, advancing by one
    var it = std.mem.window(i32, levels, 2, 1);
    const first = it.first();
    const decreasing = first[0] - first[1] > 0;
    it.reset(); // rewind the iterator

    while (it.next()) |slice| {
        const lhs: i32 = slice[0];
        const rhs: i32 = slice[1];
        if (decreasing) {
            if (lhs <= rhs) return false;
            if (lhs - rhs < 1 or lhs - rhs > 3) return false;
        } else {
            if (rhs <= lhs) return false;
            if (rhs - lhs < 1 or rhs - lhs > 3) return false;
        }
    }
    return true;
}

pub fn solve(input: []const u8) !Answer {
    var rows = splitScalar(u8, input, '\n');

    // PART 1

    // determine how many reports are safe
    var safe_reports: u32 = 0;
    var tolerated_reports: u32 = 0;
    var unsafe_reports = List([]i32).init(alloc);
    defer unsafe_reports.deinit();

    while (rows.next()) |row| {
        var levels = splitScalar(u8, row, ' ');

        var report = List(i32).init(alloc);
        defer report.deinit();

        while (levels.next()) |level| {
            const value = parseInt(i32, level, 10) catch continue;
            report.append(value) catch continue;
        }

        if (isSafe(report.items)) {
            safe_reports += 1;
        } else {
            try unsafe_reports.append(try alloc.dupe(i32, report.items));
        }
    }

    // PART 2

    // determine how many unsafe reports can be tolerated
    for (unsafe_reports.items) |report| {
        var index: usize = 0;
        while (index < report.len) : (index += 1) {
            // mutate report by removing one level
            const mutated_report = concat(
                alloc,
                i32,
                &[_][]const i32{ report[0..index], report[index + 1 ..] },
            ) catch report;
            defer alloc.free(mutated_report);

            if (isSafe(mutated_report)) {
                tolerated_reports += 1;
                break;
            }
        }
    }

    return Answer{ .safe = safe_reports, .tolerated = safe_reports + tolerated_reports };
}

pub fn main() !void {
    const answer = try solve(@embedFile("input.txt"));
    print("Part 1: {d}\n", .{answer.safe});
    print("Part 2: {d}\n", .{answer.tolerated});
}

test "test input" {
    const answer = try solve(@embedFile("test.txt"));
    try std.testing.expectEqual(2, answer.safe);
    try std.testing.expectEqual(4, answer.tolerated);
[-] morrowind@lemmy.ml 1 points 2 weeks ago

Smalltalk

Discovered a number of frustrations with this supposedly small and elegant language

  1. Smalltalk's block based iteration has NO control flow
  2. blocks are very dissimilar to functions
  3. You cannot early return from blocks (leading to a lot of horrible nested ifs or boolean operations)
  4. 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
  5. 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

Part 1

day2p1: input
    ^ (input lines 
        collect: [ :l | l substrings collect: [ :s | s asInteger ]])
        
        count: [ :nums |
            (nums = nums sorted or: nums = nums sorted reverse) 
                and: [
                    (1 to: nums size-1) allSatisfy: [ :i | 
                        ((nums at: i) - (nums at: i+1)) abs between: 1 and: 3
        ]    ]    ]

Part 2

day2p2: input    
    | temp |
    
    ^ (input lines 
        collect: [ :l | (l substrings collect: [ :s | s asInteger ]) asOrderedCollection ])
         
        count: [ :nums |
            
            (self day2p2helper: nums)
            or: [ 
                ((1 to: nums size) anySatisfy: [ :i |
                    temp := nums copy.
                    temp removeAt: i.
                    self day2p2helper: temp
                ])
            
            
            or: [(self day2p2helper: nums reversed)
            or: [
                (1 to: nums size) anySatisfy: [ :i |
                    temp := nums reversed.
                    temp removeAt: i.
                    self day2p2helper: temp
                ]
            ]]] .
        ]
day2p2helper: nums
    ^ (1 to: nums size - 1) allSatisfy: [ :i | 
        ((nums at: i+1) - (nums at: i)) between: 1 and: 3    
    ].
[-] aurele@sh.itjust.works 1 points 1 week ago

Elixir

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
load more comments
view more: ‹ prev next ›
this post was submitted on 02 Dec 2024
44 points (97.8% liked)

Advent Of Code

920 readers
62 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS