Day 23, part 2 - decompiled input by hand
This commit is contained in:
parent
746da09956
commit
f4b9ddef4b
|
@ -1,5 +1,6 @@
|
|||
*
|
||||
!.gitignore
|
||||
!*.hs
|
||||
!*.c
|
||||
!*.js
|
||||
!*.txt
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// unoptimized translation of input
|
||||
unsigned int naiveCount() {
|
||||
unsigned short h = 0;
|
||||
for (long b = 108100; b < 125100; b += 17) {
|
||||
bool f = true;
|
||||
for (long d = 2; d < b; d++) {
|
||||
for (long e = 2; e < b; e++) {
|
||||
if (d * e == b) {
|
||||
f = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
h += !f
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
unsigned int count() {
|
||||
unsigned short h = 0;
|
||||
for (long b = 108100; b < 125100; b += 17) {
|
||||
bool f = true;
|
||||
for (unsigned short d = 2; f && d * d <= b; d++) {
|
||||
if (b % d == 0) {
|
||||
f = false;
|
||||
}
|
||||
}
|
||||
h += !f;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%hu\n", count());
|
||||
}
|
10
23.hs
10
23.hs
|
@ -4,7 +4,6 @@ import Data.Char (ord)
|
|||
import Data.Sequence (Seq, fromList, index)
|
||||
import Data.Vector.Unboxed (Vector, (!), (//))
|
||||
import qualified Data.Vector.Unboxed as V (replicate)
|
||||
import Debug.Trace
|
||||
|
||||
type Registers = Vector Int
|
||||
type Instruction = State -> State
|
||||
|
@ -29,10 +28,10 @@ getValue r v = case v of
|
|||
c -> r ! (getIndex c)
|
||||
|
||||
app :: Function -> Value -> Value -> Instruction
|
||||
app (Function op fn) index value state@(State reg pos cnt) =
|
||||
let i = getIndex index
|
||||
app (Function op fn) x y (State reg pos cnt) =
|
||||
let i = getIndex x
|
||||
in State {
|
||||
registers = reg // [(i, reg ! i `fn` getValue reg value)],
|
||||
registers = reg // [(i, reg ! i `fn` getValue reg y)],
|
||||
position = pos + 1,
|
||||
countMul = cnt + fromEnum (op == Mul)
|
||||
}
|
||||
|
@ -56,7 +55,7 @@ parseLine str =
|
|||
Nothing -> Register $ head s
|
||||
|
||||
runInstructions :: Seq Instruction -> State -> Int
|
||||
runInstructions instructions state@(State reg pos cnt) =
|
||||
runInstructions instructions state@(State _ pos cnt) =
|
||||
if pos >= length instructions then cnt else
|
||||
let !nextState = instructions `index` pos $ state in runInstructions instructions nextState
|
||||
|
||||
|
@ -64,4 +63,3 @@ main :: IO ()
|
|||
main = do
|
||||
instructions <- fromList . map parseLine . lines <$> readFile "23.txt"
|
||||
print $ runInstructions instructions (State (V.replicate 8 0) 0 0)
|
||||
print $ runInstructions instructions (State (V.replicate 8 0 // [(0, 1)]) 0 0)
|
Loading…
Reference in New Issue