-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC Day 08.m
More file actions
51 lines (40 loc) · 1.28 KB
/
AoC Day 08.m
File metadata and controls
51 lines (40 loc) · 1.28 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(* ::Package:: *)
(* ::Text:: *)
(*Written December 8th, 2022.*)
(*Import*)
day = 8;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
toExpression[inputText_] :=
Map[
If[! IntegerQ[#] \[And]
StringMatchQ[#,
Alternatives["+", "-", ""] ~~ DigitCharacter ..],
ToExpression[#], #] &,
inputText,
{Depth[inputText] - 1, Depth[inputText]}];
input=toExpression[Characters /@ StringSplit[Import[inputPath], "\n"]];
(*Setup*)
{x, y} = Dimensions[input];
trees[list_, cmp_] := FirstPosition[list, _?(# >= cmp &), {Length[list]}][[1]];
(*Part 1*)
visible = Table[
If[
row == x \[Or] column == y \[Or]
input[[row, column]] >
Min[
Max[input[[row, ;; column - 1]]],
Max[input[[row, column + 1 ;;]]],
Max[input[[;; row - 1, column]]],
Max[input[[row + 1 ;;, column]]]
], 1, 0],
{row, x}, {column, y}];
Total[Total[visible]]
(*Part 2*)
scenic = Table[
Times @@ {
trees[input[[row, column - 1 ;; 1 ;; -1]], input[[row, column]]],
trees[input[[row, column + 1 ;;]], input[[row, column]]],
trees[input[[row - 1 ;; 1 ;; -1, column]], input[[row, column]]],
trees[input[[row + 1 ;;, column]], input[[row, column]]]},
{row, 2, x - 1}, {column, 2, y - 1}];
Max[Max[scenic]]