Skip to content

Commit

Permalink
pocket watch everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Dec 17, 2024
1 parent 85afdc8 commit c0035fd
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/aoc_2024/day_1.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import gleam/list
import gleam/otp/task
import gleam/result
import gleam/string
import pocket_watch

pub type Input =
#(List(Int), List(Int))

pub fn parse(input: String) -> Input {
use <- pocket_watch.simple("parse")
input
|> string.split("\n")
|> extra.pmap(string.split(_, " "))
Expand All @@ -25,6 +27,7 @@ pub fn parse(input: String) -> Input {
}

pub fn pt_1(input: Input) {
use <- pocket_watch.simple("part 1")
let sorted_xs_handle = task.async(fn() { list.sort(input.0, int.compare) })
let sorted_ys_handle = task.async(fn() { list.sort(input.1, int.compare) })
let sorted_xs = task.await_forever(sorted_xs_handle)
Expand All @@ -35,6 +38,7 @@ pub fn pt_1(input: Input) {
}

pub fn pt_2(input: Input) {
use <- pocket_watch.simple("part 2")
let #(xs, ys) = input
let freqs = extra.frequencies(ys)

Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_10.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import gleam/int
import gleam/list
import gleam/set.{type Set}
import grid.{type Grid, type XY}
import pocket_watch

pub fn parse(input: String) -> Grid(Int) {
use <- pocket_watch.simple("parse")
grid.from_string(input)
|> grid.map(fn(_, c) { extra.yolo_int(c) })
}

pub fn pt_1(input: Grid(Int)) {
use <- pocket_watch.simple("part 1")
let starting_positions: List(XY) = grid.find_all_exact(input, 0)
starting_positions
|> list.map(trailheads_count(input, _))
|> int.sum
}

pub fn pt_2(input: Grid(Int)) {
use <- pocket_watch.simple("part 2")
let starting_positions: List(XY) = grid.find_all_exact(input, 0)
starting_positions
|> list.map(trailheads_rating(input, _))
Expand Down
4 changes: 2 additions & 2 deletions src/aoc_2024/day_11.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub fn parse(input: String) -> List(Int) {
}

pub fn pt_1(input: List(Int)) {
use <- pocket_watch.simple("pt_1")
use <- pocket_watch.simple("part 1")
use cache <- memo.create()
input
|> list.map(c(_, 25, cache))
|> int.sum
}

pub fn pt_2(input: List(Int)) {
use <- pocket_watch.simple("pt_2")
use <- pocket_watch.simple("part 2")
use cache <- memo.create()
input
|> list.map(c(_, 75, cache))
Expand Down
6 changes: 5 additions & 1 deletion src/aoc_2024/day_12.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import gleam/int
import gleam/list
import gleam/set.{type Set}
import grid.{type Grid, type XY}
import pocket_watch

pub fn parse(input: String) -> Grid(String) {
use <- pocket_watch.simple("parse")
grid.from_string(input)
}

pub fn pt_1(input: Grid(String)) {
use <- pocket_watch.simple("part 1")
grid.group_consecutive(input, grid.orthogonal_dirs)
|> list.map(fn(kv) { area(kv.1) * perimeter(kv.1) })
|> int.sum
}

pub fn pt_2(input: Grid(String)) {
use <- pocket_watch.simple("part 2")
//io.println_error(grid.to_string(
// input,
// fun: fn(s) { #(s, Error(Nil)) },
Expand Down Expand Up @@ -55,7 +59,7 @@ pub fn pt_2(input: Grid(String)) {
// empty: ".",
//))

list.map2(groups, distinguished_groups, fn(old, kv) {
list.map2(groups, distinguished_groups, fn(_, kv) {
let area_ = area(kv.1)
let sides_ = sides(fences, group_id: kv.0)
let result = area_ * sides_
Expand Down
5 changes: 4 additions & 1 deletion src/aoc_2024/day_13.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import extra
import gleam/bool
import gleam/int
import gleam/io
import gleam/list
import gleam/string
import grid.{type XY}
import pocket_watch

pub type Machine {
Machine(a: XY, b: XY, prize: XY)
}

pub fn parse(input: String) -> List(Machine) {
use <- pocket_watch.simple("parse")
input
|> string.split("\n\n")
|> list.map(parse_machine)
Expand Down Expand Up @@ -38,6 +39,7 @@ fn parse_machine(input: String) -> Machine {
}

pub fn pt_1(input: List(Machine)) {
use <- pocket_watch.simple("part 1")
input
|> list.map(solve_machine)
|> int.sum
Expand Down Expand Up @@ -68,6 +70,7 @@ fn solve_machine(machine: Machine) -> Int {
}

pub fn pt_2(input: List(Machine)) {
use <- pocket_watch.simple("part 2")
input
|> list.map(fn(machine) {
Machine(
Expand Down
6 changes: 5 additions & 1 deletion src/aoc_2024/day_14.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import gleam/int
import gleam/io
import gleam/list
import gleam/string
import grid.{type XY, Dims}
import grid.{type XY}
import pocket_watch

pub type Robot {
Robot(p: XY, v: XY)
}

pub fn parse(input: String) -> List(Robot) {
use <- pocket_watch.simple("parse")
input
|> string.split("\n")
|> list.map(parse_robot)
Expand Down Expand Up @@ -38,6 +40,7 @@ fn parse_robot(line: String) -> Robot {
}

pub fn pt_1(robots: List(Robot)) {
use <- pocket_watch.simple("part 1")
robots
|> step_times(100)
|> io.debug
Expand Down Expand Up @@ -103,6 +106,7 @@ fn robots_in_quadrant(robots: List(Robot), q: XY) -> List(Robot) {
}

pub fn pt_2(robots: List(Robot)) {
use <- pocket_watch.simple("part 2")
step_pt2(0, robots)
}

Expand Down
6 changes: 5 additions & 1 deletion src/aoc_2024/day_15.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import gleam/io
import gleam/list
import gleam/string
import grid.{type Dir, type Grid, type XY}
import pocket_watch

pub type Input {
Input(grid: Grid(Entity), robot: XY, moves: List(Dir))
Expand All @@ -15,6 +16,7 @@ pub type Entity {
}

pub fn parse(input: String) -> Input {
use <- pocket_watch.simple("parse")
case string.split(input, "\n\n") {
[map, moves] -> {
let char_grid = grid.from_string(map)
Expand Down Expand Up @@ -50,6 +52,7 @@ pub fn parse(input: String) -> Input {
}

pub fn pt_1(input: Input) {
use <- pocket_watch.simple("part 1")
input.moves
|> list.fold(from: #(input.grid, input.robot), with: step)
|> fn(state) { state.0 }
Expand All @@ -60,6 +63,7 @@ pub fn pt_1(input: Input) {
}

pub fn pt_2(input: Input) {
use <- pocket_watch.simple("part 2")
let wider_grid = widen(input.grid)
let wider_robot = grid.xy_mul(input.robot, #(2, 1))
let #(final_grid, _) =
Expand Down Expand Up @@ -266,7 +270,7 @@ fn wider_step_find_nonbox(
frontmost_touches,
list.map(frontmost_touches, grid.get(grid, _)),
))
todo
panic as "Unfinished case (not needed though)"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_2.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import extra
import gleam/int
import gleam/list
import gleam/string
import pocket_watch

pub fn parse(input: String) -> List(List(Int)) {
use <- pocket_watch.simple("parse")
input
|> string.split("\n")
|> list.map(fn(line) {
Expand Down Expand Up @@ -32,10 +34,12 @@ fn is_ok(line: List(Int)) {
}

pub fn pt_1(input: List(List(Int))) {
use <- pocket_watch.simple("part 1")
list.count(input, is_ok)
}

pub fn pt_2(input: List(List(Int))) {
use <- pocket_watch.simple("part 2")
input
|> list.count(fn(line) {
line
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_3.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import gleam/int
import gleam/list
import gleam/regexp
import gleam/string
import pocket_watch

pub type Op {
Mul(Int, Int)
Expand All @@ -26,6 +27,7 @@ fn from_match(match: regexp.Match) -> Op {
}

pub fn parse(input: String) -> List(Op) {
use <- pocket_watch.simple("parse")
let assert Ok(regex) =
regexp.from_string("mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)")

Expand All @@ -35,6 +37,7 @@ pub fn parse(input: String) -> List(Op) {
}

pub fn pt_1(ops: List(Op)) {
use <- pocket_watch.simple("part 1")
ops
|> list.map(fn(op) {
case op {
Expand All @@ -51,6 +54,7 @@ type State {
}

pub fn pt_2(ops: List(Op)) {
use <- pocket_watch.simple("part 2")
let result =
ops
|> list.fold(State(0, True), fn(acc, op) {
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_4.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ import gleam/list
import gleam/result
import gleam/string
import grid.{type Grid, type XY}
import pocket_watch

pub type XGrid =
Grid(String)

pub fn parse(input: String) -> XGrid {
use <- pocket_watch.simple("parse")
grid.from_string(input)
}

pub fn pt_1(grid: XGrid) {
use <- pocket_watch.simple("part 1")
grid
|> grid.map(fn(xy, _) { xmas_count(grid, xy) })
|> grid.values
|> int.sum
}

pub fn pt_2(grid: XGrid) {
use <- pocket_watch.simple("part 2")
grid
|> grid.map(fn(xy, _) { x_mas_count(grid, xy) })
|> grid.values
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_5.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import gleam/order
import gleam/result
import gleam/set.{type Set}
import gleam/string
import pocket_watch

pub type Rule {
Rule(first: Int, then: Int)
Expand All @@ -17,6 +18,7 @@ pub type Input {
}

pub fn parse(input: String) -> Input {
use <- pocket_watch.simple("parse")
case string.split(input, "\n\n") {
[rules_str, manuals_str] -> {
let rules =
Expand Down Expand Up @@ -73,6 +75,7 @@ fn middle_page(manual: List(Int)) -> Result(Int, Nil) {
}

pub fn pt_1(input: Input) {
use <- pocket_watch.simple("part 1")
let dependents = get_dependents(input)
input.manuals
|> list.filter_map(fn(manual) {
Expand All @@ -99,6 +102,7 @@ fn fix_order(manual: List(Int), dependents: Dict(Int, Set(Int))) -> List(Int) {
}

pub fn pt_2(input: Input) {
use <- pocket_watch.simple("part 2")
let dependents = get_dependents(input)
input.manuals
|> list.filter_map(fn(manual) {
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_6.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import extra
import gleam/list
import gleam/set.{type Set}
import grid.{type Dir, type Grid, type XY}
import pocket_watch

pub type Input {
Input(grid: Grid(Nil), guard_xy: XY)
}

pub fn parse(input: String) -> Input {
use <- pocket_watch.simple("parse")
let g = grid.from_string(input)
let assert Ok(guard_xy) = grid.find_exact(g, "^")
Input(
Expand All @@ -22,6 +24,7 @@ pub fn parse(input: String) -> Input {
}

pub fn pt_1(input: Input) {
use <- pocket_watch.simple("part 1")
pt_1_step(
input.grid,
set.from_list([input.guard_xy]),
Expand All @@ -44,6 +47,7 @@ fn pt_1_step(g: Grid(Nil), seen: Set(XY), current_xy: XY, dir: Dir) -> Set(XY) {
}

pub fn pt_2(input: Input) {
use <- pocket_watch.simple("part 2")
let orig_path =
pt_1_step(
input.grid,
Expand Down
4 changes: 4 additions & 0 deletions src/aoc_2024/day_7.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import extra
import gleam/int
import gleam/list
import gleam/string
import pocket_watch

pub type Row {
Row(result: Int, operands: List(Int))
}

pub fn parse(input: String) -> List(Row) {
use <- pocket_watch.simple("parse")
string.split(input, "\n")
|> list.map(fn(line) {
case string.split(line, ": ") {
Expand All @@ -24,13 +26,15 @@ pub fn parse(input: String) -> List(Row) {
}

pub fn pt_1(input: List(Row)) {
use <- pocket_watch.simple("part 1")
input
|> list.filter(fn(row) { can_be_combined(row, [int.add, int.multiply]) })
|> list.map(fn(row) { row.result })
|> int.sum
}

pub fn pt_2(input: List(Row)) {
use <- pocket_watch.simple("part 2")
input
|> list.filter(fn(row) {
can_be_combined(row, [int.add, int.multiply, concat_digits])
Expand Down
Loading

0 comments on commit c0035fd

Please sign in to comment.