aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-04-06 14:41:14 +0200
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-04-06 14:41:14 +0200
commit20852d6e60bee896e84c9bae77f45211773dbf60 (patch)
tree9e952048527a2c97a2dcd19dbdeb1d6657b5b1f1 /aoc-2020-gleam/src/days
parentbcd24674ec0a594882b276fd48fe70c6e3d7ec86 (diff)
downloadgleam_aoc2020-20852d6e60bee896e84c9bae77f45211773dbf60.tar.gz
gleam_aoc2020-20852d6e60bee896e84c9bae77f45211773dbf60.zip
Finish day 12
Diffstat (limited to 'aoc-2020-gleam/src/days')
-rw-r--r--aoc-2020-gleam/src/days/day11.gleam20
-rw-r--r--aoc-2020-gleam/src/days/day12.gleam123
2 files changed, 131 insertions, 12 deletions
diff --git a/aoc-2020-gleam/src/days/day11.gleam b/aoc-2020-gleam/src/days/day11.gleam
index 1f0344a..db25317 100644
--- a/aoc-2020-gleam/src/days/day11.gleam
+++ b/aoc-2020-gleam/src/days/day11.gleam
@@ -1,6 +1,6 @@
import gleam/io
import gleam/string as str
-import gleam/iterator.{Next} as iter
+import gleam/iterator as iter
import gleam/map.{Map}
import ext/mapx
import ext/setx
@@ -48,8 +48,8 @@ fn build_grid(from input: String) -> Grid {
|> Grid
}
-fn count_near_adjacent(grid: Grid, pos: Pos) -> Int {
- pos
+fn count_near_adjacent(grid: Grid, from start: Pos) -> Int {
+ start
|> pos.neighbours8
|> setx.count(satisfying: fn(n) {
case map.get(grid.data, n) {
@@ -59,14 +59,12 @@ fn count_near_adjacent(grid: Grid, pos: Pos) -> Int {
})
}
-fn count_far_adjacent(grid: Grid, pos: Pos) -> Int {
+fn count_far_adjacent(grid: Grid, from start: Pos) -> Int {
pos.directions8
|> listx.count(satisfying: fn(d) {
- iter.unfold(
- from: pos.add(pos, d),
- with: fn(p) { Next(element: p, accumulator: pos.add(p, d)) },
- )
- // Bigger than the largest map size
+ start
+ |> pos.add(d)
+ |> iterx.unfold_infinitely(pos.add(_, d))
|> iter.take(up_to: 1000)
|> iterx.filter_map(with: map.get(grid.data, _))
|> iter.first
@@ -111,9 +109,7 @@ fn is_stable(grid: Grid, settings: Settings) -> Bool {
fn stabilized_occupied(input: String, settings: Settings) -> Int {
input
|> build_grid
- |> iter.unfold(with: fn(g) {
- Next(element: g, accumulator: step_grid(g, settings))
- })
+ |> iterx.unfold_infinitely(with: step_grid(_, settings))
|> iter.find(one_that: is_stable(_, settings))
|> resx.assert_unwrap
|> count_occupied
diff --git a/aoc-2020-gleam/src/days/day12.gleam b/aoc-2020-gleam/src/days/day12.gleam
new file mode 100644
index 0000000..2986c01
--- /dev/null
+++ b/aoc-2020-gleam/src/days/day12.gleam
@@ -0,0 +1,123 @@
+import gleam/io
+import gleam/int
+import gleam/list
+import gleam/string as str
+import util/input_util
+import util/pos.{Pos}
+import util/dir.{Dir, East, North, South, West}
+
+type Instr {
+ MoveIn(dir: Dir, by: Int)
+ Turn(by: Int)
+ MoveForward(by: Int)
+}
+
+fn parse_instr(line: String) -> Instr {
+ let assert Ok(#(action, value)) = str.pop_grapheme(line)
+ let assert Ok(value) = int.parse(value)
+ case action {
+ "N" -> MoveIn(dir: North, by: value)
+ "E" -> MoveIn(dir: East, by: value)
+ "S" -> MoveIn(dir: South, by: value)
+ "W" -> MoveIn(dir: West, by: value)
+ "L" -> Turn(by: dir.degree_to_turn(-value))
+ "R" -> Turn(by: dir.degree_to_turn(value))
+ "F" -> MoveForward(by: value)
+ }
+}
+
+fn process_moves(
+ lines: List(String),
+ initial: a,
+ execute: fn(a, Instr) -> a,
+ locator: fn(a) -> Pos,
+) -> Int {
+ lines
+ |> list.map(with: parse_instr)
+ |> list.fold(from: initial, with: execute)
+ |> fn(s: a) { pos.manhattan_dist(from: pos.zero, to: locator(s)) }
+}
+
+type State1 {
+ State1(pos: Pos, dir: Dir)
+}
+
+const initial_state1 = State1(pos: pos.zero, dir: East)
+
+fn execute_instr1(prev: State1, instr: Instr) -> State1 {
+ case instr {
+ MoveIn(target, times) ->
+ State1(
+ ..prev,
+ pos: target
+ |> dir.offset
+ |> pos.mul(by: times)
+ |> pos.add(prev.pos),
+ )
+ Turn(times) ->
+ State1(
+ ..prev,
+ dir: prev.dir
+ |> dir.rotate_clockwise(by: times),
+ )
+ MoveForward(times) ->
+ State1(
+ ..prev,
+ pos: prev.dir
+ |> dir.offset
+ |> pos.mul(by: times)
+ |> pos.add(prev.pos),
+ )
+ }
+}
+
+fn part1(lines: List(String)) -> Int {
+ process_moves(lines, initial_state1, execute_instr1, fn(s) { s.pos })
+}
+
+type State2 {
+ State2(ship_pos: Pos, anchor_pos: Pos)
+}
+
+const initial_state2 = State2(ship_pos: pos.zero, anchor_pos: #(10, 1))
+
+fn execute_instr2(prev: State2, instr: Instr) -> State2 {
+ case instr {
+ MoveIn(target, times) ->
+ State2(
+ ..prev,
+ anchor_pos: target
+ |> dir.offset
+ |> pos.mul(by: times)
+ |> pos.add(prev.anchor_pos),
+ )
+ Turn(times) ->
+ State2(
+ ..prev,
+ anchor_pos: pos.rotate_around_origin(this: prev.anchor_pos, by: times),
+ )
+ MoveForward(times) ->
+ State2(
+ ..prev,
+ ship_pos: prev.anchor_pos
+ |> pos.mul(by: times)
+ |> pos.add(prev.ship_pos),
+ )
+ }
+}
+
+fn part2(lines: List(String)) -> Int {
+ process_moves(lines, initial_state2, execute_instr2, fn(s) { s.ship_pos })
+}
+
+pub fn main() -> Nil {
+ let test = input_util.read_lines("test12")
+ let assert 25 = part1(test)
+ let assert 286 = part2(test)
+
+ let input = input_util.read_lines("day12")
+ io.debug(part1(input))
+ io.debug(part2(input))
+
+ Nil
+}