Spyke

Posts

advent_of_code·Advent Of Codebybugsmith

[2024 Day 06] Any Gleamings in the house?

I'm running behind as usual. I'm still rather new to Gleam and would love some feedback on how I handled the Day 06 puzzle:

import gleam/dict
import gleam/io
import gleam/list
import gleam/option.{None, Some}
import gleam/result
import gleam/set.{type Set}
import gleam/string
import simplifile

pub type Point =
  #(Int, Int)

pub type Grid(a) =
  dict.Dict(Point, a)

pub type Direction {
  North
  East
  South
  West
}

pub type Loops {
  DoesLoop
  DoesNotLoop
}

pub type Guard {
  Guard(position: Point, direction: Direction)
}

fn get_guard(grid: Grid(String)) -> Guard {
  let pos = dict.filter(grid, fn(_pos, char) { char == "^" })
  let assert Ok(pos) = case dict.size(pos) {
    1 -> list.first(dict.keys(pos))
    0 -> panic as "No guard found in input!"
    _ -> panic as "More than one guard found in input!"
  }
  Guard(pos, North)
}

fn move_guard(guard: Guard) -> Guard {
  let new_pos = case guard.direction {
    North -> #(-1, 0)
    East -> #(0, 1)
    South -> #(1, 0)
    West -> #(0, -1)
  }
  Guard(
    #(guard.position.0 + new_pos.0, guard.position.1 + new_pos.1),
    guard.direction,
  )
}

fn turn_guard(guard: Guard) -> Guard {
  let new_dir = case guard.direction {
    North -> East
    East -> South
    South -> West
    West -> North
  }
  Guard(guard.position, new_dir)
}

fn get_obstacles(grid: Grid(String)) -> List(Point) {
  dict.filter(grid, fn(_pos, char) { char == "#" })
  |> dict.keys()
}

fn recurse_grid(
  grid: Grid(String),
  guard: Guard,
  obstacles: List(#(Int, Int)),
  visited: Set(#(#(Int, Int), Direction)),
) -> #(Set(#(#(Int, Int), Direction)), Loops) {
  let new_guard = move_guard(guard)
  let position = new_guard.position
  let dir = new_guard.direction
  case dict.has_key(grid, position) {
    False -> #(visited, DoesNotLoop)
    True -> {
      case set.contains(visited, #(position, dir)) {
        True -> {
          #(visited, DoesLoop)
        }
        False -> {
          case list.contains(obstacles, position) {
            True -> recurse_grid(grid, turn_guard(guard), obstacles, visited)
            False ->
              recurse_grid(
                grid,
                new_guard,
                obstacles,
                set.insert(visited, #(position, dir)),
              )
          }
        }
      }
    }
  }
}

fn get_grid_input(filename: String) -> Grid(String) {
  let lines =
    filename
    |> simplifile.read()
    |> result.unwrap("")
    |> string.trim()
    |> string.split("\n")
  use grid, row, row_idx <- list.index_fold(lines, dict.new())
  use grid, col, col_idx <- list.index_fold(string.to_graphemes(row), grid)
  dict.insert(grid, #(row_idx, col_idx), col)
}

fn part_one(
  grid: Grid(String),
) -> #(#(Set(#(#(Int, Int), Direction)), Loops), Int) {
  let guard = get_guard(grid)
  let obstacles = get_obstacles(grid)
  let visited = set.new() |> set.insert(#(guard.position, guard.direction))
  let visited = recurse_grid(grid, guard, obstacles, visited)
  let visited_without_dir =
    set.fold(visited.0, set.new(), fn(acc, x) { set.insert(acc, x.0) })
  #(visited, visited_without_dir |> set.size())
}

fn check_loop(grid: Grid(String), blocker: Point) -> Loops {
  let blocked_grid =
    dict.upsert(grid, blocker, fn(x) {
      case x {
        Some("^") -> "^"
        Some(_) -> "#"
        None -> "#"
      }
    })
  let visited = part_one(blocked_grid).0
  visited.1
}

fn part_two(grid: Grid(String), visited: Set(#(#(Int, Int), Direction))) {
  let visited =
    set.fold(visited, set.new(), fn(acc, x) { set.insert(acc, x.0) })
  use counter, position <- set.fold(visited, 0)
  case check_loop(grid, position) {
    DoesLoop -> counter + 1
    DoesNotLoop -> counter
  }
}

pub fn main() {
  let input = "input.in"
  let p1 = input |> get_grid_input() |> part_one
  let visited = p1.0.0
  io.debug(p1.1)
  input |> get_grid_input |> part_two(visited) |> io.debug()
}
View original on programming.dev
meta·Programming.dev Metabybugsmith

Piracy-Related Content on P.D: An Open Dialogue with Our Community

Hello P.D users.

I'm sure the recent news of lemmy.world's removal of piracy communities has not escaped the attention of many of you. For those who have not heard, here is their announcement on the matter.

Now, being as transparent as possible, this is not a subject that the admin team of P.D have discussed in great detail up until this point. We have yet to create an explicit rule stating that piracy related content is forbidden on this instance, and doing so is not something any of us wish to do.

With that being said, this topic requires more deliberation and consideration. There is more complexity to this issue than taking a stance for, against, or tolerant of piracy. There are legal considerations. And the truth is that we are not 100% sure on what the legal implications are for allowing piracy related content to exist on this instance, either directly on our communities or indirectly via federation.

One thing that must be considered when looking into the legalities is jurisdiction. I believe being part of a larger federation complicates this more than a centralised service, but P.D must at a minimum be considerate of the laws of the country its server(s) is hosted in, and the laws of the country the person responsible for the instance resides in.

For those who don't know, this instance is administrated by a reasonably large team of volunteers, but is hosted and ultimately provided by only one: snowe. There is no legal entity behind P.D other than his person, and this means that any ramifications of this subject ultimately fall on his shoulders.

After an initial discussion between us, it is clear that some professional legal advice is required. Snowe is intending to seek formal legal advice. Up until that point, we cannot say what the official P.D stance will be on this topic long term.

What I can say, is that nobody in this administrative team desires to impose any restrictions on users of this instance that overreach and limit discussion unnecessarily. So long as content here is not dangerous, hurtful or offensive, we have no wish to filter it out on an instance level. Communities can impose their own rules, for the most part.

You can expect a further update on this topic in the future, once we have a better understanding ourselves, and we will keep this topic as transparent as we possibly can. Until then, in relation to this matter, we do not currently intend to defederate from any instances or ban any communities that discuss piracy. Linking to websites that provide pirated content will also not be explicitly banned, but linking to or directly hosting pirated content on P.D is prohibited. Should any of these rules change, we will communicate it as quickly as possible and far ahead of any drastic action that would need to take place as a result.

I believe the majority of our users here will understand the pressure that any Lemmy instance faces by hosting content that can make them susceptible to legal action. To those of you who are understanding, we thank you for that, and are grateful for your patience with us while we get a better understanding ourselves.

Feel free to discuss this matter here.

Thanks,
The P.D. Admin Team.

View original on programming.dev