Skip to content

Commit f98a2aa

Browse files
committed
old problem
1 parent 96b3934 commit f98a2aa

File tree

8 files changed

+232
-37
lines changed

8 files changed

+232
-37
lines changed

README

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
~haskell~.
77

88
** Scheme
9-
9+
1010
*** 2019
1111

1212
- [[./scheme/19/Day5][Day 5: Sunny with a Chance of Asteroids]]
1313

14+
*** 2017
15+
16+
- [[./scheme/17/Day24][Day 24: Electromagnetic Moat]]
17+
1418
*** 2015
1519

1620
- [[./scheme/15/Day7][Day 7: Some Assembly Required]]

code/biblio.ss

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
(lambda ()
4444
(let ((cleaner (input-chars-with transform)))
4545
(with-input-from-string (list->string (cleaner))
46-
parser))))
46+
parser))))
4747

4848
(define comma-separated
4949
(input-chars-transform-parse

input/17/24.in

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
25/13
2+
4/43
3+
42/42
4+
39/40
5+
17/18
6+
30/7
7+
12/12
8+
32/28
9+
9/28
10+
1/1
11+
16/7
12+
47/43
13+
34/16
14+
39/36
15+
6/4
16+
3/2
17+
10/49
18+
46/50
19+
18/25
20+
2/23
21+
3/21
22+
5/24
23+
46/26
24+
50/19
25+
26/41
26+
1/50
27+
47/41
28+
39/50
29+
12/14
30+
11/19
31+
28/2
32+
38/47
33+
5/5
34+
38/34
35+
39/39
36+
17/34
37+
42/16
38+
32/23
39+
13/21
40+
28/6
41+
6/20
42+
1/30
43+
44/21
44+
11/28
45+
14/17
46+
33/33
47+
17/43
48+
31/13
49+
11/21
50+
31/39
51+
0/9
52+
13/50
53+
10/14
54+
16/10
55+
3/24
56+
7/0
57+
50/50

j/19/Day3

+3-14
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,18 @@ trip =: [: <"0 [: +/\ 0&, @: >
2121
step =: (%|)@:-~
2222

2323
fill =: 4 : 0
24-
dz =. (0{y)-(_1{x)
24+
dz =. y - x
2525
step =. dz % | dz
26-
(_1{x)+(step * >:i.|dz)
26+
x+(step * >:i.|dz)
2727
)
28-
wires
28+
NB. wires
2929

30-
10 fill 10j3
31-
10 step 10j4
3230
NB. fill/ 0 10 10j3
3331
NB. 0 ;"1 (A ,: B)
3432
NB. (trip A); (zip_trip (trip A))
3533
#+end_src
3634

3735
#+RESULTS:
38-
: ┌───┬───┬────┬────┐
39-
: │8 │0j5│_5 │0j_3│
40-
: ├───┼───┼────┼────┤
41-
: │0j7│6 │0j_4│_4 │
42-
: └───┴───┴────┴────┘
43-
:
44-
: 10j1 10j2 10j3
45-
:
46-
: 0j1
4736

4837
#+begin_src j :session :exports code
4938
#+end_src

j/19/Day4

+46-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,57 @@
55
enough time for this solution to run. It does still work from a
66
~jconsole~ repl.
77

8-
#+begin_src j :session :exports both :tangle 4.ijs
8+
#+begin_src j :session :exports code
99
eg =: 228888,111111,123346,123350,123789,123345,123999
10-
input =: 134792 }. i.675810
10+
input =: 134792+i.(675810-134792)
1111

12+
#+end_src
13+
14+
#+RESULTS:
15+
16+
I am sure there's either a built in way to do this, or a better
17+
way. ~digits~ is the slowest part of the solution. Recursively
18+
div/mods by 10 and appends to a list.
19+
20+
#+BEGIN_SRC j :session :exports code
1221
digits =: 1&$`($:@:(<.@:%&10),10&|)@.(9&<)
13-
goodA =: 0&e.@~:*.]-:/:~
14-
goodB =: (_4&e.+.2&e.)@(-_1&|.)@I.@~:*.]-:/:~
22+
#+END_SRC
23+
24+
#+RESULTS:
25+
26+
Check that the digit list is sorted and check that some index is a
27+
repeat (~0&e.~). Since the list is sorted, any repeat must be a
28+
consecutive one.
29+
30+
#+BEGIN_SRC j :session :exports code
31+
goodA =: 0&e.@~: *. (-: /:~)
32+
#+END_SRC
33+
34+
#+RESULTS:
35+
36+
Check for sorted digit list again, but here the requirements are a
37+
more complicated. There must be a repeat of length exactly 2. We
38+
diverge slightly from the idea above, by looking at the differences
39+
between consecutive digits.
40+
41+
Verb uses ~I.~ to get values at classified indices, rotates by ~_1~
42+
and takes the difference. The last trick is that this solution will
43+
miss numbers like ~111122~ which has the repeat of 2 at the end of
44+
the string -- to fix this, look for negative ~_4~ which is in the
45+
list of differences precisely in this situation.
46+
47+
#+BEGIN_SRC j :session :exports code
48+
goodB =: (_4&e.+.2&e.) @ (-_1&|.) @ I. @ ~: *. (-:/:~)
49+
#+END_SRC
50+
51+
#+RESULTS:
52+
53+
print!
54+
55+
#+BEGIN_SRC j :session :exports both
1556
<"0 (] ,. (goodA ,. goodB) @: digits"0) eg
1657
([: +/ (goodA , goodB) @: digits"0) input
17-
#+end_src
58+
#+END_SRC
1859

1960
#+RESULTS:
2061
#+begin_example
@@ -34,4 +75,3 @@ goodB =: (_4&e.+.2&e.)@(-_1&|.)@I.@~:*.]-:/:~
3475
│123999│1│0│
3576
└──────┴─┴─┘
3677
#+end_example
37-

scheme/17/Day24

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -*- mode: org -*-
2+
#+title: [[https://adventofcode.com/2017/day/24][Day 24: Electromagnetic Moat]]
3+
4+
Clearing of the throat
5+
6+
#+BEGIN_SRC scheme :session :exports code
7+
(load "~/code/advent/load.ss")
8+
9+
(advent-year 17)
10+
(advent-day 24)
11+
12+
(import (prefix (patricia) t:)
13+
(prefix (patricia-set) s:))
14+
#+END_SRC
15+
16+
#+RESULTS:
17+
: #<void>
18+
19+
Rather ugly parsing of the bridges. The pool of available pieces
20+
shall be stored in a map of sets.
21+
22+
#+BEGIN_SRC scheme :session :exports code
23+
(define bridges
24+
(fold-right (lambda (bridge T)
25+
(let-values (((x y) (apply values bridge)))
26+
(t:insert-with s:union y (s:singleton x)
27+
(t:insert-with s:union x (s:singleton y) T))))
28+
t:empty-tree
29+
(n-tuples 2
30+
(parse-advent
31+
(input-chars-transform-parse
32+
(lambda (x)
33+
(if (char=? x #\/)
34+
#\space
35+
x))
36+
input)))))
37+
#+END_SRC
38+
39+
#+RESULTS:
40+
: #<void>
41+
42+
As we select a bridge to connect, we delete it from the pool. All
43+
paths are expanded from a node, which have an endpoint that agrees.
44+
45+
#+begin_src scheme :session :exports code
46+
47+
(define (connections pin bridges)
48+
(s:set->list
49+
(t:lookup-with-default pin s:empty-set bridges)))
50+
51+
(define (connect in out bridges)
52+
(t:modify (lambda (S)
53+
(s:delete in S))
54+
out
55+
(t:modify (lambda (S) (s:delete out S))
56+
in
57+
bridges)))
58+
59+
(define (expand pin network)
60+
`(,pin ,@(map (lambda (out)
61+
(expand out (connect pin out network)))
62+
(connections pin network))))
63+
#+end_src
64+
65+
#+RESULTS:
66+
: #<void>
67+
68+
To find the strongest and deepest paths, we expore them all
69+
recursively. Two variables are visible for ~set!~ ing as we find
70+
better paths.
71+
72+
#+begin_src scheme :session :exports code
73+
(define (explore root network)
74+
(let ((deepest 0)
75+
(strongest 0)
76+
(global 0))
77+
(let walk ((level 0) (signal root) (pin root) (network network))
78+
(let ((outs (connections pin network)))
79+
(cond ((null? outs)
80+
(when (or (> level deepest)
81+
(and (= level deepest)
82+
(> signal strongest)))
83+
(set! deepest level)
84+
(set! strongest signal))
85+
(when (> signal global)
86+
(set! global signal)))
87+
(else
88+
(for-all (lambda (out)
89+
(walk (1+ level)
90+
(+ signal pin out)
91+
out
92+
(connect pin out network)))
93+
outs)))))
94+
(values global strongest)))
95+
#+end_src
96+
97+
#+RESULTS:
98+
: #<void>
99+
100+
101+
And printing
102+
103+
#+BEGIN_SRC scheme :session :results output
104+
(let-values (((part-a part-b) (explore 0 bridges)))
105+
(format #t "part-a: ~a~%part-b: ~a~%" part-a part-b))
106+
#+END_SRC
107+
108+
#+RESULTS:
109+
: part-a: 1868
110+
: part-b: 1841
111+

scheme/19/1.ss

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
(define (fuel+ mass)
1111
(if (< 0 mass) (+ mass (fuel+ (fuel mass))) 0))
1212

13-
(define (part-a)
13+
(define (part-a masses)
1414
(apply + (map fuel masses)))
1515

16-
(define (part-b)
16+
(define (part-b masses)
1717
(apply + (map (compose fuel+ fuel) masses)))

scheme/19/Day5

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# -*- mode: org -*-
22
#+title: [[https://adventofcode.com/2019/day/5][Day 5: Sunny with a Chance of Asteroids]]
33

4-
I would have like to solve this in ~J~ like day 2, but the amount of
5-
conditional referencing feels a bit intimidating at my current skill
6-
level.
7-
8-
** Parsing
4+
** Reading
95

106
Intcode programs are given as a comma separated list of ints. The
117
some code in [[./../../code/][code/]] that makes getting a hold of this more
@@ -63,6 +59,7 @@
6359
#+end_src
6460

6561
#+RESULTS:
62+
: #<void>
6663

6764
** Executing
6865

@@ -119,7 +116,7 @@
119116
#+end_src
120117

121118
#+RESULTS:
122-
: #<void>
119+
>
123120

124121
To run a program to completion, we wait until the ~ip~ does not
125122
advance (opcode 99).
@@ -136,22 +133,19 @@
136133
#+RESULTS:
137134
: #<void>
138135

139-
** Solving
140136

141137
Finally, we ~run~ and read ~out~.
142138

143-
#+begin_src scheme :session :exports both :results output
144-
(display-ln
139+
#+begin_src scheme :session :exports both :results value
140+
(cons
145141
(parameterize ((in 1))
146142
(run)
147-
(out)))
148-
149-
(display-ln
143+
(out))
150144
(parameterize ((in 5))
151145
(run)
152146
(out)))
153147
#+end_src
154148

155149
#+RESULTS:
156-
: (7265618 7731427)
150+
: (7265618 . 7731427)
157151

0 commit comments

Comments
 (0)